Sanjayraju30 commited on
Commit
af7cef1
·
verified ·
1 Parent(s): 0f4e1bf

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +16 -8
ocr_engine.py CHANGED
@@ -1,27 +1,35 @@
1
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
  from PIL import Image
3
 
4
- # Load model + processor
5
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
6
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
7
 
8
  def extract_weight(image: Image.Image) -> str:
 
9
  image = image.convert("RGB")
 
 
10
  pixel_values = processor(images=image, return_tensors="pt").pixel_values
11
  generated_ids = model.generate(pixel_values)
12
  full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
13
 
14
- # Extract digits
15
- weight = ''.join(filter(lambda x: x in '0123456789.', full_text))
16
 
17
- # Check for unit in original OCR text
18
- unit = "grams" # default
19
- if "kg" in full_text.lower():
20
  unit = "kg"
21
- elif "g" in full_text.lower():
22
  unit = "grams"
 
 
23
 
24
- if weight:
 
 
 
 
25
  return f"{weight} {unit}"
26
  else:
27
  return "No valid weight detected"
 
1
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
  from PIL import Image
3
 
4
+ # Load OCR model once
5
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
6
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
7
 
8
  def extract_weight(image: Image.Image) -> str:
9
+ # Ensure image is in RGB
10
  image = image.convert("RGB")
11
+
12
+ # Process with Hugging Face OCR
13
  pixel_values = processor(images=image, return_tensors="pt").pixel_values
14
  generated_ids = model.generate(pixel_values)
15
  full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
16
 
17
+ # Normalize text
18
+ full_text_cleaned = full_text.lower().replace(" ", "")
19
 
20
+ # Detect unit
21
+ if "kg" in full_text_cleaned:
 
22
  unit = "kg"
23
+ elif "g" in full_text_cleaned or "gram" in full_text_cleaned:
24
  unit = "grams"
25
+ else:
26
+ unit = "grams" # default to grams if not clear
27
 
28
+ # Extract number (includes decimals)
29
+ import re
30
+ match = re.search(r"(\d+(\.\d+)?)", full_text_cleaned)
31
+ if match:
32
+ weight = match.group(1)
33
  return f"{weight} {unit}"
34
  else:
35
  return "No valid weight detected"