import easyocr import re # Load OCR reader once reader = easyocr.Reader(['en']) def extract_weight_from_image(pil_image): results = reader.readtext(pil_image) weight = None confidence = 0.0 for (bbox, text, conf) in results: match = re.search(r'(\d+(\.\d+)?)\s?g', text.lower()) if match: weight = match.group(1) + " g" confidence = conf break if weight: return weight, confidence else: return "No weight detected", 0.0