face_detection / app.py
pushpinder06's picture
Update app.py
21bacb4 verified
raw
history blame
1.19 kB
import numpy as np
import cv2
import gradio as gr
# Load Haar Cascade classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# Face Detection Function
def detect_faces(image_np, slider):
# Convert image to numpy array
img = np.array(image_np)
# Convert to grayscale
gray_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=slider, minNeighbors=5, minSize=(30, 30))
# Draw rectangles on original image
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
return img, f"Faces detected: {len(faces)}"
# Create Gradio Interface
iface = gr.Interface(
fn=detect_faces,
inputs=[
gr.Image(type="numpy", label="Upload Image"),
gr.Slider(minimum=1.1, maximum=2.0, step=0.1, label="Adjust the scale factor.")
],
outputs=[
gr.Image(label="Detected Faces"),
gr.Label(label="Face Count")
],
title="Face Detection",
description="Upload an image, and the model will detect faces and draw bounding boxes around them."
)
iface.launch()