Files

258 lines
12 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cv2
import numpy as np
from PIL import Image
import os
import sys
try:
import torch
TORCH_AVAILABLE = True
except ImportError:
TORCH_AVAILABLE = False
# ==========================================
# CONFIGURATION DEFAULTS (Robust Fallback)
# ==========================================
MIN_AREA = 500
MIN_WIDTH = 20
MIN_HEIGHT = 20
ALPHA_THRESHOLD = 240
COLORS = [{'name': 'Red', 'hex': '#FF4444', 'rgb': (255, 68, 68), 'bgr': (68, 68, 255)}]
BRIGHTNESS_THRESHOLD = 230
EROSION_KERNEL_SIZE = 9
BBOX_PADDING = 15 # Reverted to 15px per side (30px total)
# Try to import from config and override defaults
try:
from config import (
MIN_AREA as _MIN_AREA,
MIN_WIDTH as _MIN_WIDTH,
MIN_HEIGHT as _MIN_HEIGHT,
ALPHA_THRESHOLD as _ALPHA_THRESHOLD,
COLORS as _COLORS,
BRIGHTNESS_THRESHOLD as _BRIGHTNESS_THRESHOLD,
EROSION_KERNEL_SIZE as _EROSION_KERNEL_SIZE
)
# Override defaults with config values
MIN_AREA = _MIN_AREA
MIN_WIDTH = _MIN_WIDTH
MIN_HEIGHT = _MIN_HEIGHT
ALPHA_THRESHOLD = _ALPHA_THRESHOLD
COLORS = _COLORS
BRIGHTNESS_THRESHOLD = _BRIGHTNESS_THRESHOLD
EROSION_KERNEL_SIZE = _EROSION_KERNEL_SIZE
except ImportError as e:
print(f"⚠️ Config module not found: {e}. Using default values.")
except Exception as e:
print(f"⚠️ Error loading config: {e}. Using default values.", file=sys.stderr)
class FrameDetector:
def __init__(self):
self.device = 'cpu'
if TORCH_AVAILABLE and torch.cuda.is_available():
self.device = 'cuda'
print(f"✅ GPU Detected: {torch.cuda.get_device_name(0)}", file=sys.stderr)
else:
print("ℹ️ GPU not available or torch not installed. Using CPU.", file=sys.stderr)
def process_image(self, image_path, output_path=None):
"""
Detect frames in a PNG image.
Returns a list of rectangles and saves the processed image if output_path is provided.
"""
rectangles = []
try:
# Open image using PIL first to handle modes correctly
pil_img = Image.open(image_path)
# Check if it has alpha channel
has_alpha = pil_img.mode in ('RGBA', 'LA', 'P')
if has_alpha:
# Convert to RGBA if needed
if pil_img.mode == 'P':
pil_img = pil_img.convert('RGBA')
# Get numpy array
img_array = np.array(pil_img)
# Extract alpha channel
# Extract alpha channel
# Extract alpha channel logic
if img_array.shape[2] == 4:
# Check if alpha is fully opaque (or nearly so)
alpha_channel = img_array[:, :, 3]
min_alpha = np.min(alpha_channel)
if min_alpha >= 250:
print("ℹ️ Image appears opaque. Switching to White Area Detection.")
# Use Luminance/Brightness method
gray = cv2.cvtColor(img_array, cv2.COLOR_RGBA2GRAY)
# Detect bright white areas
mask = (gray > BRIGHTNESS_THRESHOLD).astype(np.uint8) * 255
else:
# Use Alpha method (Standard)
if self.device == 'cuda':
try:
tensor_img = torch.from_numpy(img_array).to(self.device)
alpha_tensor = tensor_img[:, :, 3]
mask_tensor = alpha_tensor < ALPHA_THRESHOLD
mask = mask_tensor.cpu().numpy().astype(np.uint8) * 255
except Exception as e:
print(f"⚠️ GPU processing failed: {e}", file=sys.stderr)
mask = (alpha_channel < ALPHA_THRESHOLD).astype(np.uint8) * 255
else:
mask = (alpha_channel < ALPHA_THRESHOLD).astype(np.uint8) * 255
# --- EROSION STEP ---
# Erode the mask to separate connected frames
# The mask has WHITE (255) for the features we want found.
kernel = np.ones((EROSION_KERNEL_SIZE, EROSION_KERNEL_SIZE), np.uint8)
mask = cv2.erode(mask, kernel, iterations=1)
# Find contours
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, countour in enumerate(contours):
x, y, w, h = cv2.boundingRect(countour)
# Filter by size
if (w * h >= MIN_AREA) and (w >= MIN_WIDTH) and (h >= MIN_HEIGHT):
# Expand bounding box by BBOX_PADDING pixels in ALL directions
# Force application of padding
img_height, img_width = img_array.shape[:2]
# Calculate expanded coordinates
# BBOX_PADDING is definitely adding to all sides
x_expanded = x - BBOX_PADDING
y_expanded = y - BBOX_PADDING
# Original width + left padding + right padding
w_expanded = w + (2 * BBOX_PADDING)
# Original height + top padding + bottom padding
h_expanded = h + (2 * BBOX_PADDING)
# Now clamp to be safe for array slicing, but the coordinate logic is applied
x_final = max(0, x_expanded)
y_final = max(0, y_expanded)
# Width needs to account for the shift in x
# If x was negative, we lose some width on the left, but we still want the right side to extend
# right_edge = x_expanded + w_expanded
# clamped_right_edge = min(img_width, right_edge)
# w_final = clamped_right_edge - x_final
# Correct clamping logic to preserve as much padding as possible within image limits
x_end_expanded = x + w + BBOX_PADDING
y_end_expanded = y + h + BBOX_PADDING
x_final = max(0, x - BBOX_PADDING)
y_final = max(0, y - BBOX_PADDING)
w_final = min(img_width, x_end_expanded) - x_final
h_final = min(img_height, y_end_expanded) - y_final
print(f"[DEBUG] Padding applied: BBOX_PADDING={BBOX_PADDING} | Original: ({x},{y} {w}x{h}) -> Expanded: ({int(x_final)},{int(y_final)} {int(w_final)}x{int(h_final)})")
# Use expanded coordinates
x, y, w, h = int(x_final), int(y_final), int(w_final), int(h_final)
# Calculate aspect ratio
aspect_ratio = float(w) / h if h > 0 else 0
# Determine shape type
if 0.85 <= aspect_ratio <= 1.15:
shape_type = "Square"
else:
shape_type = "Rectangle"
# Assign a color
color = COLORS[len(rectangles) % len(COLORS)]
rect = {
'id': len(rectangles) + 1,
'corners': {
'top_left': {'x': int(x), 'y': int(y)},
'top_right': {'x': int(x + w), 'y': int(y)},
'bottom_right': {'x': int(x + w), 'y': int(y + h)},
'bottom_left': {'x': int(x), 'y': int(y + h)}
},
'width': int(w),
'height': int(h),
'area': int(w * h),
'color': color,
'shape_type': shape_type,
'x': int(x), # For drawing convenience
'y': int(y)
}
rectangles.append(rect)
# Sort rectangles top-to-bottom, left-to-right
rectangles.sort(key=lambda r: (r['corners']['top_left']['y'], r['corners']['top_left']['x']))
# Generate coordinate string
coord_parts = []
for r in rectangles:
c = r['corners']
coord = f"{c['top_left']['x']},{c['top_left']['y']}:{c['top_right']['x']},{c['top_right']['y']}:{c['bottom_right']['x']},{c['bottom_right']['y']}:{c['bottom_left']['x']},{c['bottom_left']['y']}"
coord_parts.append(coord)
coord_string = ";".join(coord_parts)
# Save output image
if output_path and rectangles:
# Use OpenCV to draw
# Convert PIL to BGR for OpenCV
if pil_img.mode != 'RGB':
bg_img = pil_img.convert('RGB')
else:
bg_img = pil_img
opencv_img = cv2.cvtColor(np.array(bg_img), cv2.COLOR_RGB2BGR)
for r in rectangles:
# Draw rectangle
cv2.rectangle(opencv_img, (r['x'], r['y']), (r['x'] + r['width'], r['y'] + r['height']), r['color']['bgr'], 3)
# Draw corner markers
marker_size = 8
corners = [
(r['x'], r['y']),
(r['x'] + r['width'], r['y']),
(r['x'] + r['width'], r['y'] + r['height']),
(r['x'], r['y'] + r['height'])
]
for corner in corners:
cv2.circle(opencv_img, corner, marker_size, r['color']['bgr'], -1)
cv2.circle(opencv_img, corner, marker_size, (255, 255, 255), 2)
# Add label
label = f"{r['shape_type']} {r['id']}"
cv2.putText(opencv_img, label, (r['x'], r['y'] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, r['color']['bgr'], 2)
cv2.imwrite(output_path, opencv_img)
elif output_path:
# Save original if no rects found? Or maybe just return success false?
# For now let's save the original so frontend shows something
if pil_img.mode != 'RGB':
bg_img = pil_img.convert('RGB')
else:
bg_img = pil_img
opencv_img = cv2.cvtColor(np.array(bg_img), cv2.COLOR_RGB2BGR)
cv2.imwrite(output_path, opencv_img)
return {
'success': True,
'rectangles': rectangles,
'frame_count': len(rectangles),
'coordinate_string': coord_string,
'device': self.device # Return device info
}
except Exception as e:
print(f"Error processing image: {e}")
return {'success': False, 'error': str(e)}