File size: 1,982 Bytes
c28abeb
c11fb3a
8f557ef
 
c28abeb
 
8f557ef
adab0b4
c11fb3a
 
 
adab0b4
 
 
 
c11fb3a
adab0b4
 
 
 
 
 
 
cfcd3d5
c11fb3a
 
 
adab0b4
c11fb3a
c28abeb
adab0b4
c11fb3a
1362c73
adab0b4
c28abeb
adab0b4
 
c28abeb
adab0b4
c11fb3a
 
adab0b4
c28abeb
adab0b4
c28abeb
adab0b4
c11fb3a
 
cfcd3d5
c11fb3a
adab0b4
c11fb3a
cfcd3d5
adab0b4
 
c11fb3a
 
 
 
 
 
 
 
 
42b463a
393f381
adab0b4
c11fb3a
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import re
import cv2
from PIL import Image
import easyocr
import os

# βœ… Initialize OCR reader only once
reader = easyocr.Reader(['en'], gpu=False)

def preprocess_image(image):
    """
    Preprocess the image to improve OCR detection.
    Converts to grayscale and applies adaptive threshold.
    """
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    # Apply adaptive threshold to isolate digits better
    thresh = cv2.adaptiveThreshold(
        gray, 255,
        cv2.ADAPTIVE_THRESH_MEAN_C,
        cv2.THRESH_BINARY_INV,
        11, 10
    )
    return thresh

def extract_weight_from_image(pil_image):
    try:
        # βœ… Step 1: Convert to OpenCV format
        image = np.array(pil_image.convert("RGB"))

        # βœ… Step 2: Preprocess image
        processed = preprocess_image(image)

        # βœ… Step 3: Optional - Save debug image
        debug_path = "debug_processed_image.png"
        Image.fromarray(processed).save(debug_path)
        print(f"[DEBUG] Saved preprocessed image to {debug_path}")

        # βœ… Step 4: Run EasyOCR
        result = reader.readtext(processed)

        print("πŸ” OCR Results:")
        for r in result:
            print(f"  β€’ Text: '{r[1]}' | Confidence: {r[2]*100:.2f}%")

        # βœ… Step 5: Look for a decimal number like 53.25
        weight = None
        confidence = 0.0

        for detection in result:
            text = detection[1].replace(",", ".")  # Handle comma decimal (if any)
            conf = detection[2]

            # Look for numbers like 53.25 or 100
            match = re.search(r"\b\d{1,3}(\.\d{1,2})?\b", text)
            if match:
                weight = match.group()
                confidence = conf
                break

        if weight:
            return weight, round(confidence * 100, 2)
        else:
            return "No weight detected", 0.0

    except Exception as e:
        print(f"❌ OCR Error: {e}")
        return f"Error: {str(e)}", 0.0