Sanjayraju30 commited on
Commit
acddb2f
·
verified ·
1 Parent(s): 90f9c83

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +33 -20
ocr_engine.py CHANGED
@@ -5,38 +5,51 @@ 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=4, fy=4, interpolation=cv2.INTER_LINEAR)
14
- gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
15
- gray = cv2.bilateralFilter(gray, 11, 17, 17)
16
- _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
17
-
18
- results = reader.readtext(thresh)
19
-
20
- ocr_raw_texts = []
21
  weight_candidates = []
22
 
23
  for _, text, conf in results:
24
- ocr_raw_texts.append(text)
25
- t = text.lower()
26
- t = t.replace("kg", "").replace("kgs", "")
27
- t = t.replace("o", "0").replace("O", "0")
28
- t = t.replace("s", "5").replace("S", "5")
29
- t = t.replace("g", "9").replace("G", "6")
30
- t = re.sub(r"[^\d\.]", "", t)
31
 
32
- if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", t):
33
- weight_candidates.append((t, conf))
34
 
35
  if not weight_candidates:
36
- return "Not detected", 0.0, "\n".join(ocr_raw_texts)
37
 
38
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
39
- return best_weight, round(best_conf * 100, 2), "\n".join(ocr_raw_texts)
40
 
41
  except Exception as e:
42
  return f"Error: {str(e)}", 0.0, "OCR failed"
 
5
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
+ def enhance_image(img):
9
+ # Resize to make text clearer
10
+ img = cv2.resize(img, None, fx=4, fy=4, interpolation=cv2.INTER_CUBIC)
11
+
12
+ # Convert to gray
13
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
14
+
15
+ # Denoise
16
+ gray = cv2.fastNlMeansDenoising(gray, h=15)
17
+
18
+ # Sharpen
19
+ kernel = np.array([[0, -1, 0],
20
+ [-1, 5,-1],
21
+ [0, -1, 0]])
22
+ sharp = cv2.filter2D(gray, -1, kernel)
23
+
24
+ # Contrast enhancement (CLAHE)
25
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
26
+ contrast = clahe.apply(sharp)
27
+
28
+ return contrast
29
+
30
  def extract_weight_from_image(pil_img):
31
  try:
32
  img = np.array(pil_img)
33
+ enhanced = enhance_image(img)
34
 
35
+ results = reader.readtext(enhanced)
36
+ ocr_texts = [text for _, text, _ in results]
 
 
 
 
 
 
 
37
  weight_candidates = []
38
 
39
  for _, text, conf in results:
40
+ cleaned = text.lower()
41
+ cleaned = cleaned.replace("kg", "").replace("kgs", "")
42
+ cleaned = cleaned.replace("o", "0").replace("s", "5").replace("g", "9")
43
+ cleaned = re.sub(r"[^\d\.]", "", cleaned)
 
 
 
44
 
45
+ if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
46
+ weight_candidates.append((cleaned, conf))
47
 
48
  if not weight_candidates:
49
+ return "Not detected", 0.0, "\n".join(ocr_texts)
50
 
51
  best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
52
+ return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
53
 
54
  except Exception as e:
55
  return f"Error: {str(e)}", 0.0, "OCR failed"