Spaces:
Build error
Build error
Add Zero GPU Gradio app for anime face detection
Browse files- README.md +39 -6
- app.py +111 -0
- packages.txt +2 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,13 +1,46 @@
|
|
| 1 |
---
|
| 2 |
-
title: Anime Face Detector
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Anime Face Detector GPU
|
| 3 |
+
emoji: "\U0001F3AD"
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Anime Face Detector (GPU)
|
| 14 |
+
|
| 15 |
+
Detect anime faces and 28 facial landmarks using YOLOv3 + HRNetV2.
|
| 16 |
+
|
| 17 |
+
This Space uses **Zero GPU** for fast inference without constant GPU allocation costs.
|
| 18 |
+
|
| 19 |
+
## Features
|
| 20 |
+
|
| 21 |
+
- Face detection using YOLOv3
|
| 22 |
+
- 28 facial landmark detection using HRNetV2
|
| 23 |
+
- Adjustable confidence thresholds
|
| 24 |
+
|
| 25 |
+
## Usage
|
| 26 |
+
|
| 27 |
+
1. Upload an anime image
|
| 28 |
+
2. Adjust the face and landmark score thresholds
|
| 29 |
+
3. Click Submit to detect faces
|
| 30 |
+
|
| 31 |
+
## Links
|
| 32 |
+
|
| 33 |
+
- [Original Repository (hysts)](https://github.com/hysts/anime-face-detector)
|
| 34 |
+
- [Fork with GPU Docker (ayutaz)](https://github.com/ayutaz/anime-face-detector)
|
| 35 |
+
- [Docker Image (ghcr.io)](https://ghcr.io/ayutaz/anime-face-detector)
|
| 36 |
+
|
| 37 |
+
## Citation
|
| 38 |
+
|
| 39 |
+
```bibtex
|
| 40 |
+
@misc{anime-face-detector,
|
| 41 |
+
author = {hysts},
|
| 42 |
+
title = {Anime Face Detector},
|
| 43 |
+
year = {2021},
|
| 44 |
+
howpublished = {\url{https://github.com/hysts/anime-face-detector}}
|
| 45 |
+
}
|
| 46 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Anime Face Detector - Hugging Face Space with Zero GPU support"""
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# Install mmcv, mmdet, mmpose using mim before importing
|
| 7 |
+
def install_mmlab():
|
| 8 |
+
subprocess.run([sys.executable, '-m', 'pip', 'install', 'openmim'], check=True)
|
| 9 |
+
subprocess.run([sys.executable, '-m', 'mim', 'install', 'mmengine', 'mmcv', 'mmdet', 'mmpose'], check=True)
|
| 10 |
+
|
| 11 |
+
# Check if mmcv is installed
|
| 12 |
+
try:
|
| 13 |
+
import mmcv
|
| 14 |
+
except ImportError:
|
| 15 |
+
print('Installing OpenMMLab dependencies...')
|
| 16 |
+
install_mmlab()
|
| 17 |
+
|
| 18 |
+
import spaces
|
| 19 |
+
import gradio as gr
|
| 20 |
+
import cv2
|
| 21 |
+
import numpy as np
|
| 22 |
+
import PIL.Image
|
| 23 |
+
import torch
|
| 24 |
+
|
| 25 |
+
# Global detector (initialized on first GPU call)
|
| 26 |
+
detector = None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@spaces.GPU
|
| 30 |
+
def detect(image: np.ndarray, face_score_threshold: float, landmark_score_threshold: float) -> PIL.Image.Image:
|
| 31 |
+
"""Detect anime faces and landmarks in the image."""
|
| 32 |
+
global detector
|
| 33 |
+
|
| 34 |
+
# Lazy initialization on GPU
|
| 35 |
+
if detector is None:
|
| 36 |
+
from anime_face_detector import create_detector
|
| 37 |
+
detector = create_detector('yolov3', device='cuda:0')
|
| 38 |
+
|
| 39 |
+
# Convert RGB to BGR for OpenCV
|
| 40 |
+
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 41 |
+
|
| 42 |
+
# Run detection
|
| 43 |
+
preds = detector(image_bgr)
|
| 44 |
+
|
| 45 |
+
# Draw results
|
| 46 |
+
res = image_bgr.copy()
|
| 47 |
+
for pred in preds:
|
| 48 |
+
box = pred['bbox']
|
| 49 |
+
box, score = box[:4], box[4]
|
| 50 |
+
if score < face_score_threshold:
|
| 51 |
+
continue
|
| 52 |
+
box = np.round(box).astype(int)
|
| 53 |
+
|
| 54 |
+
# Line thickness based on face size
|
| 55 |
+
lt = max(2, int(3 * (box[2:] - box[:2]).max() / 256))
|
| 56 |
+
|
| 57 |
+
# Draw bounding box
|
| 58 |
+
cv2.rectangle(res, tuple(box[:2]), tuple(box[2:]), (0, 255, 0), lt)
|
| 59 |
+
|
| 60 |
+
# Draw confidence score
|
| 61 |
+
cv2.putText(res, f'{score * 100:.1f}%', (box[0], box[1] - 5),
|
| 62 |
+
cv2.FONT_HERSHEY_SIMPLEX, lt / 3, (255, 255, 255), thickness=max(lt // 2, 1))
|
| 63 |
+
|
| 64 |
+
# Draw keypoints
|
| 65 |
+
pred_pts = pred['keypoints']
|
| 66 |
+
for *pt, kpt_score in pred_pts:
|
| 67 |
+
if kpt_score < landmark_score_threshold:
|
| 68 |
+
color = (0, 255, 255) # Yellow for low confidence
|
| 69 |
+
else:
|
| 70 |
+
color = (0, 0, 255) # Red for high confidence
|
| 71 |
+
pt = tuple(np.round(pt).astype(int))
|
| 72 |
+
cv2.circle(res, pt, lt, color, cv2.FILLED)
|
| 73 |
+
|
| 74 |
+
# Convert BGR to RGB for output
|
| 75 |
+
res = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
|
| 76 |
+
return PIL.Image.fromarray(res)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Download sample image
|
| 80 |
+
def download_sample():
|
| 81 |
+
sample_path = 'input.jpg'
|
| 82 |
+
if not os.path.exists(sample_path):
|
| 83 |
+
torch.hub.download_url_to_file(
|
| 84 |
+
'https://raw.githubusercontent.com/hysts/anime-face-detector/main/assets/input.jpg',
|
| 85 |
+
sample_path
|
| 86 |
+
)
|
| 87 |
+
return sample_path
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# Create Gradio interface
|
| 91 |
+
sample_path = download_sample()
|
| 92 |
+
|
| 93 |
+
demo = gr.Interface(
|
| 94 |
+
fn=detect,
|
| 95 |
+
inputs=[
|
| 96 |
+
gr.Image(type='numpy', label='Input Image'),
|
| 97 |
+
gr.Slider(0, 1, step=0.05, value=0.5, label='Face Score Threshold'),
|
| 98 |
+
gr.Slider(0, 1, step=0.05, value=0.3, label='Landmark Score Threshold'),
|
| 99 |
+
],
|
| 100 |
+
outputs=gr.Image(type='pil', label='Output'),
|
| 101 |
+
title='Anime Face Detector (GPU)',
|
| 102 |
+
description='Detect anime faces and 28 facial landmarks using YOLOv3 + HRNetV2. Powered by Zero GPU.',
|
| 103 |
+
article='<a href="https://github.com/hysts/anime-face-detector">GitHub</a> | <a href="https://github.com/ayutaz/anime-face-detector">Fork with GPU Docker</a>',
|
| 104 |
+
examples=[
|
| 105 |
+
[sample_path, 0.5, 0.3],
|
| 106 |
+
],
|
| 107 |
+
cache_examples=False,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if __name__ == '__main__':
|
| 111 |
+
demo.launch()
|
packages.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
libgl1
|
| 2 |
+
libglib2.0-0
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
openmim
|
| 4 |
+
anime-face-detector
|
| 5 |
+
opencv-python-headless
|
| 6 |
+
gradio>=4.0.0
|
| 7 |
+
spaces
|