Sanjayraju30 commited on
Commit
8348064
·
verified ·
1 Parent(s): 5607fce

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +17 -9
ocr_engine.py CHANGED
@@ -1,36 +1,44 @@
1
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
- from PIL import Image
3
  import torch
4
  import re
5
 
6
- # Load model and processor
7
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
8
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
9
 
10
  def clean_ocr_text(text):
11
  print("[RAW OCR]", text)
12
- text = text.replace(",", ".").replace("s", "5").replace("o", "0").lower()
13
- text = re.sub(r"[^\d\.kg]", "", text)
 
14
  print("[CLEANED OCR]", text)
15
  return text
16
 
 
 
 
 
 
 
17
  def extract_weight(image):
18
  try:
 
19
  pixel_values = processor(images=image, return_tensors="pt").pixel_values
20
  generated_ids = model.generate(pixel_values)
21
  raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
22
 
23
  cleaned = clean_ocr_text(raw_text)
24
 
25
- # First try with unit
26
- match = re.search(r'(\d{1,5}(?:\.\d{1,3})?)\s*(kg|g)', cleaned)
27
  if match:
28
  return f"{match.group(1)} {match.group(2)}"
29
 
30
- # Fallback: only number, assume grams
31
- fallback = re.search(r'(\d{1,5}(?:\.\d{1,3})?)', cleaned)
32
  if fallback:
33
- return f"{fallback.group(1)} g"
34
 
35
  return f"No valid weight found | OCR: {cleaned}"
36
  except Exception as e:
 
1
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
+ from PIL import Image, ImageFilter
3
  import torch
4
  import re
5
 
6
+ # Load TrOCR model
7
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
8
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
9
 
10
  def clean_ocr_text(text):
11
  print("[RAW OCR]", text)
12
+ # Fix common misreads
13
+ text = text.replace(",", ".").replace("s", "5").replace("o", "0").replace("O", "0")
14
+ text = re.sub(r"[^\d\.kg]", "", text.lower()) # Keep digits, dot, kg
15
  print("[CLEANED OCR]", text)
16
  return text
17
 
18
+ def preprocess_image(image):
19
+ # Enlarge + sharpen for better OCR
20
+ image = image.resize((image.width * 2, image.height * 2), Image.BICUBIC)
21
+ image = image.filter(ImageFilter.SHARPEN)
22
+ return image
23
+
24
  def extract_weight(image):
25
  try:
26
+ image = preprocess_image(image)
27
  pixel_values = processor(images=image, return_tensors="pt").pixel_values
28
  generated_ids = model.generate(pixel_values)
29
  raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
30
 
31
  cleaned = clean_ocr_text(raw_text)
32
 
33
+ # Try matching decimal weight with unit
34
+ match = re.search(r'(\d{1,3}\.\d{1,3})\s*(kg|g)', cleaned)
35
  if match:
36
  return f"{match.group(1)} {match.group(2)}"
37
 
38
+ # Fallback: match only decimal number
39
+ fallback = re.search(r'(\d{1,3}\.\d{1,3})', cleaned)
40
  if fallback:
41
+ return f"{fallback.group(1)} g" # Default to grams
42
 
43
  return f"No valid weight found | OCR: {cleaned}"
44
  except Exception as e: