Spaces:
Sleeping
Sleeping
| import cv2 | |
| import numpy as np | |
| import re | |
| from PIL import Image | |
| def extract_weight_from_image(pil_img): | |
| try: | |
| img = np.array(pil_img) | |
| # Convert to grayscale | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| # Threshold image | |
| _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) | |
| # Invert if needed | |
| if np.mean(thresh > 127) < 0.5: | |
| thresh = cv2.bitwise_not(thresh) | |
| # Resize to make digits bigger | |
| scale_factor = 4 | |
| resized = cv2.resize(thresh, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR) | |
| # OCR-style region crop: focus on left part of display | |
| height, width = resized.shape | |
| digit_region = resized[0:height, 0:int(width * 0.7)] # ignore 'kg' | |
| # Use pytesseract as fallback OCR for just digits | |
| import pytesseract | |
| config = "--psm 7 -c tessedit_char_whitelist=0123456789." | |
| result = pytesseract.image_to_string(digit_region, config=config) | |
| print("Raw OCR:", result) | |
| match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)", result) | |
| if match: | |
| return f"{match.group()} kg", 100.0 | |
| else: | |
| return "No weight detected kg", 0.0 | |
| except Exception as e: | |
| return f"Error: {str(e)}", 0.0 | |