Sanjayraju30 commited on
Commit
5d38db5
·
verified ·
1 Parent(s): f744985

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +16 -7
ocr_engine.py CHANGED
@@ -1,28 +1,37 @@
1
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
- from PIL import Image
3
  import re
4
 
5
- # Load lightweight, fast, public model
6
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
7
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
8
 
9
  def extract_weight(image: Image.Image) -> str:
10
- image = image.convert("RGB")
11
- pixel_values = processor(images=image, return_tensors="pt").pixel_values
 
 
 
 
 
 
 
 
 
 
12
  generated_ids = model.generate(pixel_values, max_length=32)
13
  full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
14
 
15
- # Extract number
16
  cleaned = full_text.lower().replace(" ", "")
17
  match = re.search(r"(\d+(\.\d+)?)", cleaned)
18
  weight = match.group(1) if match else None
19
 
20
- # Detect unit
21
  if any(u in cleaned for u in ["kg", "kgs", "kilogram", "kilo"]):
22
  unit = "kg"
23
  elif any(u in cleaned for u in ["g", "gram", "grams"]):
24
  unit = "grams"
25
  else:
26
- unit = "kg" if weight and float(weight) >= 5 else "grams"
27
 
28
  return f"{weight} {unit}" if weight else ""
 
1
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
+ from PIL import Image, ImageEnhance
3
  import re
4
 
5
+ # Load processor + model
6
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
7
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
8
 
9
  def extract_weight(image: Image.Image) -> str:
10
+ # Crop only display region (adjust based on your image format)
11
+ width, height = image.size
12
+ display_area = image.crop((width * 0.35, height * 0.1, width * 0.65, height * 0.25)) # crop display center
13
+
14
+ # Enhance contrast & sharpness
15
+ display_area = display_area.convert("L") # grayscale
16
+ display_area = ImageEnhance.Contrast(display_area).enhance(2.0)
17
+ display_area = ImageEnhance.Sharpness(display_area).enhance(2.5)
18
+ display_area = display_area.convert("RGB")
19
+
20
+ # OCR
21
+ pixel_values = processor(images=display_area, return_tensors="pt").pixel_values
22
  generated_ids = model.generate(pixel_values, max_length=32)
23
  full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
24
 
25
+ # Clean & parse
26
  cleaned = full_text.lower().replace(" ", "")
27
  match = re.search(r"(\d+(\.\d+)?)", cleaned)
28
  weight = match.group(1) if match else None
29
 
 
30
  if any(u in cleaned for u in ["kg", "kgs", "kilogram", "kilo"]):
31
  unit = "kg"
32
  elif any(u in cleaned for u in ["g", "gram", "grams"]):
33
  unit = "grams"
34
  else:
35
+ unit = "kg" if weight and float(weight) >= 20 else "grams"
36
 
37
  return f"{weight} {unit}" if weight else ""