Sanjayraju30 commited on
Commit
83e0faf
·
verified ·
1 Parent(s): 1f19915

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +4 -4
ocr_engine.py CHANGED
@@ -13,7 +13,7 @@ def extract_weight_from_image(pil_img):
13
  img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
14
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
15
 
16
- # Denoise & Adaptive Threshold (better than OTSU for displays)
17
  gray = cv2.bilateralFilter(gray, 11, 17, 17)
18
  thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
19
  cv2.THRESH_BINARY_INV, 11, 2)
@@ -21,7 +21,7 @@ def extract_weight_from_image(pil_img):
21
  # Run OCR
22
  results = reader.readtext(thresh)
23
 
24
- print("OCR Results:", results) # For debugging
25
 
26
  weight_candidates = []
27
  for _, text, conf in results:
@@ -31,9 +31,9 @@ def extract_weight_from_image(pil_img):
31
  cleaned = cleaned.replace("s", "5").replace("S", "5")
32
  cleaned = cleaned.replace("g", "9").replace("G", "6")
33
 
34
- cleaned = re.sub(r"[^\d\.]", "", cleaned) # remove non-numeric
35
 
36
- if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned): # allow 2-4 digit weights
37
  weight_candidates.append((cleaned, conf))
38
 
39
  if not weight_candidates:
 
13
  img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
14
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
15
 
16
+ # Denoise & Adaptive Thresholding
17
  gray = cv2.bilateralFilter(gray, 11, 17, 17)
18
  thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
19
  cv2.THRESH_BINARY_INV, 11, 2)
 
21
  # Run OCR
22
  results = reader.readtext(thresh)
23
 
24
+ print("OCR Results:", results)
25
 
26
  weight_candidates = []
27
  for _, text, conf in results:
 
31
  cleaned = cleaned.replace("s", "5").replace("S", "5")
32
  cleaned = cleaned.replace("g", "9").replace("G", "6")
33
 
34
+ cleaned = re.sub(r"[^\d\.]", "", cleaned)
35
 
36
+ if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
37
  weight_candidates.append((cleaned, conf))
38
 
39
  if not weight_candidates: