Spaces:
Build error
Build error
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 | |