import os import numpy as np import torch import cv2 import matplotlib.pyplot as plt import gradio as gr import io from PIL import Image, ImageDraw, ImageFont import spaces from typing import Dict, List, Any, Optional, Tuple from ultralytics import YOLO from detection_model import DetectionModel from color_mapper import ColorMapper from visualization_helper import VisualizationHelper from evaluation_metrics import EvaluationMetrics from style import Style color_mapper = ColorMapper() model_instances = {} @spaces.GPU def process_image(image, model_instance, confidence_threshold, filter_classes=None): """ Process an image for object detection Args: image: Input image (numpy array or PIL Image) model_instance: DetectionModel instance to use confidence_threshold: Confidence threshold for detection filter_classes: Optional list of classes to filter results Returns: Tuple of (result_image, result_text, stats_data) """ # initialize key variables result = None stats = {} temp_path = None try: # update confidence threshold model_instance.confidence = confidence_threshold # processing input image if isinstance(image, np.ndarray): # Convert BGR to RGB if needed if image.shape[2] == 3: image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) else: image_rgb = image pil_image = Image.fromarray(image_rgb) elif image is None: return None, "No image provided. Please upload an image.", {} else: pil_image = image # store temp files import uuid import tempfile temp_dir = tempfile.gettempdir() # use system temp directory temp_filename = f"temp_{uuid.uuid4().hex}.jpg" temp_path = os.path.join(temp_dir, temp_filename) pil_image.save(temp_path) # object detection result = model_instance.detect(temp_path) if result is None: return None, "Detection failed. Please try again with a different image.", {} # calculate stats stats = EvaluationMetrics.calculate_basic_stats(result) # add space calculation spatial_metrics = EvaluationMetrics.calculate_distance_metrics(result) stats["spatial_metrics"] = spatial_metrics if filter_classes and len(filter_classes) > 0: # get classes, boxes, confidence classes = result.boxes.cls.cpu().numpy().astype(int) confs = result.boxes.conf.cpu().numpy() boxes = result.boxes.xyxy.cpu().numpy() mask = np.zeros_like(classes, dtype=bool) for cls_id in filter_classes: mask = np.logical_or(mask, classes == cls_id) filtered_stats = { "total_objects": int(np.sum(mask)), "class_statistics": {}, "average_confidence": float(np.mean(confs[mask])) if np.any(mask) else 0, "spatial_metrics": stats["spatial_metrics"] } # update stats names = result.names for cls, conf in zip(classes[mask], confs[mask]): cls_name = names[int(cls)] if cls_name not in filtered_stats["class_statistics"]: filtered_stats["class_statistics"][cls_name] = { "count": 0, "average_confidence": 0 } filtered_stats["class_statistics"][cls_name]["count"] += 1 filtered_stats["class_statistics"][cls_name]["average_confidence"] = conf stats = filtered_stats viz_data = EvaluationMetrics.generate_visualization_data( result, color_mapper.get_all_colors() ) result_image = VisualizationHelper.visualize_detection( temp_path, result, color_mapper=color_mapper, figsize=(12, 12), return_pil=True ) result_text = EvaluationMetrics.format_detection_summary(viz_data) return result_image, result_text, stats except Exception as e: error_message = f"Error Occurs: {str(e)}" import traceback traceback.print_exc() print(error_message) return None, error_message, {} finally: if temp_path and os.path.exists(temp_path): try: os.remove(temp_path) except Exception as e: print(f"Cannot delete temp files {temp_path}: {str(e)}") def format_result_text(stats): """ Format detection statistics into readable text with improved spacing Args: stats: Dictionary containing detection statistics Returns: Formatted text summary """ if not stats or "total_objects" not in stats: return "No objects detected." # 減少不必要的空行 lines = [ f"Detected {stats['total_objects']} objects.", f"Average confidence: {stats.get('average_confidence', 0):.2f}", "Objects by class:" ] if "class_statistics" in stats and stats["class_statistics"]: # 按計數排序類別 sorted_classes = sorted( stats["class_statistics"].items(), key=lambda x: x[1]["count"], reverse=True ) for cls_name, cls_stats in sorted_classes: count = cls_stats["count"] conf = cls_stats.get("average_confidence", 0) item_text = "item" if count == 1 else "items" lines.append(f"• {cls_name}: {count} {item_text} (avg conf: {conf:.2f})") else: lines.append("No class information available.") # 添加空間信息 if "spatial_metrics" in stats and "spatial_distribution" in stats["spatial_metrics"]: lines.append("Object Distribution:") dist = stats["spatial_metrics"]["spatial_distribution"] x_mean = dist.get("x_mean", 0) y_mean = dist.get("y_mean", 0) # 描述物體的大致位置 if x_mean < 0.33: h_pos = "on the left side" elif x_mean < 0.67: h_pos = "in the center" else: h_pos = "on the right side" if y_mean < 0.33: v_pos = "in the upper part" elif y_mean < 0.67: v_pos = "in the middle" else: v_pos = "in the lower part" lines.append(f"• Most objects appear {h_pos} {v_pos} of the image") return "\n".join(lines) def format_json_for_display(stats): """ Format statistics JSON for better display Args: stats: Raw statistics dictionary Returns: Formatted statistics structure for display """ # Create a cleaner copy of the stats for display display_stats = {} # Add summary section display_stats["summary"] = { "total_objects": stats.get("total_objects", 0), "average_confidence": round(stats.get("average_confidence", 0), 3) } # Add class statistics in a more organized way if "class_statistics" in stats and stats["class_statistics"]: # Sort classes by count (descending) sorted_classes = sorted( stats["class_statistics"].items(), key=lambda x: x[1].get("count", 0), reverse=True ) class_stats = {} for cls_name, cls_data in sorted_classes: class_stats[cls_name] = { "count": cls_data.get("count", 0), "average_confidence": round(cls_data.get("average_confidence", 0), 3) } display_stats["detected_objects"] = class_stats # Simplify spatial metrics if "spatial_metrics" in stats: spatial = stats["spatial_metrics"] # Simplify spatial distribution if "spatial_distribution" in spatial: dist = spatial["spatial_distribution"] display_stats["spatial"] = { "distribution": { "x_mean": round(dist.get("x_mean", 0), 3), "y_mean": round(dist.get("y_mean", 0), 3), "x_std": round(dist.get("x_std", 0), 3), "y_std": round(dist.get("y_std", 0), 3) } } # Add simplified size information if "size_distribution" in spatial: size = spatial["size_distribution"] display_stats["spatial"]["size"] = { "mean_area": round(size.get("mean_area", 0), 3), "min_area": round(size.get("min_area", 0), 3), "max_area": round(size.get("max_area", 0), 3) } return display_stats def get_all_classes(): """ Get all available COCO classes from the currently active model or fallback to standard COCO classes Returns: List of tuples (class_id, class_name) """ global model_instances # Try to get class names from any loaded model for model_name, model_instance in model_instances.items(): if model_instance and model_instance.is_model_loaded: try: class_names = model_instance.class_names return [(idx, name) for idx, name in class_names.items()] except Exception: pass # Fallback to standard COCO classes return [ (0, 'person'), (1, 'bicycle'), (2, 'car'), (3, 'motorcycle'), (4, 'airplane'), (5, 'bus'), (6, 'train'), (7, 'truck'), (8, 'boat'), (9, 'traffic light'), (10, 'fire hydrant'), (11, 'stop sign'), (12, 'parking meter'), (13, 'bench'), (14, 'bird'), (15, 'cat'), (16, 'dog'), (17, 'horse'), (18, 'sheep'), (19, 'cow'), (20, 'elephant'), (21, 'bear'), (22, 'zebra'), (23, 'giraffe'), (24, 'backpack'), (25, 'umbrella'), (26, 'handbag'), (27, 'tie'), (28, 'suitcase'), (29, 'frisbee'), (30, 'skis'), (31, 'snowboard'), (32, 'sports ball'), (33, 'kite'), (34, 'baseball bat'), (35, 'baseball glove'), (36, 'skateboard'), (37, 'surfboard'), (38, 'tennis racket'), (39, 'bottle'), (40, 'wine glass'), (41, 'cup'), (42, 'fork'), (43, 'knife'), (44, 'spoon'), (45, 'bowl'), (46, 'banana'), (47, 'apple'), (48, 'sandwich'), (49, 'orange'), (50, 'broccoli'), (51, 'carrot'), (52, 'hot dog'), (53, 'pizza'), (54, 'donut'), (55, 'cake'), (56, 'chair'), (57, 'couch'), (58, 'potted plant'), (59, 'bed'), (60, 'dining table'), (61, 'toilet'), (62, 'tv'), (63, 'laptop'), (64, 'mouse'), (65, 'remote'), (66, 'keyboard'), (67, 'cell phone'), (68, 'microwave'), (69, 'oven'), (70, 'toaster'), (71, 'sink'), (72, 'refrigerator'), (73, 'book'), (74, 'clock'), (75, 'vase'), (76, 'scissors'), (77, 'teddy bear'), (78, 'hair drier'), (79, 'toothbrush') ] def create_interface(): """創建 Gradio 界面,包含美化的視覺效果""" css = Style.get_css() # 獲取可用模型信息 available_models = DetectionModel.get_available_models() model_choices = [model["model_file"] for model in available_models] model_labels = [f"{model['name']} - {model['inference_speed']}" for model in available_models] # 可用類別過濾選項 available_classes = get_all_classes() class_choices = [f"{id}: {name}" for id, name in available_classes] # 創建 Gradio Blocks 界面 with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="teal", secondary_hue="blue")) as demo: # 頁面頂部標題 with gr.Group(elem_classes="app-header"): gr.HTML("""