File size: 2,130 Bytes
5b5e1e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()