Spaces:
Runtime error
Runtime error
import numpy as np | |
import re | |
import cv2 | |
import pytesseract | |
def extract_weight_from_image(pil_img): | |
try: | |
img = np.array(pil_img) | |
# Convert to grayscale | |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
# Resize (sharpens small digits) | |
gray = cv2.resize(gray, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC) | |
# Thresholding to clean up image | |
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | |
# Run Tesseract OCR | |
custom_config = r'--oem 3 --psm 6' | |
text = pytesseract.image_to_string(thresh, config=custom_config) | |
print("OCR Text:", text) | |
# Extract weight pattern like 25.50 or 150 | |
match = re.search(r"\b\d{1,4}\.?\d{0,2}\b", text) | |
if match: | |
return match.group(), 95.0 | |
else: | |
return "No weight detected", 0.0 | |
except Exception as e: | |
return f"Error: {str(e)}", 0.0 | |