File size: 404 Bytes
3165179
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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"