File size: 1,707 Bytes
21db550
 
 
 
 
a968fd4
21db550
 
 
a968fd4
 
 
 
21db550
 
 
 
 
 
 
2343867
7c19286
21db550
 
 
 
 
 
 
 
 
 
 
 
 
a968fd4
 
 
 
21db550
a968fd4
21db550
 
 
2343867
 
 
 
21db550
2343867
 
21db550
2343867
 
 
 
 
 
 
 
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
#!/usr/bin/env python

from __future__ import annotations

import functools
import os
import pathlib
import subprocess

if os.environ.get('SYSTEM') == 'spaces':
    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.62'.split())

import cv2
import face_alignment
import gradio as gr
import numpy as np
import torch

TITLE = 'face-alignment'
DESCRIPTION = 'This is an unofficial demo for https://github.com/1adrianb/face-alignment.'


def detect(
    image: np.ndarray,
    detector,
    device: torch.device,
) -> np.ndarray:
    preds = detector.get_landmarks(image)
    if len(preds) == 0:
        raise RuntimeError('No face was found')

    res = image.copy()
    for pts in preds:
        tl = pts.min(axis=0)
        br = pts.max(axis=0)
        size = (br - tl).max()
        radius = max(2, int(3 * size / 256))
        for pt in np.round(pts).astype(int):
            cv2.circle(res, tuple(pt), radius, (0, 255, 0), cv2.FILLED)
    return res


device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
detector = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D,
                                        device=device.type)
func = functools.partial(detect, detector=detector, device=device)

image_paths = sorted(pathlib.Path('images').glob('*.jpg'))
examples = [[path.as_posix()] for path in image_paths]

gr.Interface(
    fn=func,
    inputs=gr.Image(label='Input', type='numpy'),
    outputs=gr.Image(label='Output', type='numpy'),
    examples=examples,
    title=TITLE,
    description=DESCRIPTION,
).launch(show_api=False)