File size: 1,334 Bytes
77829f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import cv2
import gradio as gr

def detect_face(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    path = "haarcascade_frontalface_default.xml"
    face_cascade = cv2.CascadeClassifier(path)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=17, minSize=(40, 40))
    return len(faces)

def face_detector(image):
    img_np = np.array(image)
    img = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
    # img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    num_faces = detect_face(img)
    return num_faces

title = "Face Detector: Counts the Number of Faces in an Image\n\n"
title += "<span style='font-size: smaller;'>Subtitle: The face detector counts the number of faces and returns it. It works fairly well. This is built based on Haar Cascade Algorithm.</span>"

iface = gr.Interface(
    fn=face_detector,
    inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
    outputs="text",
    title=title,
    examples=[
        ["sample_faces/angry_face.jpg"],
        ["sample_faces/beard_man.jpg"],
        ["sample_faces/bw_face.jpg"],
        ["sample_faces/faces.jpeg"],
        ["sample_faces/glass_man.jpg"],
        ["sample_faces/normal_face.jpg"],
        ["sample_faces/red_dress.jpg"]
    ],
    allow_flagging=False,
)

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