Sanjayraju30 commited on
Commit
5b73d24
·
verified ·
1 Parent(s): 447adb6

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +11 -15
ocr_engine.py CHANGED
@@ -1,32 +1,28 @@
 
1
  import numpy as np
2
- import re
3
  import cv2
4
- import pytesseract
5
 
6
  def extract_weight_from_image(pil_img):
7
  try:
8
  img = np.array(pil_img)
9
 
10
- # Convert to grayscale
11
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 
 
 
12
 
13
- # Resize (sharpens small digits)
14
- gray = cv2.resize(gray, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC)
 
 
15
 
16
- # Thresholding to clean up image
17
- _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
18
-
19
- # Run Tesseract OCR
20
- custom_config = r'--oem 3 --psm 6'
21
- text = pytesseract.image_to_string(thresh, config=custom_config)
22
- print("OCR Text:", text)
23
-
24
- # Extract weight pattern like 25.50 or 150
25
  match = re.search(r"\b\d{1,4}\.?\d{0,2}\b", text)
26
  if match:
27
  return match.group(), 95.0
28
  else:
29
  return "No weight detected", 0.0
30
-
31
  except Exception as e:
32
  return f"Error: {str(e)}", 0.0
 
1
+ import pytesseract
2
  import numpy as np
 
3
  import cv2
4
+ import re
5
 
6
  def extract_weight_from_image(pil_img):
7
  try:
8
  img = np.array(pil_img)
9
 
10
+ # Preprocessing
11
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
12
+ resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
13
+ blur = cv2.GaussianBlur(resized, (5, 5), 0)
14
+ _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
15
 
16
+ # OCR using pytesseract
17
+ config = "--psm 6" # Assume a single uniform block of text
18
+ text = pytesseract.image_to_string(thresh, config=config)
19
+ print("OCR Output:", text)
20
 
21
+ # Regex to find weight-like numbers
 
 
 
 
 
 
 
 
22
  match = re.search(r"\b\d{1,4}\.?\d{0,2}\b", text)
23
  if match:
24
  return match.group(), 95.0
25
  else:
26
  return "No weight detected", 0.0
 
27
  except Exception as e:
28
  return f"Error: {str(e)}", 0.0