Sanjayraju30 commited on
Commit
78e8bf3
Β·
verified Β·
1 Parent(s): afdd8dc

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +3 -9
ocr_engine.py CHANGED
@@ -3,21 +3,18 @@ import numpy as np
3
  import cv2
4
  import re
5
 
6
- # Initialize OCR
7
  reader = easyocr.Reader(['en'], gpu=False)
8
 
9
  def extract_weight_from_image(pil_img):
10
  try:
11
  img = np.array(pil_img)
12
 
13
- # Resize large images to prevent OCR failure
14
  max_dim = 1000
15
  height, width = img.shape[:2]
16
  if max(height, width) > max_dim:
17
  scale = max_dim / max(height, width)
18
  img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
19
 
20
- # Read text
21
  results = reader.readtext(img)
22
  print("DEBUG OCR RESULTS:", results)
23
 
@@ -26,21 +23,18 @@ def extract_weight_from_image(pil_img):
26
  fallback_weight = None
27
  fallback_conf = 0.0
28
 
29
- for box, (text, conf) in results: # βœ… CORRECTED unpacking
30
  original = text
31
  cleaned = text.lower().strip()
32
 
33
- # Fix OCR errors
34
  cleaned = cleaned.replace(",", ".")
35
- cleaned = cleaned.replace("o", "0").replace("O", "0")
36
- cleaned = cleaned.replace("s", "5").replace("S", "5")
37
- cleaned = cleaned.replace("g", "9").replace("G", "6")
38
  cleaned = cleaned.replace("kg", "").replace("kgs", "")
39
  cleaned = re.sub(r"[^0-9\.]", "", cleaned)
40
 
41
  raw_texts.append(f"{original} β†’ {cleaned} (conf: {round(conf, 2)})")
42
 
43
- if cleaned and cleaned.replace(".", "").isdigit() and not fallback_weight:
44
  fallback_weight = cleaned
45
  fallback_conf = conf
46
 
 
3
  import cv2
4
  import re
5
 
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
  def extract_weight_from_image(pil_img):
9
  try:
10
  img = np.array(pil_img)
11
 
 
12
  max_dim = 1000
13
  height, width = img.shape[:2]
14
  if max(height, width) > max_dim:
15
  scale = max_dim / max(height, width)
16
  img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
17
 
 
18
  results = reader.readtext(img)
19
  print("DEBUG OCR RESULTS:", results)
20
 
 
23
  fallback_weight = None
24
  fallback_conf = 0.0
25
 
26
+ for box, (text, conf) in results: # βœ… THIS LINE IS CORRECT
27
  original = text
28
  cleaned = text.lower().strip()
29
 
 
30
  cleaned = cleaned.replace(",", ".")
31
+ cleaned = cleaned.replace("o", "0").replace("s", "5").replace("g", "9")
 
 
32
  cleaned = cleaned.replace("kg", "").replace("kgs", "")
33
  cleaned = re.sub(r"[^0-9\.]", "", cleaned)
34
 
35
  raw_texts.append(f"{original} β†’ {cleaned} (conf: {round(conf, 2)})")
36
 
37
+ if cleaned.replace(".", "").isdigit() and not fallback_weight:
38
  fallback_weight = cleaned
39
  fallback_conf = conf
40