Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,60 @@
|
|
1 |
-
import cv2
|
2 |
-
import imutils
|
3 |
import gradio as gr
|
|
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
frame = np.array(img)
|
10 |
-
frame = frame[:, :, ::-1].copy()
|
11 |
-
frame = imutils.resize(frame, width=500)
|
12 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
13 |
|
14 |
-
faceRects = face_detector.detectMultiScale(gray, scaleFactor=scale, minNeighbors=neighbours, minSize=(size, size),flags=cv2.CASCADE_SCALE_IMAGE)
|
15 |
|
16 |
-
box_data = []
|
17 |
|
18 |
-
|
19 |
-
0: "face"
|
20 |
-
}
|
21 |
|
22 |
-
|
23 |
-
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
|
24 |
|
25 |
-
|
26 |
-
midY = int(y+h/2)
|
27 |
-
box = {"position": {"middle": [midX, midY],"width": float(w),"height": float(h)},"domain" : "pixel","class_id" : 0}
|
28 |
-
box_data.append(box)
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
return re_im
|
34 |
-
|
35 |
-
image = gr.components.Image()
|
36 |
-
out_im = gr.components.Image()
|
37 |
|
38 |
-
|
39 |
-
neighbour_slider = gr.components.Slider(minimum=1, maximum=20, value=5, step=1, label="Min Number of Neighbours")
|
40 |
-
scale_slider = gr.components.Slider(minimum=1.1, maximum=2.0, value=1.3, step=0.1, label="Scale Factor")
|
41 |
|
42 |
-
description = """Face Detection with Haar Cascades using OpenCV"""
|
43 |
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
import numpy as np
|
5 |
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
def detect_faces(image , slider ) :
|
13 |
+
|
14 |
+
# detect faces
|
15 |
+
|
16 |
+
# convert image in to numpy array
|
17 |
+
|
18 |
+
image_np = np.array(image)
|
19 |
+
|
20 |
+
# convert image into gray
|
21 |
+
|
22 |
+
gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
23 |
+
|
24 |
+
# use detectmultiscale function to detect faces using haar cascade
|
25 |
+
|
26 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
27 |
+
|
28 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=slider, minNeighbors=5, minSize=(30, 30))
|
29 |
+
|
30 |
+
# draw rectangle along detected faces
|
31 |
+
|
32 |
+
for (x, y, w, h) in faces:
|
33 |
+
|
34 |
+
cv2.rectangle(image_np, (x, y), (x+w, y+h), (255, 0, 0), 5)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
return image_np
|
39 |
+
|
40 |
+
|
41 |
|
42 |
+
slider = gr.Slider(minimum=1, maximum=2, step=.1, label="Adjust the ScaleFactor")
|
|
|
|
|
|
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
46 |
+
iface = gr.Interface( fn=detect_faces,
|
|
|
|
|
47 |
|
48 |
+
inputs=["image","slider"],
|
|
|
49 |
|
50 |
+
outputs="image",
|
|
|
|
|
|
|
51 |
|
52 |
+
title="Face Detection using Haar Cascade Classifier ",
|
53 |
|
54 |
+
description="Upload an image,and the model will detect faces and draw bounding boxes around them.",
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
)
|
|
|
|
|
57 |
|
|
|
58 |
|
59 |
|
60 |
+
iface.launch()
|
|