Spaces:
Running
Running
File size: 1,183 Bytes
fa507b4 65ed4c1 8fe1b94 a71f519 fa507b4 65ed4c1 363a646 65ed4c1 363a646 65ed4c1 fa507b4 363a646 fa507b4 712c074 f823764 fa507b4 712c074 fa507b4 712c074 f823764 33069a9 fa507b4 33069a9 712c074 fa507b4 65ed4c1 fa507b4 f823764 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 |
import pytesseract
import numpy as np
import cv2
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)
# Resize and enhance
gray = cv2.resize(gray, None, fx=4, fy=4, interpolation=cv2.INTER_LINEAR)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
gray = cv2.equalizeHist(gray)
# Threshold
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Invert if dark background
if np.mean(thresh > 127) < 0.5:
thresh = cv2.bitwise_not(thresh)
# OCR using Tesseract
config = "--psm 6 -c tessedit_char_whitelist=0123456789."
text = pytesseract.image_to_string(thresh, config=config)
print("🔍 OCR Text:", text)
# Extract weight
match = re.search(r"\d{1,3}(?:\.\d{1,2})?", text)
if match:
weight = match.group()
return f"{weight} kg", 100.0
else:
return "No weight detected kg", 0.0
except Exception as e:
return f"Error: {str(e)}", 0.0
|