Autoweight / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
a8c66ae verified
raw
history blame
952 Bytes
try:
from paddleocr import PaddleOCR
import re
ocr = PaddleOCR(use_angle_cls=True, lang='en')
except Exception as e:
import streamlit as st
st.error(f"❌ OCR Engine Load Failed: {e}")
ocr = None
def extract_weight_from_image(image):
if ocr is None:
return ("OCR not initialized", 0.0)
result = ocr.ocr(image, cls=True)
debug_texts = []
if not result or not result[0]:
return ("No weight detected", 0.0)
for line in result[0]:
text, confidence = line[1][0], line[1][1]
debug_texts.append(f"{text} (Conf: {confidence:.2f})")
match = re.search(r'(\d+\.?\d*)\s*(kg|g)?', text.lower())
if match:
weight = match.group(1)
unit = match.group(2) if match.group(2) else "g"
return (f"{weight} {unit}", confidence)
print("OCR Debug Output:")
for t in debug_texts:
print(t)
return ("No weight detected", 0.0)