Spaces:
Running
Running
Add files
Browse files- .gitignore +1 -0
- app.py +120 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
images
|
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import argparse
|
6 |
+
import os
|
7 |
+
import pathlib
|
8 |
+
import subprocess
|
9 |
+
import tarfile
|
10 |
+
|
11 |
+
if os.environ.get('SYSTEM') == 'spaces':
|
12 |
+
subprocess.call('pip uninstall -y opencv-python'.split())
|
13 |
+
subprocess.call('pip uninstall -y opencv-python-headless'.split())
|
14 |
+
subprocess.call('pip install opencv-python-headless==4.5.5.64'.split())
|
15 |
+
|
16 |
+
import gradio as gr
|
17 |
+
import huggingface_hub
|
18 |
+
import mediapipe as mp
|
19 |
+
import numpy as np
|
20 |
+
|
21 |
+
mp_face_detection = mp.solutions.face_detection
|
22 |
+
mp_drawing = mp.solutions.drawing_utils
|
23 |
+
|
24 |
+
TITLE = 'MediaPipe Face Detection'
|
25 |
+
DESCRIPTION = 'https://google.github.io/mediapipe/'
|
26 |
+
ARTICLE = None
|
27 |
+
|
28 |
+
TOKEN = os.environ['TOKEN']
|
29 |
+
|
30 |
+
|
31 |
+
def parse_args() -> argparse.Namespace:
|
32 |
+
parser = argparse.ArgumentParser()
|
33 |
+
parser.add_argument('--theme', type=str)
|
34 |
+
parser.add_argument('--live', action='store_true')
|
35 |
+
parser.add_argument('--share', action='store_true')
|
36 |
+
parser.add_argument('--port', type=int)
|
37 |
+
parser.add_argument('--disable-queue',
|
38 |
+
dest='enable_queue',
|
39 |
+
action='store_false')
|
40 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
41 |
+
parser.add_argument('--allow-screenshot', action='store_true')
|
42 |
+
return parser.parse_args()
|
43 |
+
|
44 |
+
|
45 |
+
def load_sample_images() -> list[pathlib.Path]:
|
46 |
+
image_dir = pathlib.Path('images')
|
47 |
+
if not image_dir.exists():
|
48 |
+
image_dir.mkdir()
|
49 |
+
dataset_repo = 'hysts/input-images'
|
50 |
+
filenames = ['001.tar', '005.tar']
|
51 |
+
for name in filenames:
|
52 |
+
path = huggingface_hub.hf_hub_download(dataset_repo,
|
53 |
+
name,
|
54 |
+
repo_type='dataset',
|
55 |
+
use_auth_token=TOKEN)
|
56 |
+
with tarfile.open(path) as f:
|
57 |
+
f.extractall(image_dir.as_posix())
|
58 |
+
return sorted(image_dir.rglob('*.jpg'))
|
59 |
+
|
60 |
+
|
61 |
+
def run(image: np.ndarray, model_selection: int,
|
62 |
+
min_detection_confidence: float) -> np.ndarray:
|
63 |
+
with mp_face_detection.FaceDetection(
|
64 |
+
model_selection=model_selection,
|
65 |
+
min_detection_confidence=min_detection_confidence
|
66 |
+
) as face_detection:
|
67 |
+
results = face_detection.process(image)
|
68 |
+
|
69 |
+
res = image[:, :, ::-1].copy()
|
70 |
+
if results.detections is not None:
|
71 |
+
for detection in results.detections:
|
72 |
+
mp_drawing.draw_detection(res, detection)
|
73 |
+
return res[:, :, ::-1]
|
74 |
+
|
75 |
+
|
76 |
+
def main():
|
77 |
+
gr.close_all()
|
78 |
+
|
79 |
+
args = parse_args()
|
80 |
+
|
81 |
+
model_types = [
|
82 |
+
'Short-range model (best for faces within 2 meters)',
|
83 |
+
'Full-range model (best for faces within 5 meters)',
|
84 |
+
]
|
85 |
+
|
86 |
+
image_paths = load_sample_images()
|
87 |
+
examples = [[path.as_posix(), model_types[0], 0.5] for path in image_paths]
|
88 |
+
|
89 |
+
gr.Interface(
|
90 |
+
run,
|
91 |
+
[
|
92 |
+
gr.inputs.Image(type='numpy', label='Input'),
|
93 |
+
gr.inputs.Radio(model_types,
|
94 |
+
type='index',
|
95 |
+
default=model_types[0],
|
96 |
+
label='Model'),
|
97 |
+
gr.inputs.Slider(0,
|
98 |
+
1,
|
99 |
+
step=0.05,
|
100 |
+
default=0.5,
|
101 |
+
label='Minimum Detection Confidence'),
|
102 |
+
],
|
103 |
+
gr.outputs.Image(type='numpy', label='Output'),
|
104 |
+
examples=examples,
|
105 |
+
title=TITLE,
|
106 |
+
description=DESCRIPTION,
|
107 |
+
article=ARTICLE,
|
108 |
+
theme=args.theme,
|
109 |
+
allow_screenshot=args.allow_screenshot,
|
110 |
+
allow_flagging=args.allow_flagging,
|
111 |
+
live=args.live,
|
112 |
+
).launch(
|
113 |
+
enable_queue=args.enable_queue,
|
114 |
+
server_port=args.port,
|
115 |
+
share=args.share,
|
116 |
+
)
|
117 |
+
|
118 |
+
|
119 |
+
if __name__ == '__main__':
|
120 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
mediapipe==0.8.9.1
|
2 |
+
numpy==1.22.3
|
3 |
+
opencv-python-headless==4.5.5.64
|