auto-weight-logger4 / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
6a65c1f verified
raw
history blame
684 Bytes
from mmocr.utils.ocr import MMOCR
# Load MMOCR model
mmocr = MMOCR(det='DB_r18', recog='CRNN', det_config=None, recog_config=None)
def extract_weight_from_image(image):
result = mmocr.readtext(image, output=None)
texts = [item['text'] for item in result]
debug_text = "\n".join(texts)
# Find the first valid float that could represent a weight
for text in texts:
if any(char.isdigit() for char in text):
cleaned = text.replace('kg', '').replace('KG', '').strip()
try:
weight = float(cleaned)
return str(weight), debug_text
except:
continue
return None, debug_text