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 += "Subtitle: The face detector counts the number of faces and returns it. It works fairly well. This is built based on Haar Cascade Algorithm." 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()