Sanjayraju30 commited on
Commit
2132698
·
verified ·
1 Parent(s): 0fd82a1

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +5 -21
ocr_engine.py CHANGED
@@ -1,46 +1,30 @@
1
- import easyocr
2
- import numpy as np
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
- # Resize and grayscale
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)
20
 
21
- # Run OCR
22
  results = reader.readtext(thresh)
23
-
24
- print("OCR Results:", results)
25
 
26
  weight_candidates = []
27
  for _, text, conf in results:
28
- cleaned = text.lower()
29
- cleaned = cleaned.replace("kg", "").replace("kgs", "")
30
  cleaned = cleaned.replace("o", "0").replace("O", "0")
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:
40
- return "Not detected", 0.0
41
 
42
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
43
- return best_weight, round(best_conf * 100, 2)
44
 
45
  except Exception as e:
46
- return f"Error: {str(e)}", 0.0
 
 
 
 
 
 
 
 
1
  def extract_weight_from_image(pil_img):
2
  try:
3
  img = np.array(pil_img)
 
 
4
  img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
5
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 
 
6
  gray = cv2.bilateralFilter(gray, 11, 17, 17)
7
  thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
8
  cv2.THRESH_BINARY_INV, 11, 2)
9
 
 
10
  results = reader.readtext(thresh)
11
+ all_texts = [text for _, text, _ in results]
 
12
 
13
  weight_candidates = []
14
  for _, text, conf in results:
15
+ cleaned = text.lower().replace("kg", "").replace("kgs", "")
 
16
  cleaned = cleaned.replace("o", "0").replace("O", "0")
17
  cleaned = cleaned.replace("s", "5").replace("S", "5")
18
  cleaned = cleaned.replace("g", "9").replace("G", "6")
 
19
  cleaned = re.sub(r"[^\d\.]", "", cleaned)
 
20
  if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
21
  weight_candidates.append((cleaned, conf))
22
 
23
  if not weight_candidates:
24
+ return "Not detected", 0.0, "\n".join(all_texts)
25
 
26
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
27
+ return best_weight, round(best_conf * 100, 2), "\n".join(all_texts)
28
 
29
  except Exception as e:
30
+ return f"Error: {str(e)}", 0.0, "OCR failed"