AutoWeightLogger1 / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
373b0d2 verified
raw
history blame
13.9 kB
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):
"""Preprocess image for better OCR accuracy."""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply bilateral filter to preserve edges
denoised = cv2.bilateralFilter(gray, 11, 17, 17)
save_debug_image(denoised, "01_preprocess_bilateral")
# Enhance contrast using CLAHE
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
enhanced = clahe.apply(denoised)
save_debug_image(enhanced, "02_preprocess_clahe")
return enhanced
def detect_roi(img):
"""Detect and crop the region of interest (likely the digital display)."""
try:
save_debug_image(img, "03_original")
preprocessed = preprocess_image(img)
brightness_map = cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (15, 15), 0)
# Dynamic adaptive thresholding
block_size = max(11, min(31, int(img.shape[0] / 20) * 2 + 1))
thresh = cv2.adaptiveThreshold(preprocessed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, block_size, 5)
_, 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, "04_roi_combined_threshold")
# Morphological operations to connect digits
kernel = np.ones((5, 5), np.uint8)
dilated = cv2.dilate(combined_thresh, kernel, iterations=2)
eroded = cv2.erode(dilated, kernel, iterations=1)
save_debug_image(eroded, "05_roi_morphological")
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])
aspect_ratio = w / h
if (1000 < area < (img_area * 0.9) and
1.0 <= aspect_ratio <= 10.0 and w > 80 and h > 40 and roi_brightness > 100):
valid_contours.append((c, roi_brightness))
logging.debug(f"Contour: Area={area}, Aspect={aspect_ratio:.2f}, Brightness={roi_brightness:.2f}")
if valid_contours:
contour, _ = max(valid_contours, key=lambda x: x[1]) # Max brightness
x, y, w, h = cv2.boundingRect(contour)
padding = 80
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, "06_detected_roi")
logging.info(f"Detected ROI with dimensions: ({x}, {y}, {w}, {h})")
return roi_img, (x, y, w, h)
logging.info("No suitable ROI found, attempting fallback criteria.")
# Fallback with relaxed criteria
valid_contours = [c for c in contours if 500 < cv2.contourArea(c) < (img_area * 0.95) and
0.8 <= cv2.boundingRect(c)[2]/cv2.boundingRect(c)[3] <= 12.0]
if valid_contours:
contour = max(valid_contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(contour)
padding = 80
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, "06_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, "06_no_roi_original_fallback")
return img, None
except Exception as e:
logging.error(f"ROI detection failed: {str(e)}")
save_debug_image(img, "06_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 < 20 or w < 15:
return None
segments = {
'top': (int(w*0.15), int(w*0.85), 0, int(h*0.2)),
'middle': (int(w*0.15), int(w*0.85), int(h*0.45), int(h*0.55)),
'bottom': (int(w*0.15), int(w*0.85), int(h*0.8), h),
'left_top': (0, int(w*0.25), int(h*0.15), int(h*0.5)),
'left_bottom': (0, int(w*0.25), int(h*0.5), int(h*0.85)),
'right_top': (int(w*0.75), w, int(h*0.15), int(h*0.5)),
'right_bottom': (int(w*0.75), w, int(h*0.5), int(h*0.85))
}
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.3 if brightness < 100 else 0.5)
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.3 * non_matches_penalty
if matches >= len(pattern) * 0.8:
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)
brightness = estimate_brightness(img)
thresh_value = 100 if brightness < 100 else 0
_, thresh = cv2.threshold(preprocessed, thresh_value, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
save_debug_image(thresh, "07_roi_thresh_for_digits")
# Morphological operations to enhance digit segments
kernel = np.ones((3, 3), np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
save_debug_image(thresh, "08_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.3, adjust_contrast=1.0,
text_threshold=0.6, mag_ratio=3.0,
allowlist='0123456789.', batch_size=batch_size, y_ths=0.2)
logging.info(f"EasyOCR results: {results}")
if not results:
logging.info("EasyOCR found no digits.")
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 len(text) == 1 and (text.isdigit() or text == '.') and h_bbox > 10:
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"09_digit_crop_{idx}_{easyocr_char}")
if easyocr_conf > 0.95 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")
# Rotation correction
edges = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 100, 200)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10)
if lines is not None:
angle = np.mean([np.arctan2(line[0][3] - line[0][1], line[0][2] - line[0][0]) * 180 / np.pi for line in lines])
if abs(angle) > 5:
(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")
brightness = estimate_brightness(img)
conf_threshold = 0.7 if brightness > 150 else (0.6 if brightness > 80 else 0.4)
if roi_bbox := detect_roi(img)[1]:
roi_area = roi_bbox[2] * roi_bbox[3]
conf_threshold *= 1.2 if roi_area > (img.shape[0] * img.shape[1] * 0.5) else 1.0
roi_img, roi_bbox = detect_roi(img)
custom_result = custom_seven_segment_ocr(roi_img, roi_bbox)
if custom_result:
try:
weight = float(custom_result)
if 0.01 <= weight <= 500:
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)
kernel_sharpening = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
sharpened_roi = cv2.filter2D(preprocessed_roi, -1, kernel_sharpening)
save_debug_image(sharpened_roi, "10_fallback_sharpened")
block_size = max(11, min(31, int(roi_img.shape[0] / 20) * 2 + 1))
final_roi = cv2.adaptiveThreshold(sharpened_roi, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, block_size, 8)
save_debug_image(final_roi, "11_fallback_adaptive_thresh")
batch_size = max(4, min(16, int(roi_img.shape[0] * roi_img.shape[1] / 100000)))
results = easyocr_reader.readtext(final_roi, detail=1, paragraph=False,
contrast_ths=0.4, adjust_contrast=1.2,
text_threshold=0.5, mag_ratio=4.0,
allowlist='0123456789. kglb', batch_size=batch Horrible error: invalid syntax