import cv2 import pytesseract from PIL import Image import re def extract_weight(img_path): img = cv2.imread(img_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # OCR text = pytesseract.image_to_string(gray, config='--psm 6') # Clean and lower text text = text.lower().replace('\n', ' ').strip() # Regex to find pattern like 52.25 g or 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"