Sanjayraju30 commited on
Commit
5a025ab
·
verified ·
1 Parent(s): 292bc54

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +5 -6
ocr_engine.py CHANGED
@@ -3,21 +3,18 @@ import numpy as np
3
  import cv2
4
  import re
5
 
6
- # Load EasyOCR reader
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 if large
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
- # OCR
21
  results = reader.readtext(img)
22
  print("DEBUG OCR RESULTS:", results)
23
 
@@ -26,7 +23,10 @@ def extract_weight_from_image(pil_img):
26
  fallback_weight = None
27
  fallback_conf = 0.0
28
 
29
- for _, (text, conf) in results: # ✅ Correct unpacking
 
 
 
30
  original = text
31
  cleaned = text.lower().strip()
32
 
@@ -46,7 +46,6 @@ def extract_weight_from_image(pil_img):
46
  if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
47
  weight_candidates.append((cleaned, conf))
48
 
49
- # Choose result
50
  if weight_candidates:
51
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
52
  elif fallback_weight:
@@ -54,7 +53,7 @@ def extract_weight_from_image(pil_img):
54
  else:
55
  return "Not detected", 0.0, "OCR returned nothing useful"
56
 
57
- # Clean leading zeros
58
  if "." in best_weight:
59
  int_part, dec_part = best_weight.split(".")
60
  int_part = int_part.lstrip("0") or "0"
 
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 item in results:
27
+ box, content = item # ✅ This fixes the unpacking error
28
+ text, conf = content
29
+
30
  original = text
31
  cleaned = text.lower().strip()
32
 
 
46
  if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
47
  weight_candidates.append((cleaned, conf))
48
 
 
49
  if weight_candidates:
50
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
51
  elif fallback_weight:
 
53
  else:
54
  return "Not detected", 0.0, "OCR returned nothing useful"
55
 
56
+ # Remove leading zeros
57
  if "." in best_weight:
58
  int_part, dec_part = best_weight.split(".")
59
  int_part = int_part.lstrip("0") or "0"