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