Sanjayraju30 commited on
Commit
33069a9
·
verified ·
1 Parent(s): 7dd3534

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +26 -13
ocr_engine.py CHANGED
@@ -9,24 +9,37 @@ def extract_weight_from_image(pil_img):
9
  try:
10
  img = np.array(pil_img)
11
 
12
- # Step 1: Convert to grayscale
13
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
14
 
15
- # Step 2: Apply adaptive threshold to handle lighting
16
- thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
17
- cv2.THRESH_BINARY_INV, 11, 2)
18
 
19
- # Step 3: Dilate to make digits thicker
20
- kernel = np.ones((2, 2), np.uint8)
21
- dilated = cv2.dilate(thresh, kernel, iterations=1)
22
 
23
- # Step 4: OCR on the preprocessed image
24
- result = reader.readtext(dilated, detail=0)
25
- text = " ".join(result).strip()
26
- print("OCR Text:", text)
27
 
28
- # Step 5: Match numeric values like 52.30 or 003.25
29
- match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  if match:
31
  return match.group(), 95.0
32
  else:
 
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: