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

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +14 -14
ocr_engine.py CHANGED
@@ -2,31 +2,31 @@ 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"]):
@@ -34,4 +34,4 @@ def extract_weight(image: Image.Image) -> str:
34
  else:
35
  unit = "kg" if weight and float(weight) >= 20 else "grams"
36
 
37
- return f"{weight} {unit}" if weight else ""
 
2
  from PIL import Image, ImageEnhance
3
  import re
4
 
5
+ # Load OCR 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
+ # Step 1: Enhance image
11
+ image = image.convert("L") # grayscale
12
+ image = ImageEnhance.Contrast(image).enhance(2.0)
13
+ image = ImageEnhance.Sharpness(image).enhance(2.5)
14
+ image = image.convert("RGB")
15
 
16
+ # Step 2: Run OCR
17
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
 
 
 
 
 
 
18
  generated_ids = model.generate(pixel_values, max_length=32)
19
  full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
20
 
21
+ # Debug
22
+ print("OCR Output:", full_text)
23
+
24
+ # Step 3: Extract number
25
  cleaned = full_text.lower().replace(" ", "")
26
  match = re.search(r"(\d+(\.\d+)?)", cleaned)
27
  weight = match.group(1) if match else None
28
 
29
+ # Step 4: Detect unit based on actual OCR text
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"]):
 
34
  else:
35
  unit = "kg" if weight and float(weight) >= 20 else "grams"
36
 
37
+ return f"{weight} {unit}" if weight else "No valid weight detected"