Sanjayraju30 commited on
Commit
f901f58
·
verified ·
1 Parent(s): 103f82b

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +21 -19
ocr_engine.py CHANGED
@@ -9,33 +9,35 @@ def extract_weight_from_image(pil_img):
9
  try:
10
  img = np.array(pil_img)
11
 
12
- # Convert to grayscale and 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 and adaptive threshold
17
- gray = cv2.equalizeHist(gray)
18
- thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
19
- cv2.THRESH_BINARY, 11, 2)
20
- thresh = cv2.bitwise_not(thresh)
21
 
22
- # OCR with bounding boxes
 
 
 
 
23
  results = reader.readtext(thresh)
24
 
25
- # Filter potential weight values
26
- candidates = []
27
- for (bbox, text, confidence) in results:
28
- # Clean text
29
- clean_text = text.replace('kg', '').strip()
30
- if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", clean_text):
31
- candidates.append((clean_text, confidence))
 
32
 
33
- if not candidates:
34
  return "Not detected", 0.0
35
 
36
- # Choose the highest confidence match
37
- best_weight, conf = sorted(candidates, key=lambda x: -x[1])[0]
38
- return best_weight, round(conf, 2)
39
 
40
  except Exception as e:
41
  return f"Error: {str(e)}", 0.0
 
9
  try:
10
  img = np.array(pil_img)
11
 
12
+ # Resize and convert to grayscale
13
+ img = cv2.resize(img, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_LINEAR)
14
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 
15
 
16
+ # Apply Gaussian blur to remove noise
17
+ blurred = cv2.GaussianBlur(gray, (5, 5), 0)
 
 
 
18
 
19
+ # Apply adaptive threshold
20
+ thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
21
+ cv2.THRESH_BINARY_INV, 15, 6)
22
+
23
+ # OCR
24
  results = reader.readtext(thresh)
25
 
26
+ # Debug: Print all detected text
27
+ print("OCR Results:", results)
28
+
29
+ weight_candidates = []
30
+ for _, text, conf in results:
31
+ text = text.lower().replace('kg', '').replace('kgs', '').strip()
32
+ if re.match(r'^\d{2,4}(\.\d{1,2})?$', text):
33
+ weight_candidates.append((text, conf))
34
 
35
+ if not weight_candidates:
36
  return "Not detected", 0.0
37
 
38
+ # Return the one with highest confidence
39
+ weight, confidence = sorted(weight_candidates, key=lambda x: -x[1])[0]
40
+ return weight, round(confidence * 100, 2)
41
 
42
  except Exception as e:
43
  return f"Error: {str(e)}", 0.0