Spaces:
Build error
Build error
File size: 910 Bytes
7e1096c 1362c73 7e1096c 9c7d97d 7e1096c c11fb3a 9c7d97d 7e1096c 9c7d97d 7e1096c 9c7d97d 7e1096c 07d4fed 7e1096c 9c7d97d 7e1096c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import cv2
import pytesseract
import numpy as np
from PIL import Image
def extract_weight_from_image(pil_img):
try:
img = pil_img.convert("L") # grayscale
img = np.array(img)
img = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
blur = cv2.GaussianBlur(img, (5, 5), 0)
thresh = cv2.adaptiveThreshold(
blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 11, 2
)
config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
text = pytesseract.image_to_string(thresh, config=config)
print("🔍 OCR RAW OUTPUT:", repr(text))
weight = ''.join(filter(lambda c: c in '0123456789.', text)).strip()
confidence = 95 if weight else 0
return weight, confidence
except Exception as e:
print("❌ OCR Exception:", str(e))
return "", 0
|