from PIL import Image import pytesseract import re def extract_weight(img_path): img = Image.open(img_path).convert("L") # Grayscale # OCR text = pytesseract.image_to_string(img, config='--psm 6') text = text.lower().replace('\n', ' ').strip() # Find weight + unit (e.g., 52.25 g, 75.8 kg) match = re.search(r'(\d+\.\d+|\d+)\s*(kg|g)', text) if match: number = match.group(1) unit = match.group(2) return f"{number} {unit}" else: return "Weight not detected"