File size: 1,253 Bytes
c1bf349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import cv2
import numpy as np
import os

from modules import face_analyser, globals

def detect_faces(image):
    if image is None:
        return None

    # Convert from PIL to OpenCV (RGB → BGR)
    cv2_img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

    # Save temporary image file
    temp_path = "input.jpg"
    cv2.imwrite(temp_path, cv2_img)
    globals.target_path = temp_path

    # Reset mapping before each run
    globals.source_target_map = []

    try:
        face_analyser.get_unique_faces_from_target_image()
    except Exception as e:
        return f"Face detection error: {e}"

    # Return first cropped face (if available)
    if globals.source_target_map and "target" in globals.source_target_map[0]:
        crop = globals.source_target_map[0]["target"]["cv2"]
        return cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)

    return "No faces found."

demo = gr.Interface(
    fn=detect_faces,
    inputs=gr.Image(type="pil", label="Upload your image"),
    outputs="image",
    title="🦉 OwlCamPro – Face Detection Preview",
    description="Upload an image to detect and extract the most prominent face. Powered by InsightFace and Deep-Live-Cam 2.0."
)

if __name__ == "__main__":
    demo.launch()