Sanjayraju30 commited on
Commit
8211ee7
·
verified ·
1 Parent(s): 6257859

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +20 -15
ocr_engine.py CHANGED
@@ -6,7 +6,6 @@ import re
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
  def enhance_image(img):
9
- # Resize big images
10
  max_dim = 1000
11
  height, width = img.shape[:2]
12
  if max(height, width) > max_dim:
@@ -20,9 +19,9 @@ def enhance_image(img):
20
  sharp = cv2.filter2D(gray, -1, kernel)
21
 
22
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
23
- contrast = clahe.apply(sharp)
24
 
25
- return contrast
26
 
27
  def extract_weight_from_image(pil_img):
28
  try:
@@ -32,34 +31,40 @@ def extract_weight_from_image(pil_img):
32
  results = reader.readtext(enhanced)
33
  print("DEBUG OCR RESULTS:", results)
34
 
35
- ocr_texts = [text for _, text, _ in results]
 
 
 
36
  weight_candidates = []
37
 
38
  for _, text, conf in results:
 
39
  cleaned = text.lower().strip()
40
-
41
- # Fix common misreads
42
- cleaned = cleaned.replace(",", ".") # comma → dot
43
  cleaned = cleaned.replace("o", "0").replace("O", "0")
44
  cleaned = cleaned.replace("s", "5").replace("S", "5")
45
  cleaned = cleaned.replace("g", "9").replace("G", "6")
46
  cleaned = cleaned.replace("kg", "").replace("kgs", "")
47
- cleaned = re.sub(r"[^\d\.]", "", cleaned) # Keep only digits + dot
 
 
48
 
49
- # Match: 2 to 4 digits, optional .digit
50
- if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
51
  weight_candidates.append((cleaned, conf))
52
 
53
  if not weight_candidates:
54
- return "Not detected", 0.0, "\n".join(ocr_texts)
55
 
56
- # Get highest confidence result
57
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
58
 
59
- # Remove leading zeros
60
- best_weight = best_weight.lstrip('0') or '0'
 
 
 
 
61
 
62
- return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
63
 
64
  except Exception as e:
65
  return f"Error: {str(e)}", 0.0, "OCR failed"
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
  def enhance_image(img):
 
9
  max_dim = 1000
10
  height, width = img.shape[:2]
11
  if max(height, width) > max_dim:
 
19
  sharp = cv2.filter2D(gray, -1, kernel)
20
 
21
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
22
+ enhanced = clahe.apply(sharp)
23
 
24
+ return enhanced
25
 
26
  def extract_weight_from_image(pil_img):
27
  try:
 
31
  results = reader.readtext(enhanced)
32
  print("DEBUG OCR RESULTS:", results)
33
 
34
+ if not results:
35
+ return "No text detected", 0.0, "OCR returned empty list"
36
+
37
+ all_texts = []
38
  weight_candidates = []
39
 
40
  for _, text, conf in results:
41
+ original = text
42
  cleaned = text.lower().strip()
43
+ cleaned = cleaned.replace(",", ".")
 
 
44
  cleaned = cleaned.replace("o", "0").replace("O", "0")
45
  cleaned = cleaned.replace("s", "5").replace("S", "5")
46
  cleaned = cleaned.replace("g", "9").replace("G", "6")
47
  cleaned = cleaned.replace("kg", "").replace("kgs", "")
48
+ cleaned = re.sub(r"[^\d\.]", "", cleaned)
49
+
50
+ all_texts.append(f"{original} → {cleaned} (conf: {round(conf, 2)})")
51
 
52
+ if re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
 
53
  weight_candidates.append((cleaned, conf))
54
 
55
  if not weight_candidates:
56
+ return "Not detected", 0.0, "\n".join(all_texts)
57
 
 
58
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
59
 
60
+ if "." in best_weight:
61
+ parts = best_weight.split(".")
62
+ parts[0] = parts[0].lstrip("0") or "0"
63
+ best_weight = ".".join(parts)
64
+ else:
65
+ best_weight = best_weight.lstrip("0") or "0"
66
 
67
+ return best_weight, round(best_conf * 100, 2), "\n".join(all_texts)
68
 
69
  except Exception as e:
70
  return f"Error: {str(e)}", 0.0, "OCR failed"