Spaces:
Build error
Build error
File size: 1,165 Bytes
8f557ef 42b463a 393f381 1362c73 42b463a 1362c73 42b463a 1362c73 42b463a 1362c73 42b463a 1362c73 393f381 1362c73 393f381 1362c73 393f381 |
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 |
import cv2
import pytesseract
import numpy as np
from PIL import Image
def extract_weight_from_image(pil_img):
try:
# Convert to OpenCV image
img = pil_img.convert("RGB")
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Crop or resize if needed (optional based on display layout)
# Convert to grayscale and enhance contrast
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
# Optional: Resize for better OCR (helps with small digits)
resized = cv2.resize(thresh, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
# Apply OCR with proper config for digits
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
text = pytesseract.image_to_string(resized, config=custom_config)
# Filter out non-numeric parts
weight = ''.join(filter(lambda c: c in '0123456789.', text))
confidence = 95 if weight else 0
return weight.strip(), confidence
except Exception as e:
print("OCR Exception:", str(e))
return "", 0
|