Spaces:
Running
Running
File size: 597 Bytes
81b527b 2f21856 17d218a 65ef5f8 81b527b 65ef5f8 81b527b 17d218a 81b527b |
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 |
import cv2
import pytesseract
from PIL import Image
import re
def extract_weight(img_path):
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# OCR
text = pytesseract.image_to_string(gray, config='--psm 6')
# Clean and lower text
text = text.lower().replace('\n', ' ').strip()
# Regex to find pattern like 52.25 g or 75.8 kg
match = re.search(r'(\d+\.\d+|\d+)\s*(kg|g)', text)
if match:
number = match.group(1)
unit = match.group(2)
return f"{number} {unit}"
else:
return "Weight not detected"
|