Sanjayraju30 commited on
Commit
c925f8d
·
verified ·
1 Parent(s): 7a8e198

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +12 -22
ocr_engine.py CHANGED
@@ -6,22 +6,28 @@ import re
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:
12
  scale = max_dim / max(height, width)
13
  img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
14
 
 
15
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 
 
16
  gray = cv2.fastNlMeansDenoising(gray, h=15)
17
 
18
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
 
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:
@@ -35,34 +41,18 @@ def extract_weight_from_image(pil_img):
35
  weight_candidates = []
36
 
37
  for _, text, conf in results:
38
- cleaned = text.lower().strip()
39
-
40
- # Fix common OCR errors
41
- cleaned = cleaned.replace(",", ".")
42
- cleaned = cleaned.replace("o", "0").replace("O", "0")
43
- cleaned = cleaned.replace("s", "5").replace("S", "5")
44
- cleaned = cleaned.replace("g", "9").replace("G", "6")
45
  cleaned = cleaned.replace("kg", "").replace("kgs", "")
 
46
  cleaned = re.sub(r"[^\d\.]", "", cleaned)
47
 
48
- # Match weights like: 58.8, 75.02, 97.2, 102.34, etc.
49
- if re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
50
  weight_candidates.append((cleaned, conf))
51
 
52
  if not weight_candidates:
53
  return "Not detected", 0.0, "\n".join(ocr_texts)
54
 
55
- # Pick the highest confidence result
56
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
57
-
58
- # Remove unnecessary leading zeros
59
- if "." in best_weight:
60
- parts = best_weight.split(".")
61
- parts[0] = parts[0].lstrip("0") or "0"
62
- best_weight = ".".join(parts)
63
- else:
64
- best_weight = best_weight.lstrip("0") or "0"
65
-
66
  return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
67
 
68
  except Exception as e:
 
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:
13
  scale = max_dim / max(height, width)
14
  img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
15
 
16
+ # Grayscale
17
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
18
+
19
+ # Denoise
20
  gray = cv2.fastNlMeansDenoising(gray, h=15)
21
 
22
+ # Sharpen
23
+ kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]])
24
  sharp = cv2.filter2D(gray, -1, kernel)
25
 
26
+ # Contrast
27
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
28
+ contrast = clahe.apply(sharp)
29
 
30
+ return contrast
31
 
32
  def extract_weight_from_image(pil_img):
33
  try:
 
41
  weight_candidates = []
42
 
43
  for _, text, conf in results:
44
+ cleaned = text.lower()
 
 
 
 
 
 
45
  cleaned = cleaned.replace("kg", "").replace("kgs", "")
46
+ cleaned = cleaned.replace("o", "0").replace("s", "5").replace("g", "9")
47
  cleaned = re.sub(r"[^\d\.]", "", cleaned)
48
 
49
+ if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
 
50
  weight_candidates.append((cleaned, conf))
51
 
52
  if not weight_candidates:
53
  return "Not detected", 0.0, "\n".join(ocr_texts)
54
 
 
55
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
 
 
 
 
 
 
 
 
 
56
  return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
57
 
58
  except Exception as e: