Spaces:
Runtime error
Runtime error
| import numpy as np | |
| from PIL import Image, ImageDraw | |
| import json | |
| def draw_detections(image, face_results, object_results, show_labels, box_color): | |
| """Draw detection boxes on image using PIL.""" | |
| try: | |
| pil_image = Image.fromarray(image) | |
| draw = ImageDraw.Draw(pil_image) | |
| # Convert color name to RGB | |
| color_map = { | |
| "red": (255, 0, 0), | |
| "green": (0, 255, 0), | |
| "blue": (0, 0, 255), | |
| "yellow": (255, 255, 0), | |
| "purple": (128, 0, 128), | |
| "orange": (255, 165, 0) | |
| } | |
| color = color_map.get(box_color, (255, 0, 0)) | |
| # Draw face boxes | |
| for face in face_results: | |
| x, y, w, h = face["bbox"] | |
| draw.rectangle([x, y, x + w, y + h], outline=color, width=3) | |
| if show_labels: | |
| label = f"Face {face.get('confidence', '')}" | |
| draw.text((x, y - 20), label, fill=color) | |
| # Draw object boxes | |
| for obj in object_results: | |
| x, y, w, h = obj["bbox"] | |
| draw.rectangle([x, y, x + w, y + h], outline=color, width=3) | |
| if show_labels: | |
| label = f"{obj['label']} {obj.get('confidence', '')}" | |
| draw.text((x, y - 20), label, fill=color) | |
| return np.array(pil_image) | |
| except Exception as e: | |
| print(f"Error drawing detections: {e}") | |
| return image | |
| def process_image(image, face_cascade, object_net, object_classes, enable_face, enable_objects, face_conf, object_conf): | |
| """Process image and detect faces and objects.""" | |
| from models import detect_faces, detect_objects | |
| face_results = [] | |
| object_results = [] | |
| if enable_face: | |
| face_results = detect_faces(image, face_cascade, face_conf) | |
| if enable_objects: | |
| object_results = detect_objects(image, object_net, object_classes, object_conf) | |
| return image.copy(), face_results, object_results | |
| def load_detection_models(): | |
| """Load detection models.""" | |
| from models import load_detection_models as load_models | |
| return load_models() |