Irina Tolstykh commited on
Commit
5f16544
1 Parent(s): 025ec32
Files changed (2) hide show
  1. .gitignore +2 -0
  2. app.py +37 -16
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio_cached_examples/
2
+ flagged/
app.py CHANGED
@@ -1,48 +1,69 @@
1
  #!/usr/bin/env python
2
 
 
 
 
 
 
 
 
 
 
 
3
  import pathlib
4
  import os
5
- import cv2
6
  import gradio as gr
7
  import huggingface_hub
8
  import numpy as np
9
  import functools
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- from ultralytics import YOLO
12
- from ultralytics.yolo.engine.results import Results
13
 
14
  TITLE = 'Age and Gender Estimation with Transformers from Face and Body Images in the Wild'
15
  DESCRIPTION = 'This is an official demo for https://github.com/...'
16
 
17
  HF_TOKEN = os.getenv('HF_TOKEN')
18
 
19
- def load_model():
20
- path = huggingface_hub.hf_hub_download('iitolstykh/demo_yolov8_detector',
21
  'yolov8x_person_face.pt',
22
  use_auth_token=HF_TOKEN)
23
- yolo = YOLO(path)
24
- yolo.fuse()
25
 
26
- return yolo
 
 
27
 
28
- def detect(image: np.ndarray, detector: YOLO) -> np.ndarray:
29
- # input is rgb image, output must be rgb too
30
 
31
- detector_kwargs = {'conf': 0.5, 'iou': 0.5, 'half': False, 'verbose': False}
32
 
33
- image = image[:, :, ::-1] # RGB -> BGR
34
- results: Results = detector.predict(image, **detector_kwargs)[0]
35
- out_im = results.plot()
36
 
 
 
37
  return out_im[:, :, ::-1] # BGR -> RGB
38
 
39
 
40
- detector = load_model()
41
 
42
  image_dir = pathlib.Path('images')
43
  examples = [[path.as_posix()] for path in sorted(image_dir.glob('*.jpg'))]
44
 
45
- func = functools.partial(detect, detector=detector)
46
 
47
  gr.Interface(
48
  fn=func,
 
1
  #!/usr/bin/env python
2
 
3
+ import os
4
+ import shlex
5
+ import subprocess
6
+
7
+ if os.getenv('SYSTEM') == 'spaces':
8
+ GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
9
+ GITHUB_USER = os.getenv('GITHUB_USER')
10
+ git_repo = f"https://{GITHUB_TOKEN}@github.com/{GITHUB_USER}/xnet_demo.git"
11
+ subprocess.call(shlex.split(f'pip install git+{git_repo}'))
12
+
13
  import pathlib
14
  import os
 
15
  import gradio as gr
16
  import huggingface_hub
17
  import numpy as np
18
  import functools
19
+ from dataclasses import dataclass
20
+
21
+ from xnet.predictor import Predictor
22
+
23
+
24
+ @dataclass
25
+ class Cfg:
26
+ detector_weights: str
27
+ checkpoint: str
28
+ device: str = "cpu"
29
+ with_persons: bool = True
30
+ disable_faces: bool = False
31
+ draw: bool = True
32
 
 
 
33
 
34
  TITLE = 'Age and Gender Estimation with Transformers from Face and Body Images in the Wild'
35
  DESCRIPTION = 'This is an official demo for https://github.com/...'
36
 
37
  HF_TOKEN = os.getenv('HF_TOKEN')
38
 
39
+ def load_models():
40
+ detector_path = huggingface_hub.hf_hub_download('iitolstykh/demo_yolov8_detector',
41
  'yolov8x_person_face.pt',
42
  use_auth_token=HF_TOKEN)
 
 
43
 
44
+ age_gender_path = huggingface_hub.hf_hub_download('iitolstykh/demo_xnet_volo_cross',
45
+ 'checkpoint-377.pth.tar',
46
+ use_auth_token=HF_TOKEN)
47
 
48
+ predictor_cfg = Cfg(detector_path, age_gender_path)
49
+ predictor = Predictor(predictor_cfg)
50
 
51
+ return predictor
52
 
53
+ def detect(image: np.ndarray, predictor: Predictor) -> np.ndarray:
54
+ # input is rgb image, output must be rgb too
 
55
 
56
+ image = image[:, :, ::-1] # RGB -> BGR
57
+ detected_objects, out_im = predictor.recognize(image)
58
  return out_im[:, :, ::-1] # BGR -> RGB
59
 
60
 
61
+ predictor = load_models()
62
 
63
  image_dir = pathlib.Path('images')
64
  examples = [[path.as_posix()] for path in sorted(image_dir.glob('*.jpg'))]
65
 
66
+ func = functools.partial(detect, predictor=predictor)
67
 
68
  gr.Interface(
69
  fn=func,