Autoweight / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
ddf0c54 verified
raw
history blame
753 Bytes
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
img = pil_img.convert("RGB")
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Preprocess
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
# OCR
text = pytesseract.image_to_string(blur, config='--psm 7 digits')
weight = ''.join(filter(lambda c: c in '0123456789.', text))
confidence = 95 # Replace with real confidence logic if needed
return weight.strip(), confidence
except Exception as e:
print(f"OCR error: {e}")
return "", 0