File size: 1,524 Bytes
21ce843
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
025ec32
 
21ce843
 
025ec32
21ce843
 
 
025ec32
21ce843
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

import pathlib
import os
import cv2
import gradio as gr
import huggingface_hub
import numpy as np
import functools

from ultralytics import YOLO
from ultralytics.yolo.engine.results import Results

TITLE = 'Age and Gender Estimation with Transformers from Face and Body Images in the Wild'
DESCRIPTION = 'This is an official demo for https://github.com/...'

HF_TOKEN = os.getenv('HF_TOKEN')

def load_model():
    path = huggingface_hub.hf_hub_download('iitolstykh/demo_yolov8_detector',
                                           'yolov8x_person_face.pt',
                                           use_auth_token=HF_TOKEN)
    yolo = YOLO(path)
    yolo.fuse()

    return yolo

def detect(image: np.ndarray, detector: YOLO) -> np.ndarray:
    # input is rgb image, output must be rgb too

    detector_kwargs = {'conf': 0.5, 'iou': 0.5, 'half': False, 'verbose': False}

    image = image[:, :, ::-1]  # RGB -> BGR
    results: Results = detector.predict(image, **detector_kwargs)[0]
    out_im = results.plot()

    return out_im[:, :, ::-1]  # BGR -> RGB


detector = load_model()

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

func = functools.partial(detect, detector=detector)

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