Spaces:
Running
Running
File size: 1,308 Bytes
8fe1b94 9e5703a a71f519 9e5703a 65ed4c1 363a646 65ed4c1 363a646 65ed4c1 fa507b4 363a646 fa507b4 9e5703a 712c074 9661246 9e5703a f823764 33069a9 9e5703a 33069a9 9e5703a 65ed4c1 9e5703a f617821 8fe1b94 65ed4c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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
|