from paddleocr import PaddleOCR import re # Initialize PaddleOCR once ocr = PaddleOCR(use_angle_cls=True, lang='en') def extract_weight_from_image(image): result = ocr.ocr(image, cls=True) debug_texts = [] if not result or not result[0]: return ("No weight detected", 0.0) for line in result[0]: text, confidence = line[1][0], line[1][1] debug_texts.append(f"{text} (Conf: {confidence:.2f})") # Find number with optional decimal + optional "kg", "g" match = re.search(r'(\d+\.?\d*)\s*(kg|g)?', text.lower()) if match: unit = match.group(2) if match.group(2) else "g" weight = match.group(1) return (f"{weight} {unit}", confidence) print("DEBUG OCR OUTPUT:") for d in debug_texts: print(d) return ("No weight detected", 0.0)