Sanjayraju30 commited on
Commit
393f381
·
verified ·
1 Parent(s): a51774c

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +10 -6
ocr_engine.py CHANGED
@@ -4,9 +4,13 @@ import numpy as np
4
  from PIL import Image
5
 
6
  def extract_weight_from_image(image):
7
- image = np.array(image)
8
- gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
9
- text = pytesseract.image_to_string(gray, config='--psm 7 digits')
10
- weight = ''.join(filter(lambda x: x in '0123456789.', text))
11
- confidence = 95 # Placeholder
12
- return weight.strip(), confidence
 
 
 
 
 
4
  from PIL import Image
5
 
6
  def extract_weight_from_image(image):
7
+ try:
8
+ image = np.array(image.convert("RGB")) # Ensure RGB format
9
+ gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
10
+ blur = cv2.GaussianBlur(gray, (3, 3), 0) # Optional: denoise
11
+ text = pytesseract.image_to_string(blur, config='--psm 7 digits')
12
+ weight = ''.join(filter(lambda x: x in '0123456789.', text))
13
+ confidence = 95 # Placeholder
14
+ return weight.strip(), confidence
15
+ except Exception as e:
16
+ return "", 0