Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,47 +3,46 @@ import imutils
|
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
|
| 7 |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 8 |
|
| 9 |
def detect_faces(img, size, neighbours, scale):
|
| 10 |
-
frame = np.array(img)
|
| 11 |
frame = frame[:, :, ::-1].copy()
|
| 12 |
frame = imutils.resize(frame, width=500)
|
| 13 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 14 |
|
|
|
|
| 15 |
faceRects = face_detector.detectMultiScale(
|
| 16 |
-
gray,
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
-
box_data = []
|
| 20 |
-
class_labels = {0: "face"}
|
| 21 |
-
|
| 22 |
for (x, y, w, h) in faceRects:
|
| 23 |
-
|
| 24 |
-
midX = int(x + w / 2)
|
| 25 |
-
midY = int(y + h / 2)
|
| 26 |
-
box = {
|
| 27 |
-
"position": {"middle": [midX, midY], "width": float(w), "height": float(h)},
|
| 28 |
-
"domain": "pixel",
|
| 29 |
-
"class_id": 0
|
| 30 |
-
}
|
| 31 |
-
box_data.append(box)
|
| 32 |
|
| 33 |
re_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 34 |
return re_im
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
size_slider = gr.
|
| 40 |
-
neighbour_slider = gr.
|
| 41 |
-
scale_slider = gr.
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
fn=detect_faces,
|
| 45 |
inputs=[image, size_slider, neighbour_slider, scale_slider],
|
| 46 |
outputs=out_im,
|
| 47 |
-
title="Haar Cascade
|
| 48 |
-
description="
|
| 49 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# ✅ Use OpenCV’s built-in Haar Cascade path (no local folder needed)
|
| 7 |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 8 |
|
| 9 |
def detect_faces(img, size, neighbours, scale):
|
| 10 |
+
frame = np.array(img)
|
| 11 |
frame = frame[:, :, ::-1].copy()
|
| 12 |
frame = imutils.resize(frame, width=500)
|
| 13 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 14 |
|
| 15 |
+
# ✅ Use slider values safely
|
| 16 |
faceRects = face_detector.detectMultiScale(
|
| 17 |
+
gray,
|
| 18 |
+
scaleFactor=float(scale),
|
| 19 |
+
minNeighbors=int(neighbours),
|
| 20 |
+
minSize=(int(size), int(size))
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
for (x, y, w, h) in faceRects:
|
| 24 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
re_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 27 |
return re_im
|
| 28 |
|
| 29 |
+
# ✅ Define Gradio components
|
| 30 |
+
image = gr.Image(label="Upload an Image")
|
| 31 |
+
out_im = gr.Image(label="Detected Faces")
|
| 32 |
|
| 33 |
+
size_slider = gr.Slider(minimum=5, maximum=50, value=30, step=5, label="Min Face Size (pixels)")
|
| 34 |
+
neighbour_slider = gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Min Neighbours")
|
| 35 |
+
scale_slider = gr.Slider(minimum=1.1, maximum=2.0, value=1.3, step=0.1, label="Scale Factor")
|
| 36 |
|
| 37 |
+
# ✅ Gradio Interface
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
fn=detect_faces,
|
| 40 |
inputs=[image, size_slider, neighbour_slider, scale_slider],
|
| 41 |
outputs=out_im,
|
| 42 |
+
title="Haar Cascade Face Detection",
|
| 43 |
+
description="Detect faces using OpenCV's Haar Cascade classifier. Adjust parameters to fine-tune detection."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# ✅ Hugging Face needs this to run the app
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|