Spaces:
Runtime error
Runtime error
Johannes
commited on
Commit
Β·
b713355
1
Parent(s):
969d055
init
Browse files
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Face Detection Cnn
|
3 |
-
emoji:
|
4 |
colorFrom: yellow
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Face Detection Cnn
|
3 |
+
emoji: π±
|
4 |
colorFrom: yellow
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
|
7 |
+
import kornia as K
|
8 |
+
from kornia.contrib import FaceDetector, FaceDetectorResult
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
import face_detection
|
13 |
+
|
14 |
+
|
15 |
+
def detect_faces(img: np.ndarray, method:str):
|
16 |
+
frame = np.array(img)
|
17 |
+
|
18 |
+
kornia_detections = kornia_detect(frame)
|
19 |
+
retina_detections = retina_detect(frame)
|
20 |
+
retina_mobile_detections = retina_mobilenet_detect(frame)
|
21 |
+
dsfd_detections = dsfd_detect(frame)
|
22 |
+
|
23 |
+
# if method == "Kornia YuNet":
|
24 |
+
# re_im = kornia_detect(frame)
|
25 |
+
# elif method == "RetinaFace":
|
26 |
+
# re_im = retina_detect(frame)
|
27 |
+
|
28 |
+
return kornia_detections, retina_detections, retina_mobile_detections, dsfd_detections
|
29 |
+
|
30 |
+
def scale_image(img: np.ndarray, size: int) -> np.ndarray:
|
31 |
+
h, w = img.shape[:2]
|
32 |
+
scale = 1.0 * size / w
|
33 |
+
return cv2.resize(img, (int(w * scale), int(h * scale)))
|
34 |
+
|
35 |
+
|
36 |
+
def base_detect(detector, img):
|
37 |
+
img = scale_image(img, 400)
|
38 |
+
|
39 |
+
detections = detector.detect(img)
|
40 |
+
img_vis = img.copy()
|
41 |
+
|
42 |
+
for box in detections:
|
43 |
+
img_vis = cv2.rectangle(img_vis,
|
44 |
+
box[:2].astype(int).tolist(),
|
45 |
+
box[2:4].astype(int).tolist(),
|
46 |
+
(0, 255, 0), 1)
|
47 |
+
|
48 |
+
return img_vis
|
49 |
+
|
50 |
+
|
51 |
+
def retina_detect(img):
|
52 |
+
detector = face_detection.build_detector(
|
53 |
+
"RetinaNetResNet50", confidence_threshold=.5, nms_iou_threshold=.3)
|
54 |
+
|
55 |
+
img_vis = base_detect(detector, img)
|
56 |
+
|
57 |
+
return img_vis
|
58 |
+
|
59 |
+
|
60 |
+
def retina_mobilenet_detect(img):
|
61 |
+
detector = face_detection.build_detector(
|
62 |
+
"RetinaNetMobileNetV1", confidence_threshold=.5, nms_iou_threshold=.3)
|
63 |
+
|
64 |
+
img_vis = base_detect(detector, img)
|
65 |
+
|
66 |
+
return img_vis
|
67 |
+
|
68 |
+
|
69 |
+
def dsfd_detect(img):
|
70 |
+
detector = face_detection.build_detector(
|
71 |
+
"DSFDDetector", confidence_threshold=.5, nms_iou_threshold=.3)
|
72 |
+
|
73 |
+
img_vis = base_detect(detector, img)
|
74 |
+
|
75 |
+
return img_vis
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
def kornia_detect(img):
|
80 |
+
# select the device
|
81 |
+
device = torch.device('cpu')
|
82 |
+
vis_threshold = 0.6
|
83 |
+
|
84 |
+
# load the image and scale
|
85 |
+
img_raw = scale_image(img, 400)
|
86 |
+
|
87 |
+
# preprocess
|
88 |
+
img = K.image_to_tensor(img_raw, keepdim=False).to(device)
|
89 |
+
img = K.color.bgr_to_rgb(img.float())
|
90 |
+
|
91 |
+
# create the detector and find the faces !
|
92 |
+
face_detection = FaceDetector().to(device)
|
93 |
+
|
94 |
+
with torch.no_grad():
|
95 |
+
dets = face_detection(img)
|
96 |
+
dets = [FaceDetectorResult(o) for o in dets]
|
97 |
+
|
98 |
+
# show image
|
99 |
+
|
100 |
+
img_vis = img_raw.copy()
|
101 |
+
|
102 |
+
for b in dets:
|
103 |
+
if b.score < vis_threshold:
|
104 |
+
continue
|
105 |
+
|
106 |
+
# draw face bounding box
|
107 |
+
img_vis = cv2.rectangle(img_vis,
|
108 |
+
b.top_left.int().tolist(),
|
109 |
+
b.bottom_right.int().tolist(),
|
110 |
+
(0, 255, 0),
|
111 |
+
1)
|
112 |
+
|
113 |
+
return img_vis
|
114 |
+
|
115 |
+
|
116 |
+
input_image = gr.components.Image()
|
117 |
+
|
118 |
+
image_kornia = gr.components.Image(label="Kornia YuNet")
|
119 |
+
image_retina = gr.components.Image(label="RetinaFace")
|
120 |
+
image_retina_mobile = gr.components.Image(label="Retina Mobilenet")
|
121 |
+
image_dsfd = gr.components.Image(label="DSFD")
|
122 |
+
|
123 |
+
|
124 |
+
confidence_slider = gr.components.Slider(minimum=0.1, maximum=0.9, value=0.5, label="Confidence Threshold")
|
125 |
+
nms_slider = gr.components.Slider(minimum=0.1, maximum=0.9, value=0.5, label="Min Number of Neighbours")
|
126 |
+
# scale_slider = gr.components.Slider(minimum=1.1, maximum=2.0, value=1.3, step=0.1, label="Scale Factor")
|
127 |
+
# classifier_radio = gr.components.Radio(s)
|
128 |
+
|
129 |
+
methods_dropdown = gr.components.Dropdown(["Kornia YuNet", "RetinaFace", "RetinaMobile", "DSFD"], value="Kornia YuNet", label="Choose a method")
|
130 |
+
|
131 |
+
description = """Face Detection"""
|
132 |
+
|
133 |
+
|
134 |
+
Iface = gr.Interface(
|
135 |
+
fn=detect_faces,
|
136 |
+
inputs=[input_image, methods_dropdown],#, size_slider, neighbour_slider, scale_slider],
|
137 |
+
outputs=[image_kornia, image_retina, image_retina_mobile, image_dsfd],
|
138 |
+
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"]],
|
139 |
+
title="Face Detection",
|
140 |
+
).launch()
|
data/12_Group_Group_12_Group_Group_12_39.jpg
ADDED
data/31_Waiter_Waitress_Waiter_Waitress_31_55.jpg
ADDED
data/9_Press_Conference_Press_Conference_9_86.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
kornia
|
2 |
+
opencv-python
|
3 |
+
git+https://github.com/hukkelas/DSFD-Pytorch-Inference.git
|