Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,38 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import cv2
|
| 3 |
import gradio as gr
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
# Load Haar Cascade classifier
|
| 7 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 8 |
-
|
| 9 |
# Face Detection Function
|
| 10 |
-
def detect_faces(image_np,slider):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
gray_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 14 |
|
| 15 |
# Detect faces
|
| 16 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=slider, minNeighbors=5, minSize=(30, 30))
|
| 17 |
|
| 18 |
-
# Draw rectangles
|
| 19 |
for (x, y, w, h) in faces:
|
| 20 |
-
cv2.rectangle(
|
| 21 |
|
| 22 |
-
return img, len(faces)
|
| 23 |
|
| 24 |
# Create Gradio Interface
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=detect_faces,
|
| 27 |
-
inputs=[
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
title="Face Detection",
|
| 30 |
description="Upload an image, and the model will detect faces and draw bounding boxes around them."
|
| 31 |
)
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import cv2
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
# Load Haar Cascade classifier
|
| 6 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 7 |
+
|
| 8 |
# Face Detection Function
|
| 9 |
+
def detect_faces(image_np, slider):
|
| 10 |
+
# Convert image to numpy array
|
| 11 |
+
img = np.array(image_np)
|
| 12 |
+
|
| 13 |
+
# Convert to grayscale
|
| 14 |
gray_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 15 |
|
| 16 |
# Detect faces
|
| 17 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=slider, minNeighbors=5, minSize=(30, 30))
|
| 18 |
|
| 19 |
+
# Draw rectangles on original image
|
| 20 |
for (x, y, w, h) in faces:
|
| 21 |
+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 22 |
|
| 23 |
+
return img, f"Faces detected: {len(faces)}"
|
| 24 |
|
| 25 |
# Create Gradio Interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=detect_faces,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.Image(type="numpy", label="Upload Image"),
|
| 30 |
+
gr.Slider(minimum=1.1, maximum=2.0, step=0.1, label="Adjust the scale factor.")
|
| 31 |
+
],
|
| 32 |
+
outputs=[
|
| 33 |
+
gr.Image(label="Detected Faces"),
|
| 34 |
+
gr.Label(label="Face Count")
|
| 35 |
+
],
|
| 36 |
title="Face Detection",
|
| 37 |
description="Upload an image, and the model will detect faces and draw bounding boxes around them."
|
| 38 |
)
|