Sanjayraju30 commited on
Commit
a71f519
·
verified ·
1 Parent(s): 91787ba

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +16 -26
ocr_engine.py CHANGED
@@ -1,7 +1,7 @@
1
  import easyocr
2
  import numpy as np
3
- import re
4
  import cv2
 
5
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
@@ -9,37 +9,27 @@ def extract_weight_from_image(pil_img):
9
  try:
10
  img = np.array(pil_img)
11
 
12
- # Convert to grayscale
13
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 
14
 
15
- # Apply bilateral filter to reduce noise while keeping edges
16
- filtered = cv2.bilateralFilter(gray, 11, 17, 17)
17
-
18
- # Apply binary threshold
19
- _, thresh = cv2.threshold(filtered, 150, 255, cv2.THRESH_BINARY_INV)
20
-
21
- # Find contours
22
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
23
-
24
- if not contours:
25
- return "No weight detected", 0.0
26
 
27
- # Get the largest contour assuming it's the display area
28
- largest_contour = max(contours, key=cv2.contourArea)
29
- x, y, w, h = cv2.boundingRect(largest_contour)
30
 
31
- # Add padding
32
- pad = 10
33
- x, y = max(x - pad, 0), max(y - pad, 0)
34
- cropped = gray[y:y+h+pad, x:x+w+pad]
35
 
36
- # OCR on cropped area
37
- result = reader.readtext(cropped, detail=0)
38
- combined = " ".join(result)
39
- print("Detected Text:", combined)
40
 
41
- # Match weight patterns like 52.30 or 003.25
42
- match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", combined)
43
  if match:
44
  return match.group(), 95.0
45
  else:
 
1
  import easyocr
2
  import numpy as np
 
3
  import cv2
4
+ import re
5
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
 
9
  try:
10
  img = np.array(pil_img)
11
 
12
+ # Grayscale + resize
13
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
14
+ gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
15
 
16
+ # Histogram equalization
17
+ gray = cv2.equalizeHist(gray)
 
 
 
 
 
 
 
 
 
18
 
19
+ # Adaptive threshold
20
+ thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
21
+ cv2.THRESH_BINARY, 11, 2)
22
 
23
+ # Invert colors (LCD digits are usually dark on light)
24
+ thresh = cv2.bitwise_not(thresh)
 
 
25
 
26
+ # OCR
27
+ result = reader.readtext(thresh, detail=0)
28
+ combined_text = " ".join(result)
29
+ print("OCR Text:", combined_text)
30
 
31
+ # Match weight pattern (e.g. 52.30, 003.2, 250)
32
+ match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", combined_text)
33
  if match:
34
  return match.group(), 95.0
35
  else: