File size: 22,459 Bytes
975f9c6
 
 
 
5234a64
d373620
 
5234a64
9ac49a2
 
5234a64
9ac49a2
5234a64
 
d373620
 
 
 
 
 
 
 
 
 
 
 
9ac49a2
d373620
0f29b7c
c320b80
0f29b7c
9ac49a2
d373620
554a2ee
c320b80
a6294cd
 
 
c320b80
554a2ee
 
a6294cd
554a2ee
 
 
 
 
 
 
 
3137c41
 
a6294cd
3137c41
 
 
 
 
a6294cd
554a2ee
3137c41
 
a6294cd
554a2ee
3137c41
 
 
 
 
 
 
 
 
 
c320b80
9ac49a2
c320b80
5234a64
a6294cd
373b0d2
9ac49a2
554a2ee
 
 
a6294cd
554a2ee
 
 
 
 
 
 
 
a6294cd
554a2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ac49a2
373b0d2
 
554a2ee
a6294cd
554a2ee
a6294cd
 
554a2ee
 
373b0d2
 
 
554a2ee
373b0d2
 
 
a6294cd
373b0d2
 
 
9ac49a2
a6294cd
9ac49a2
5234a64
9ac49a2
a6294cd
9ac49a2
 
373b0d2
c320b80
9ac49a2
554a2ee
4c95d04
fcdea18
9ac49a2
a6294cd
 
554a2ee
a6294cd
 
 
 
9ac49a2
5234a64
9ac49a2
 
 
 
 
 
 
 
 
 
554a2ee
975f9c6
9ac49a2
 
 
 
 
 
 
 
 
 
 
 
4c95d04
9ac49a2
 
 
 
 
554a2ee
 
c320b80
 
 
9ac49a2
 
 
 
d373620
9ac49a2
c320b80
9ac49a2
554a2ee
9ac49a2
554a2ee
c320b80
a6294cd
c320b80
a6294cd
c320b80
 
a6294cd
d373620
373b0d2
d373620
554a2ee
 
 
753fcb8
554a2ee
9ac49a2
554a2ee
9ac49a2
753fcb8
9ac49a2
 
 
 
554a2ee
9ac49a2
 
 
753fcb8
9ac49a2
 
 
 
 
 
 
 
a6294cd
554a2ee
9ac49a2
 
373b0d2
c320b80
753fcb8
9ac49a2
 
753fcb8
9ac49a2
373b0d2
 
 
9ac49a2
373b0d2
9ac49a2
 
 
 
 
975f9c6
9ac49a2
c320b80
9ac49a2
 
 
c320b80
9ac49a2
3137c41
 
373b0d2
9ac49a2
554a2ee
3137c41
b613b80
 
373b0d2
a6294cd
9ac49a2
 
554a2ee
9ac49a2
c320b80
554a2ee
c320b80
 
 
 
9ac49a2
c320b80
9ac49a2
c320b80
554a2ee
 
3137c41
554a2ee
a6294cd
9ac49a2
373b0d2
554a2ee
 
 
 
 
 
 
 
a6294cd
554a2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b613b80
554a2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3137c41
554a2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b613b80
 
 
554a2ee
 
 
b613b80
 
 
 
 
 
 
 
 
 
 
554a2ee
 
 
 
b613b80
 
 
554a2ee
b613b80
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import easyocr
import numpy as np
import cv2
import re
import logging
from datetime import datetime
import os

# Set up logging for debugging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

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

# Directory for debug images
DEBUG_DIR = "debug_images"
os.makedirs(DEBUG_DIR, exist_ok=True)

def save_debug_image(img, filename_suffix, prefix=""):
    """Saves an image to the debug directory with a timestamp."""
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
    filename = os.path.join(DEBUG_DIR, f"{prefix}{timestamp}_{filename_suffix}.png")
    if len(img.shape) == 3:  # Color image
        cv2.imwrite(filename, img)
    else:  # Grayscale image
        cv2.imwrite(filename, img)
    logging.info(f"Saved debug image: {filename}")

