debug: add detailed logging to image processing pipeline

- Log input image size, crop bounds, and rotation degrees
- Log crop rectangle coordinates and result size
- Log OpenCV smart crop success/failure details
- Track pixel count to detect zero-size crops
This commit is contained in:
2026-04-22 09:23:51 +03:00
parent ba581744ec
commit a2847092ac

View File

@@ -81,6 +81,7 @@ class ImageProcessor:
# Open image with PIL
image = Image.open(io.BytesIO(file_bytes))
original_size = image.size
self.logger.info(f"[PROCESS] Input: {len(file_bytes)} bytes, size={original_size}, crop_bounds={crop_bounds}, rotation={rotation_degrees}°")
# Extract and apply EXIF orientation
exif_orientation = self._extract_exif_orientation(image)
@@ -96,25 +97,26 @@ class ImageProcessor:
if crop_bounds:
# Manual crop bounds provided
cropped_image = image.crop(
(
crop_bounds['x'],
crop_bounds['y'],
crop_bounds['x'] + crop_bounds['width'],
crop_bounds['y'] + crop_bounds['height'],
)
crop_rect = (
crop_bounds['x'],
crop_bounds['y'],
crop_bounds['x'] + crop_bounds['width'],
crop_bounds['y'] + crop_bounds['height'],
)
self.logger.info(f"[CROP] Manual bounds: {crop_rect}")
cropped_image = image.crop(crop_rect)
crop_size = cropped_image.size
crop_method = 'manual'
self.logger.info(f"Applied manual crop: {crop_size}")
self.logger.info(f"[CROP] Result size: {crop_size}, pixels={crop_size[0]*crop_size[1]}")
else:
# Try OpenCV smart crop
self.logger.info("[CROP] Attempting OpenCV smart crop...")
try:
crop_result = self._smart_crop_opencv(image)
if crop_result is not None:
cropped_image, crop_size = crop_result
crop_method = 'opencv'
self.logger.info(f"Applied OpenCV crop: {crop_size}")
self.logger.info(f"[CROP] OpenCV success: {crop_size}, pixels={crop_size[0]*crop_size[1]}")
# Detect text orientation within the cropped region
text_angle, angle_status = self._detect_text_orientation(
@@ -122,18 +124,19 @@ class ImageProcessor:
)
if text_angle is not None:
self.logger.info(
f"Detected text angle: {text_angle}° ({angle_status})"
f"[CROP] Text angle: {text_angle}° ({angle_status})"
)
if angle_status in ['upside_down', 'sideways']:
cropped_image = self._rotate_image(
cropped_image, text_angle
)
else:
self.logger.warning("[CROP] OpenCV returned None, using full image")
crop_method = 'pillow'
except (IOError, ValueError, cv2.error) as e:
# Fallback to Pillow if OpenCV fails
self.logger.warning(
f"OpenCV crop failed, falling back to Pillow: {e}"
f"[CROP] OpenCV failed: {e}, using full image"
)
crop_method = 'pillow'