File size: 2,956 Bytes
9b0a8c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import cv2
import numpy as np
import time
import os
from retinaface import RetinaFace


def blur_faces(image, blur_amount=30, verbose=False):
    # Start timer
    start_time = time.time()
    
    # Convert image from BGR to RGB (if using OpenCV's imread)
    # image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Detect faces using RetinaFace
    detection_start = time.time()
    faces = RetinaFace.detect_faces(image)
    detection_time = time.time() - detection_start
    
    # Create a copy of the image
    output_image = image.copy()
    
    face_count = 0
    blurring_start = time.time()
    # import pdb; pdb.set_trace()
    # Process each face
    if faces and isinstance(faces, dict):
        for face_key in faces:
            face_count += 1
            face_data = faces[face_key]
            
            # Get bounding box coordinates
            x1, y1, x2, y2 = face_data['facial_area']
            
            # Ensure the coordinates are within the image boundaries
            ih, iw = image.shape[:2]
            x1, y1 = max(0, x1), max(0, y1)
            x2, y2 = min(iw, x2), min(ih, y2)
            
            # Extract face region
            face_region = output_image[y1:y2, x1:x2]
            
            # Apply blur
            blurred_face = cv2.GaussianBlur(face_region, (blur_amount, blur_amount), 0)
            
            # Replace face region with blurred version
            output_image[y1:y2, x1:x2] = blurred_face
    
    blurring_time = time.time() - blurring_start
    total_time = time.time() - start_time
    
    if verbose:
        # Print timing information
        print(f"Face blurring performance metrics:")
        print(f"Total faces detected: {face_count}")
        print(f"Face detection time: {detection_time:.4f} seconds")
        print(f"Face blurring time: {blurring_time:.4f} seconds")
        print(f"Total processing time: {total_time:.4f} seconds")
        print(f"Average time per face: {(total_time/max(1, face_count)):.4f} seconds")
    
    return output_image, {
        "face_count": face_count,
        "detection_time": detection_time,
        "blurring_time": blurring_time,
        "total_time": total_time,
        "avg_time_per_face": total_time/max(1, face_count)
    }

# Example usage
if __name__ == "__main__":
    # Read an image from file
    image_path = "multiple_people2.jpeg"  # Replace with your image path
    output_path = "blurred_output.jpg"     # Output path for the blurred image
    
    image = cv2.imread(image_path)
    
    if image is None:
        print(f"Could not read image from {image_path}")
    else:
        # Process the image with timing
        result_image, timing_info = blur_faces(image, blur_amount=31)
        
        # Save the result
        cv2.imwrite(output_path, result_image)
        print(f"Blurred image saved to {output_path}")



## requirements and cv2
# pip install retina-face 
# pip install opencv-python