File size: 952 Bytes
a8c66ae
 
 
 
 
 
 
 
1362c73
34234c5
a8c66ae
 
 
34234c5
 
c28abeb
34234c5
 
c11fb3a
34234c5
 
 
 
 
 
a8c66ae
34234c5
c11fb3a
a8c66ae
 
 
42b463a
34234c5
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
26
27
28
29
30
31
32
33
34
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)