Spaces:
Runtime error
Runtime error
likhithAIML24
commited on
Commit
•
938b9ab
1
Parent(s):
a1f0fc9
Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,25 @@ import cv2
|
|
2 |
import gradio as gr
|
3 |
|
4 |
def detect_faces(image):
|
5 |
-
# Convert to grayscale for detection
|
6 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
10 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
|
11 |
-
|
|
|
12 |
for (x, y, w, h) in faces:
|
13 |
-
cv2.rectangle(image, (x, y), (x
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
iface = gr.Interface(fn=detect_faces,
|
18 |
-
inputs=gr.components.Image(
|
19 |
-
outputs=[gr.components.Image(type="pil"), gr.components.
|
20 |
-
title="Face Detection
|
21 |
-
description="This app detects faces in real-time
|
22 |
iface.launch()
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
def detect_faces(image):
|
5 |
+
# Convert the image to grayscale for better detection
|
6 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
7 |
+
|
8 |
+
# Load the pre-trained face detection model from OpenCV
|
9 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
10 |
+
|
11 |
+
# Detect faces in the image
|
12 |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
|
13 |
+
|
14 |
+
# Draw rectangles around the faces
|
15 |
for (x, y, w, h) in faces:
|
16 |
+
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
17 |
+
|
18 |
+
# Return the modified image and the number of faces detected
|
19 |
+
return image, len(faces)
|
20 |
|
21 |
iface = gr.Interface(fn=detect_faces,
|
22 |
+
inputs=gr.components.Image(source="webcam", type="pil"),
|
23 |
+
outputs=[gr.components.Image(type="pil"), gr.components.Label()],
|
24 |
+
title="Face Detection",
|
25 |
+
description="This app detects faces in real-time from your webcam.")
|
26 |
iface.launch()
|