Autoweight / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
f47b893 verified
raw
history blame contribute delete
966 Bytes
try:
from paddleocr import PaddleOCR
import re
ocr = PaddleOCR(use_angle_cls=True, lang='en')
except Exception as e:
print(f"❌ PaddleOCR failed to load: {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})")
# Regex: number with optional kg/g
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:")
for t in debug_texts:
print(t)
return ("No weight detected", 0.0)