Spaces:
Runtime error
Runtime error
File size: 6,085 Bytes
0795e9e 2263890 9bc4359 0795e9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
#!/usr/bin/env python
from __future__ import annotations
import argparse
import functools
import os
import pathlib
import subprocess
import sys
import urllib.request
if os.environ.get('SYSTEM') == 'spaces':
import mim
mim.install('mmcv-full==1.3.3', is_yes=True)
subprocess.call('pip uninstall -y opencv-python'.split())
subprocess.call('pip uninstall -y opencv-python-headless'.split())
subprocess.call('pip install opencv-python-headless==4.5.5.64'.split())
subprocess.call('pip install terminaltables==3.1.0'.split())
subprocess.call('pip install mmpycocotools==12.0.3'.split())
subprocess.call('pip install insightface==0.6.2'.split())
import cv2
import gradio as gr
import huggingface_hub
import numpy as np
import torch
import torch.nn as nn
sys.path.insert(0, 'insightface/detection/scrfd')
from mmdet.apis import inference_detector, init_detector, show_result_pyplot
TITLE = 'insightface Face Detection (SCRFD)'
DESCRIPTION = 'This is a demo for https://github.com/deepinsight/insightface/tree/master/detection/scrfd.'
ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.insightface-scrfd" alt="visitor badge"/></center>'
TOKEN = os.environ['TOKEN']
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('--face-score-slider-step', type=float, default=0.05)
parser.add_argument('--face-score-threshold', type=float, default=0.3)
parser.add_argument('--device', type=str, default='cpu')
parser.add_argument('--theme', type=str)
parser.add_argument('--live', action='store_true')
parser.add_argument('--share', action='store_true')
parser.add_argument('--port', type=int)
parser.add_argument('--disable-queue',
dest='enable_queue',
action='store_false')
parser.add_argument('--allow-flagging', type=str, default='never')
return parser.parse_args()
def load_model(model_size: str, device) -> nn.Module:
ckpt_path = huggingface_hub.hf_hub_download(
'hysts/insightface',
f'models/scrfd_{model_size}/model.pth',
use_auth_token=TOKEN)
scrfd_dir = 'insightface/detection/scrfd'
config_path = f'{scrfd_dir}/configs/scrfd/scrfd_{model_size}.py'
model = init_detector(config_path, ckpt_path, device.type)
return model
def update_test_pipeline(model: nn.Module, mode: int):
cfg = model.cfg
pipelines = cfg.data.test.pipeline
for pipeline in pipelines:
if pipeline.type == 'MultiScaleFlipAug':
if mode == 0: #640 scale
pipeline.img_scale = (640, 640)
if hasattr(pipeline, 'scale_factor'):
del pipeline.scale_factor
elif mode == 1: #for single scale in other pages
pipeline.img_scale = (1100, 1650)
if hasattr(pipeline, 'scale_factor'):
del pipeline.scale_factor
elif mode == 2: #original scale
pipeline.img_scale = None
pipeline.scale_factor = 1.0
transforms = pipeline.transforms
for transform in transforms:
if transform.type == 'Pad':
if mode != 2:
transform.size = pipeline.img_scale
if hasattr(transform, 'size_divisor'):
del transform.size_divisor
else:
transform.size = None
transform.size_divisor = 32
def detect(image: np.ndarray, model_size: str, mode: int,
face_score_threshold: float,
detectors: dict[str, nn.Module]) -> np.ndarray:
model = detectors[model_size]
update_test_pipeline(model, mode)
# RGB -> BGR
image = image[:, :, ::-1]
preds = inference_detector(model, image)
boxes = preds[0]
res = image.copy()
for box in boxes:
box, score = box[:4], box[4]
if score < face_score_threshold:
continue
box = np.round(box).astype(int)
line_width = max(2, int(3 * (box[2:] - box[:2]).max() / 256))
cv2.rectangle(res, tuple(box[:2]), tuple(box[2:]), (0, 255, 0),
line_width)
res = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
return res
def main():
args = parse_args()
device = torch.device(args.device)
model_sizes = [
'500m',
'1g',
'2.5g',
'10g',
'34g',
]
detectors = {
model_size: load_model(model_size, device=device)
for model_size in model_sizes
}
modes = [
'(640, 640)',
'(1100, 1650)',
'original',
]
func = functools.partial(detect, detectors=detectors)
func = functools.update_wrapper(func, detect)
image_path = pathlib.Path('selfie.jpg')
if not image_path.exists():
url = 'https://raw.githubusercontent.com/peiyunh/tiny/master/data/demo/selfie.jpg'
urllib.request.urlretrieve(url, image_path)
examples = [[image_path.as_posix(), '10g', modes[0], 0.3]]
gr.Interface(
func,
[
gr.inputs.Image(type='numpy', label='Input'),
gr.inputs.Radio(
model_sizes, type='value', default='10g', label='Model'),
gr.inputs.Radio(
modes, type='index', default=modes[0], label='Mode'),
gr.inputs.Slider(0,
1,
step=args.face_score_slider_step,
default=args.face_score_threshold,
label='Face Score Threshold'),
],
gr.outputs.Image(type='numpy', label='Output'),
examples=examples,
title=TITLE,
description=DESCRIPTION,
article=ARTICLE,
theme=args.theme,
allow_flagging=args.allow_flagging,
live=args.live,
).launch(
enable_queue=args.enable_queue,
server_port=args.port,
share=args.share,
)
if __name__ == '__main__':
main()
|