Spaces:
Build error
Build error
File size: 753 Bytes
7e1096c 1362c73 7e1096c ddf0c54 9c7d97d ddf0c54 9c7d97d ddf0c54 c11fb3a ddf0c54 7e1096c ddf0c54 7e1096c ddf0c54 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 |
import cv2
import pytesseract
import numpy as np
from PIL import Image
def extract_weight_from_image(pil_img):
try:
# Convert PIL image to OpenCV
img = pil_img.convert("RGB")
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Preprocess
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
# OCR
text = pytesseract.image_to_string(blur, config='--psm 7 digits')
weight = ''.join(filter(lambda c: c in '0123456789.', text))
confidence = 95 # Replace with real confidence logic if needed
return weight.strip(), confidence
except Exception as e:
print(f"OCR error: {e}")
return "", 0
|