Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Models
|
| 4 |
+
Datasets
|
| 5 |
+
Spaces
|
| 6 |
+
Community
|
| 7 |
+
Docs
|
| 8 |
+
Pricing
|
| 9 |
+
|
| 10 |
+
Log In
|
| 11 |
+
Sign Up
|
| 12 |
+
Spaces:
|
| 13 |
+
johko
|
| 14 |
+
/
|
| 15 |
+
haar-cascade-frontal-face-detection
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
like
|
| 19 |
+
0
|
| 20 |
+
App
|
| 21 |
+
Files
|
| 22 |
+
Community
|
| 23 |
+
haar-cascade-frontal-face-detection
|
| 24 |
+
/
|
| 25 |
+
app.py
|
| 26 |
+
|
| 27 |
+
sagemaker
|
| 28 |
+
remove exmples
|
| 29 |
+
e427213
|
| 30 |
+
over 3 years ago
|
| 31 |
+
raw
|
| 32 |
+
|
| 33 |
+
Copy download link
|
| 34 |
+
history
|
| 35 |
+
blame
|
| 36 |
+
contribute
|
| 37 |
+
delete
|
| 38 |
+
|
| 39 |
+
2.1 kB
|
| 40 |
+
import cv2
|
| 41 |
+
import imutils
|
| 42 |
+
import gradio as gr
|
| 43 |
+
import numpy as np
|
| 44 |
+
|
| 45 |
+
face_detector = cv2.CascadeClassifier("haar_cascades/haarcascade_frontalface_default.xml")
|
| 46 |
+
|
| 47 |
+
def detect_faces(img, size, neighbours, scale):
|
| 48 |
+
frame = np.array(img)
|
| 49 |
+
frame = frame[:, :, ::-1].copy()
|
| 50 |
+
frame = imutils.resize(frame, width=500)
|
| 51 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 52 |
+
|
| 53 |
+
faceRects = face_detector.detectMultiScale(
|
| 54 |
+
gray, scaleFactor=scale, minNeighbors=neighbours, minSize=(size, size),
|
| 55 |
+
flags=cv2.CASCADE_SCALE_IMAGE)
|
| 56 |
+
|
| 57 |
+
box_data = []
|
| 58 |
+
|
| 59 |
+
class_labels = {
|
| 60 |
+
0: "face"
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
for (x,y,w,h) in faceRects:
|
| 64 |
+
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
|
| 65 |
+
|
| 66 |
+
midX = int(x+w/2)
|
| 67 |
+
midY = int(y+h/2)
|
| 68 |
+
box = {
|
| 69 |
+
"position": {
|
| 70 |
+
"middle": [midX, midY],
|
| 71 |
+
"width": float(w),
|
| 72 |
+
"height": float(h)
|
| 73 |
+
},
|
| 74 |
+
"domain" : "pixel",
|
| 75 |
+
"class_id" : 0
|
| 76 |
+
}
|
| 77 |
+
box_data.append(box)
|
| 78 |
+
|
| 79 |
+
predictions = {"predictions": {
|
| 80 |
+
"box_data": box_data,
|
| 81 |
+
"class_labels": class_labels
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
re_im =cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 86 |
+
return re_im
|
| 87 |
+
|
| 88 |
+
image = gr.components.Image()
|
| 89 |
+
out_im = gr.components.Image()
|
| 90 |
+
|
| 91 |
+
size_slider = gr.components.Slider(minimum=5, maximum=50, value=30, step=5, label="MinSize in Pixel")
|
| 92 |
+
neighbour_slider = gr.components.Slider(minimum=1, maximum=20, value=5, step=1, label="Min Number of Neighbours")
|
| 93 |
+
scale_slider = gr.components.Slider(minimum=1.1, maximum=2.0, value=1.3, step=0.1, label="Scale Factor")
|
| 94 |
+
|
| 95 |
+
description = """Face Detection with Haar Cascades using OpenCV"""
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
Iface = gr.Interface(
|
| 99 |
+
fn=detect_faces,
|
| 100 |
+
inputs=[image, size_slider, neighbour_slider, scale_slider],
|
| 101 |
+
outputs=out_im,
|
| 102 |
+
#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"]],
|
| 103 |
+
title="Haar Cascade Object Detection",
|
| 104 |
+
).launch()
|