Spaces:
Build error
Build error
File size: 1,238 Bytes
7e1096c 1362c73 7e1096c a8c66ae 7e1096c c11fb3a 7e1096c c11fb3a 7e1096c 07d4fed 7e1096c |
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 pytesseract
import numpy as np
from PIL import Image
def extract_weight_from_image(pil_img):
try:
# Convert PIL image to OpenCV format
img = pil_img.convert("RGB")
img_np = np.array(img)
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
# Convert to grayscale
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
# Adaptive Thresholding for 7-segment LCD
processed = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 15, 10
)
# Resize to enhance small text
resized = cv2.resize(processed, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
# OCR config tuned for digit blocks
config = r'--oem 3 --psm 7 -c tessedit_char_whitelist=0123456789.'
# Run OCR
text = pytesseract.image_to_string(resized, config=config)
print("🔍 RAW OCR OUTPUT:", repr(text))
# Clean the text
weight = ''.join(c for c in text if c in '0123456789.')
weight = weight.strip()
confidence = 95 if weight else 0
return weight, confidence
except Exception as e:
print("❌ OCR Error:", str(e))
return "", 0
|