logger1 / ocr_engine.py
Sanjayraju30's picture
Rename weight_detector.py to ocr_engine.py
3165179 verified
raw
history blame
404 Bytes
import cv2
import pytesseract
from PIL import Image
import numpy as np
def extract_weight(image: Image.Image) -> str:
img = np.array(image.convert("RGB"))
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
text = pytesseract.image_to_string(gray, config="--psm 7 digits")
weight = ''.join(filter(lambda x: x in '0123456789.', text))
return weight if weight else "No valid weight detected"