Spaces:
Build error
Build error
import re | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
from paddleocr import PaddleOCR | |
# β Initialize PaddleOCR once | |
ocr = PaddleOCR(use_angle_cls=False, lang='en', show_log=False) | |
def preprocess_image(image): | |
""" | |
Convert to grayscale and enhance contrast to help OCR. | |
""" | |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
processed = cv2.adaptiveThreshold( | |
gray, 255, | |
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, | |
cv2.THRESH_BINARY_INV, | |
11, 2 | |
) | |
return processed | |
def extract_weight_from_image(pil_image): | |
try: | |
# β Convert to OpenCV format | |
image = np.array(pil_image.convert("RGB")) | |
# β Preprocess image | |
processed = preprocess_image(image) | |
# Optional: Save debug image | |
debug_path = "debug_processed_image.png" | |
Image.fromarray(processed).save(debug_path) | |
print(f"[DEBUG] Saved preprocessed image to {debug_path}") | |
# β Run OCR on original (RGB) image (not preprocessed) | |
result = ocr.ocr(image, cls=False) | |
print("π PaddleOCR Results:") | |
for line in result[0]: | |
text = line[1][0] | |
conf = line[1][1] | |
print(f" β’ Text: '{text}' | Confidence: {conf*100:.2f}%") | |
# Try to extract number like 53.25 or 100 | |
match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text) | |
if match: | |
return match.group(), round(conf * 100, 2) | |
return "No weight detected", 0.0 | |
except Exception as e: | |
print(f"β OCR Error: {e}") | |
return f"Error: {str(e)}", 0.0 | |