def estimate_brightness(img):
    """Estimate image brightness to detect illuminated displays."""
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return np.mean(gray)

def preprocess_image(img, scale=1.0, method='clahe'):
    """Preprocess image for better OCR accuracy."""
    if scale != 1.0:
        img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
        save_debug_image(img, f"01_preprocess_scaled_{scale}")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Gentle denoising
    denoised = cv2.bilateralFilter(gray, 7, 10, 10)
    save_debug_image(denoised, "02_preprocess_bilateral")
    # Enhance contrast
    if method == 'clahe':
        clahe = cv2.createCLAHE(clipLimit=3.5, tileGridSize=(8, 8))
        enhanced = clahe.apply(denoised)
    else:  # Histogram equalization
        enhanced = cv2.equalizeHist(denoised)
    save_debug_image(enhanced, f"03_preprocess_{method}")
    # Sharpen
    kernel_sharpening = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
    sharpened = cv2.filter2D(enhanced, -1, kernel_sharpening)
    save_debug_image(sharpened, "04_preprocess_sharpened")
    return sharpened

def correct_rotation(img):
    """Correct image rotation using Hough Transform."""
    try:
        edges = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 50, 150)
        lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=50, minLineLength=40, maxLineGap=10)
        if lines is not None:
            angles = [np.arctan2(line[0][3] - line[0][1], line[0][2] - line[0][0]) * 180 / np.pi for line in lines]
            angle = np.median(angles)
            if abs(angle) > 2:
                (h, w) = img.shape[:2]
                center = (w // 2, h // 2)
                M = cv2.getRotationMatrix2D(center, angle, 1.0)
                img = cv2.warpAffine(img, M, (w, h))
                save_debug_image(img, "00_rotated_image")
                logging.info(f"Applied rotation correction: {angle:.2f} degrees")
        return img
    except Exception as e:
        logging.error(f"Rotation correction failed: {str(e)}")
        return img

def detect_roi(img):
    """Detect and crop the region of interest (likely the digital display)."""
    try:
        save_debug_image(img, "05_original")
        brightness_map = cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (15, 15), 0)
        
        # Try multiple scales and methods
        scales = [1.0, 1.5, 0.5]
        methods = ['clahe', 'hist']
        for scale in scales:
            for method in methods:
                preprocessed = preprocess_image(img, scale, method)
                block_size = max(9, min(31, int(img.shape[0] / 25) * 2 + 1))
                thresh = cv2.adaptiveThreshold(preprocessed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
                                               cv2.THRESH_BINARY_INV, block_size, 3)
                _, otsu_thresh = cv2.threshold(preprocessed, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
                combined_thresh = cv2.bitwise_and(thresh, otsu_thresh)
                save_debug_image(combined_thresh, f"06_roi_combined_threshold_scale_{scale}_{method}")
                
                # Morphological operations
                kernel = np.ones((3, 3), np.uint8)
                dilated = cv2.dilate(combined_thresh, kernel, iterations=2)
                eroded = cv2.erode(dilated, kernel, iterations=1)
                save_debug_image(eroded, f"07_roi_morphological_scale_{scale}_{method}")
                
                contours, _ = cv2.findContours(eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
                
                if contours:
                    img_area = img.shape[0] * img.shape[1]
                    valid_contours = []
                    for c in contours:
                        area = cv2.contourArea(c)
                        x, y, w, h = cv2.boundingRect(c)
                        roi_brightness = np.mean(brightness_map[y:y+h, x:x+w] if scale == 1.0 else cv2.resize(brightness_map, (img.shape[1], img.shape[0])))
                        aspect_ratio = w / h
                        if (100 < area < (img_area * 0.95) and 
                            0.3 <= aspect_ratio <= 20.0 and w > 40 and h > 15 and roi_brightness > 50):
                            valid_contours.append((c, roi_brightness))
                            logging.debug(f"Contour: Scale={scale}, Method={method}, Area={area}, Aspect={aspect_ratio:.2f}, Brightness={roi_brightness:.2f}")
                    
                    if valid_contours:
                        contour, _ = max(valid_contours, key=lambda x: x[1])
                        x, y, w, h = cv2.boundingRect(contour)
                        if scale != 1.0:
                            x, y, w, h = [int(v / scale) for v in (x, y, w, h)]
                        padding = 150
                        x, y = max(0, x - padding), max(0, y - padding)
                        w, h = min(w + 2 * padding, img.shape[1] - x), min(h + 2 * padding, img.shape[0] - y)
                        roi_img = img[y:y+h, x:x+w]
                        save_debug_image(roi_img, f"08_detected_roi_scale_{scale}_{method}")
                        logging.info(f"Detected ROI with dimensions: ({x}, {y}, {w}, {h}) at scale {scale}, method {method}")
                        return roi_img, (x, y, w, h)
        
        logging.info("No suitable ROI found, attempting fallback criteria.")
        # Fallback with relaxed criteria
        preprocessed = preprocess_image(img, method='clahe')
        thresh = cv2.adaptiveThreshold(preprocessed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
                                       cv2.THRESH_BINARY_INV, block_size, 5)
        save_debug_image(thresh, "06_roi_fallback_threshold")
        contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        valid_contours = [c for c in contours if 50 < cv2.contourArea(c) < (img.shape[0] * img.shape[1] * 0.95) and 
                          0.2 <= cv2.boundingRect(c)[2]/cv2.boundingRect(c)[3] <= 25.0]
        if valid_contours:
            contour = max(valid_contours, key=cv2.contourArea)
            x, y, w, h = cv2.boundingRect(contour)
            padding = 150
            x, y = max(0, x - padding), max(0, y - padding)
            w, h = min(w + 2 * padding, img.shape[1] - x), min(h + 2 * padding, img.shape[0] - y)
            roi_img = img[y:y+h, x:x+w]
            save_debug_image(roi_img, "08_detected_roi_fallback")
            logging.info(f"Detected fallback ROI with dimensions: ({x}, {y}, {w}, {h})")
            return roi_img, (x, y, w, h)
        
        logging.info("No suitable ROI found, returning original image.")
        save_debug_image(img, "08_no_roi_original_fallback")
        return img, None
    except Exception as e:
        logging.error(f"ROI detection failed: {str(e)}")
        save_debug_image(img, "08_roi_detection_error_fallback")
        return img, None

def detect_segments(digit_img, brightness):
    """Detect seven-segment patterns in a digit image."""
    h, w = digit_img.shape
    if h < 8 or w < 6:
        return None

    segments = {
        'top': (int(w*0.1), int(w*0.9), 0, int(h*0.25)),
        'middle': (int(w*0.1), int(w*0.9), int(h*0.45), int(h*0.55)),
        'bottom': (int(w*0.1), int(w*0.9), int(h*0.75), h),
        'left_top': (0, int(w*0.3), int(h*0.1), int(h*0.5)),
        'left_bottom': (0, int(w*0.3), int(h*0.5), int(h*0.9)),
        'right_top': (int(w*0.7), w, int(h*0.1), int(h*0.5)),
        'right_bottom': (int(w*0.7), w, int(h*0.5), int(h*0.9))
    }

    segment_presence = {}
    for name, (x1, x2, y1, y2) in segments.items():
        x1, y1 = max(0, x1), max(0, y1)
        x2, y2 = min(w, x2), min(h, y2)
        region = digit_img[y1:y2, x1:x2]
        if region.size == 0:
            segment_presence[name] = False
            continue
        pixel_count = np.sum(region == 255)
        total_pixels = region.size
        segment_presence[name] = pixel_count / total_pixels > (0.15 if brightness < 80 else 0.35)

    digit_patterns = {
        '0': ('top', 'bottom', 'left_top', 'left_bottom', 'right_top', 'right_bottom'),
        '1': ('right_top', 'right_bottom'),
        '2': ('top', 'middle', 'bottom', 'left_bottom', 'right_top'),
        '3': ('top', 'middle', 'bottom', 'right_top', 'right_bottom'),
        '4': ('middle', 'left_top', 'right_top', 'right_bottom'),
        '5': ('top', 'middle', 'bottom', 'left_top', 'right_bottom'),
        '6': ('top', 'middle', 'bottom', 'left_top', 'left_bottom', 'right_bottom'),
        '7': ('top', 'right_top', 'right_bottom'),
        '8': ('top', 'middle', 'bottom', 'left_top', 'left_bottom', 'right_top', 'right_bottom'),
        '9': ('top', 'middle', 'bottom', 'left_top', 'right_top', 'right_bottom')
    }

    best_match = None
    max_score = -1
    for digit, pattern in digit_patterns.items():
        matches = sum(1 for segment in pattern if segment_presence.get(segment, False))
        non_matches_penalty = sum(1 for segment in segment_presence if segment not in pattern and segment_presence[segment])
        score = matches - 0.15 * non_matches_penalty
        if matches >= len(pattern) * 0.65:
            score += 1.0
        if score > max_score:
            max_score = score
            best_match = digit
    
    logging.debug(f"Segment presence: {segment_presence}, Detected digit: {best_match}")
    return best_match

def custom_seven_segment_ocr(img, roi_bbox):
    """Perform custom OCR for seven-segment displays."""
    try:
        preprocessed = preprocess_image(img, method='clahe')
        brightness = estimate_brightness(img)
        thresh_value = 60 if brightness < 80 else 0
        _, thresh = cv2.threshold(preprocessed, thresh_value, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
        save_debug_image(thresh, "09_roi_thresh_for_digits")

        # Morphological operations
        kernel = np.ones((3, 3), np.uint8)
        thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
        save_debug_image(thresh, "10_morph_closed")

        batch_size = max(4, min(16, int(img.shape[0] * img.shape[1] / 100000)))
        results = easyocr_reader.readtext(thresh, detail=1, paragraph=False, 
                                          contrast_ths=0.1, adjust_contrast=1.3, 
                                          text_threshold=0.3, mag_ratio=6.0, 
                                          allowlist='0123456789.', batch_size=batch_size, y_ths=0.4)
        
        logging.info(f"EasyOCR results (seven-segment): {results}")
        if not results:
            logging.info("EasyOCR found no digits in seven-segment OCR.")
            return None

        digits_info = []
        for (bbox, text, conf) in results:
            (x1, y1), (x2, y2), (x3, y3), (x4, y4) = bbox
            h_bbox = max(y1, y2, y3, y4) - min(y1, y2, y3, y4)
            if (text.isdigit() or text == '.') and h_bbox > 5:
                x_min, x_max = int(min(x1, x4)), int(max(x2, x3))
                y_min, y_max = int(min(y1, y2)), int(max(y3, y4))
                digits_info.append((x_min, x_max, y_min, y_max, text, conf))

        digits_info.sort(key=lambda x: x[0])
        recognized_text = ""
        for idx, (x_min, x_max, y_min, y_max, easyocr_char, easyocr_conf) in enumerate(digits_info):
            x_min, y_min = max(0, x_min), max(0, y_min)
            x_max, y_max = min(thresh.shape[1], x_max), min(thresh.shape[0], y_max)
            if x_max <= x_min or y_max <= y_min:
                continue
            digit_img_crop = thresh[y_min:y_max, x_min:x_max]
            save_debug_image(digit_img_crop, f"11_digit_crop_{idx}_{easyocr_char}")
            if easyocr_conf > 0.85 or easyocr_char == '.':
                recognized_text += easyocr_char
            else:
                digit_from_segments = detect_segments(digit_img_crop, brightness)
                recognized_text += digit_from_segments if digit_from_segments else easyocr_char
        
        logging.info(f"Before validation, recognized_text: {recognized_text}")
        text = re.sub(r"[^\d\.]", "", recognized_text)
        if text.count('.') > 1:
            text = text.replace('.', '', text.count('.') - 1)
        if text and re.fullmatch(r"^\d*\.?\d*$", text):
            text = text.strip('.')
            if text == '':
                return None
            return text.lstrip('0') or '0'
        logging.info(f"Custom OCR text '{recognized_text}' failed validation.")
        return None
    except Exception as e:
        logging.error(f"Custom seven-segment OCR failed: {str(e)}")
        return None

def extract_weight_from_image(pil_img):
    """Extract weight from a PIL image of a digital scale display."""
    try:
        img = np.array(pil_img)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        save_debug_image(img, "00_input_image")

        # Apply rotation correction
        img = correct_rotation(img)

        brightness = estimate_brightness(img)
        conf_threshold = 0.65 if brightness > 150 else (0.45 if brightness > 80 else 0.25)

        roi_img, roi_bbox = detect_roi(img)
        if roi_bbox:
            roi_area = roi_bbox[2] * roi_bbox[3]
            conf_threshold *= 1.1 if roi_area > (img.shape[0] * img.shape[1] * 0.5) else 1.0

        custom_result = custom_seven_segment_ocr(roi_img, roi_bbox)
        if custom_result and custom_result != '0':
            try:
                weight = float(custom_result)
                if 0.0001 <= weight <= 5000:
                    logging.info(f"Custom OCR result: {custom_result}, Confidence: 95.0%")
                    return custom_result, 95.0
                else:
                    logging.warning(f"Custom OCR result {custom_result} outside typical weight range.")
            except ValueError:
                logging.warning(f"Custom OCR result '{custom_result}' is not a valid number.")

        logging.info("Custom OCR failed or invalid, falling back to enhanced EasyOCR.")
        preprocessed_roi = preprocess_image(roi_img, method='hist')
        block_size = max(9, min(31, int(roi_img.shape[0] / 25) * 2 + 1))
        final_roi = cv2.adaptiveThreshold(preprocessed_roi, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
                                          cv2.THRESH_BINARY_INV, block_size, 5)
        save_debug_image(final_roi, "12_fallback_adaptive_thresh")

        batch_size = max(4, min(16, int(roi_img.shape[0] * roi_img.shape[1] / 100000)))
        ocr_passes = [
            {'contrast_ths': 0.2, 'text_threshold': 0.3, 'mag_ratio': 6.0, 'y_ths': 0.4, 'label': 'first'},
            {'contrast_ths': 0.1, 'text_threshold': 0.2, 'mag_ratio': 7.0, 'y_ths': 0.5, 'label': 'second'},
            {'contrast_ths': 0.05, 'text_threshold': 0.1, 'mag_ratio': 8.0, 'y_ths': 0.6, 'label': 'third'}
        ]
        candidates = []

        for ocr_pass in ocr_passes:
            results = easyocr_reader.readtext(final_roi, detail=1, paragraph=False, 
                                              contrast_ths=ocr_pass['contrast_ths'], 
                                              adjust_contrast=1.4, 
                                              text_threshold=ocr_pass['text_threshold'], 
                                              mag_ratio=ocr_pass['mag_ratio'], 
                                              allowlist='0123456789. kglb', 
                                              batch_size=batch_size, 
                                              y_ths=ocr_pass['y_ths'])
            logging.info(f"EasyOCR results ({ocr_pass['label']} pass): {results}")
            save_debug_image(final_roi, f"12_fallback_adaptive_thresh_{ocr_pass['label']}_pass")
            
            unit = None
            for (bbox, text, conf) in results:
                if 'kg' in text.lower():
                    unit = 'kg'
                    continue
                elif 'g' in text.lower():
                    unit = 'g'
                    continue
                elif 'lb' in text.lower():
                    unit = 'lb'
                    continue
                text = re.sub(r"[^\d\.]", "", text)
                if text.count('.') > 1:
                    text = text.replace('.', '', text.count('.') - 1)
                text = text.strip('.')
                if re.fullmatch(r"^\d*\.?\d*$", text):
                    try:
                        weight = float(text)
                        if unit == 'g':
                            weight /= 1000
                        elif unit == 'lb':
                            weight *= 0.453592
                        range_score = 1.5 if 0.0001 <= weight <= 5000 else 0.6
                        digit_count = len(text.replace('.', ''))
                        digit_score = 1.4 if 1 <= digit_count <= 8 else 0.7
                        score = conf * range_score * digit_score
                        if roi_bbox:
                            (x_roi, y_roi, w_roi, h_roi) = roi_bbox
                            roi_area = w_roi * h_roi
                            x_min, y_min = int(min(b[0] for b in bbox)), int(min(b[1] for b in bbox))
                            x_max, y_max = int(max(b[0] for b in bbox)), int(max(b[1] for b in bbox))
                            bbox_area = (x_max - x_min) * (y_max - y_min)
                            if roi_area > 0 and bbox_area / roi_area < 0.02:
                                score *= 0.4
                        candidates.append((text, conf, score, unit))
                        logging.info(f"Candidate EasyOCR weight: '{text}', Unit: {unit or 'none'}, Conf: {conf}, Score: {score}")
                    except ValueError:
                        logging.warning(f"Could not convert '{text}' to float during EasyOCR fallback.")

        # Fallback to full image if no candidates
        if not candidates:
            logging.info("No candidates from ROI, trying full image.")
            preprocessed_full = preprocess_image(img, method='hist')
            final_full = cv2.adaptiveThreshold(preprocessed_full, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
                                               cv2.THRESH_BINARY_INV, block_size, 5)
            save_debug_image(final_full, "12_fallback_full_image")
            results = easyocr_reader.readtext(final_full, detail=1, paragraph=False, 
                                              contrast_ths=0.1, adjust_contrast=1.5, 
                                              text_threshold=0.2, mag_ratio=7.0, 
                                              allowlist='0123456789. kglb', batch_size=batch_size, y_ths=0.5)
            logging.info(f"EasyOCR results (full image): {results}")
            
            unit = None
            for (bbox, text, conf) in results:
                if 'kg' in text.lower():
                    unit = 'kg'
                    continue
                elif 'g' in text.lower():
                    unit = 'g'
                    continue
                elif 'lb' in text.lower():
                    unit = 'lb'
                    continue
                text = re.sub(r"[^\d\.]", "", text)
                if text.count('.') > 1:
                    text = text.replace('.', '', text.count('.') - 1)
                text = text.strip('.')
                if re.fullmatch(r"^\d*\.?\d*$", text):
                    try:
                        weight = float(text)
                        if unit == 'g':
                            weight /= 1000
                        elif unit == 'lb':
                            weight *= 0.453592
                        range_score = 1.2 if 0.0001 <= weight <= 5000 else 0.5
                        digit_count = len(text.replace('.', ''))
                        digit_score = 1.2 if 1 <= digit_count <= 8 else 0.6
                        score = conf * range_score * digit_score * 0.8  # Penalty for full image
                        candidates.append((text, conf, score, unit))
                        logging.info(f"Candidate EasyOCR weight (full image): '{text}', Unit: {unit or 'none'}, Conf: {conf}, Score: {score}")
                    except ValueError:
                        logging.warning(f"Could not convert '{text}' to float during full image fallback.")

        if not candidates:
            logging.info("No valid weight detected after all attempts.")
            return "Not detected", 0.0

        # Select best candidate
        best_weight, best_conf, best_score, best_unit = max(candidates, key=lambda x: x[2])
        
        # Format the weight
        if "." in best_weight:
            int_part, dec_part = best_weight.split(".")
            int_part = int_part.lstrip("0") or "0"
            dec_part = dec_part.rstrip('0')
            best_weight = f"{int_part}.{dec_part}" if dec_part else int_part
        else:
            best_weight = best_weight.lstrip('0') or "0"

        try:
            final_weight = float(best_weight)
            if final_weight < 0.0001 or final_weight > 5000:
                best_conf *= 0.5
            elif final_weight == 0 and best_conf < 0.95:
                best_conf *= 0.6  # Penalize zero weights
        except ValueError:
            pass

        logging.info(f"Final detected weight: {best_weight} kg, Confidence: {round(best_conf * 100, 2)}%, Unit: {best_unit or 'none'}")
        return best_weight, round(best_conf * 100, 2)

    except Exception as e:
        logging.error(f"Weight extraction failed unexpectedly: {str(e)}")
        return "Not detected", 0.0