File size: 1,407 Bytes
5d670ae
deda0a5
0ba4671
8e6ee74
da9f292
8e6ee74
deda0a5
b272a68
8e6ee74
 
 
 
 
da9f292
0ba4671
8e6ee74
 
 
 
 
 
005d086
8e6ee74
 
25728eb
8e6ee74
 
 
25728eb
8e6ee74
25728eb
0ba4671
8e6ee74
25728eb
 
0ba4671
8e6ee74
158e677
deda0a5
 
8e6ee74
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
import cv2
import easyocr
import numpy as np
import re

# Initialize EasyOCR reader
reader = easyocr.Reader(['en'], gpu=False)

def sharpen_image(img_cv):
    kernel = np.array([[0, -1, 0],
                       [-1, 5,-1],
                       [0, -1, 0]])
    return cv2.filter2D(img_cv, -1, kernel)

def extract_weight_from_image(pil_img):
    # Convert PIL to OpenCV
    img_cv = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)

    # Resize and sharpen
    img_cv = cv2.resize(img_cv, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
    img_cv = sharpen_image(img_cv)

    # Convert to grayscale
    img_gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)

    # Apply adaptive threshold and morphology
    img_thresh = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                       cv2.THRESH_BINARY, 11, 3)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
    img_clean = cv2.morphologyEx(img_thresh, cv2.MORPH_CLOSE, kernel)

    # OCR
    results = reader.readtext(img_clean)
    filtered = [text for _, text, conf in results if len(text.strip()) <= 5 and conf > 0.4]
    raw_text = " ".join(filtered)

    matches = re.findall(r"\d+\.\d+|\d+", raw_text)
    weight = matches[0] if matches else "0.0"
    confidence = int(results[0][2] * 100) if results else 0

    return weight, confidence, raw_text, img_cv  # Return processed image