Sanjayraju30 commited on
Commit
79d34b2
·
verified ·
1 Parent(s): 216f81d

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +6 -11
ocr_engine.py CHANGED
@@ -1,14 +1,9 @@
 
1
  import pytesseract
2
  from PIL import Image
3
- import re
4
 
5
- def extract_weight_from_image(image):
6
- """Extract weight as a decimal number (e.g., 53.76) from the image."""
7
- try:
8
- text = pytesseract.image_to_string(image)
9
- match = re.search(r"\d+.\d{2}", text)
10
- if match:
11
- return match.group()
12
- return "No weight found"
13
- except Exception as e:
14
- return f"Error: {str(e)}"
 
1
+ import cv2
2
  import pytesseract
3
  from PIL import Image
 
4
 
5
+ def extract_weight(img_path):
6
+ img = cv2.imread(img_path)
7
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
8
+ text = pytesseract.image_to_string(gray, config='--psm 7 digits')
9
+ return ''.join(filter(lambda x: x in '0123456789.', text))