Sanjayraju30 commited on
Commit
f617821
·
verified ·
1 Parent(s): 66afd91

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +19 -17
ocr_engine.py CHANGED
@@ -9,29 +9,31 @@ def extract_weight_from_image(pil_img):
9
  try:
10
  img = np.array(pil_img)
11
 
12
- # Preprocessing for OCR
 
 
 
 
13
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
14
- gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
15
  gray = cv2.equalizeHist(gray)
16
- gray = cv2.GaussianBlur(gray, (3, 3), 0)
17
- inverted = cv2.bitwise_not(gray)
 
 
18
 
19
- # OCR read
20
- result = reader.readtext(inverted, detail=0)
21
  combined_text = " ".join(result)
22
- print("OCR Raw Text:", combined_text)
23
 
24
- # First try matching with "kg"
25
- match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)\s*(kg|KG|Kg)", combined_text)
26
  if match:
27
- return f"{match.group(1)} kg", 95.0
28
-
29
- # Then try fallback match: only numbers
30
- fallback = re.search(r"\d{1,4}(?:\.\d{1,2})?", combined_text)
31
- if fallback:
32
- return f"{fallback.group(0)} kg", 75.0
33
-
34
- return "No weight detected kg", 0.0
35
 
36
  except Exception as e:
37
  return f"Error: {str(e)}", 0.0
 
9
  try:
10
  img = np.array(pil_img)
11
 
12
+ # Resize if image is too large
13
+ if img.shape[1] > 1000:
14
+ img = cv2.resize(img, (1000, int(img.shape[0] * 1000 / img.shape[1])))
15
+
16
+ # Preprocessing
17
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
18
+ gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
19
  gray = cv2.equalizeHist(gray)
20
+ blurred = cv2.GaussianBlur(gray, (3, 3), 0)
21
+ thresh = cv2.adaptiveThreshold(
22
+ blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2
23
+ )
24
 
25
+ # OCR
26
+ result = reader.readtext(thresh, detail=0)
27
  combined_text = " ".join(result)
28
+ print("OCR Text:", combined_text)
29
 
30
+ # Regex to match numbers with optional 'kg'
31
+ match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)\s*(kg|KG|Kg)?", combined_text)
32
  if match:
33
+ weight = match.group(1)
34
+ return f"{weight} kg", 95.0
35
+ else:
36
+ return "No weight detected kg", 0.0
 
 
 
 
37
 
38
  except Exception as e:
39
  return f"Error: {str(e)}", 0.0