narugo1992 commited on
Commit
522af51
1 Parent(s): ebc873b

dev(narugo): use head models

Browse files
Files changed (3) hide show
  1. app.py +26 -1
  2. face.py +3 -3
  3. head.py +43 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import gradio as gr
4
 
5
  from face import _FACE_MODELS, _DEFAULT_FACE_MODEL, _gr_detect_faces
 
6
  from manbits import _MANBIT_MODELS, _DEFAULT_MANBIT_MODEL, _gr_detect_manbits
7
  from person import _PERSON_MODELS, _DEFAULT_PERSON_MODEL, _gr_detect_person
8
 
@@ -17,7 +18,7 @@ if __name__ == '__main__':
17
  gr_face_infer_size = gr.Slider(480, 960, value=640, step=32, label='Max Infer Size')
18
  with gr.Row():
19
  gr_face_iou_threshold = gr.Slider(0.0, 1.0, 0.7, label='IOU Threshold')
20
- gr_face_score_threshold = gr.Slider(0.0, 1.0, 0.25, label='Score Threshold')
21
 
22
  gr_face_submit = gr.Button(value='Submit', variant='primary')
23
 
@@ -33,6 +34,30 @@ if __name__ == '__main__':
33
  outputs=[gr_face_output_image],
34
  )
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  with gr.Tab('Person Detection'):
37
  with gr.Row():
38
  with gr.Column():
 
3
  import gradio as gr
4
 
5
  from face import _FACE_MODELS, _DEFAULT_FACE_MODEL, _gr_detect_faces
6
+ from head import _gr_detect_heads, _HEAD_MODELS, _DEFAULT_HEAD_MODEL
7
  from manbits import _MANBIT_MODELS, _DEFAULT_MANBIT_MODEL, _gr_detect_manbits
8
  from person import _PERSON_MODELS, _DEFAULT_PERSON_MODEL, _gr_detect_person
9
 
 
18
  gr_face_infer_size = gr.Slider(480, 960, value=640, step=32, label='Max Infer Size')
19
  with gr.Row():
20
  gr_face_iou_threshold = gr.Slider(0.0, 1.0, 0.7, label='IOU Threshold')
21
+ gr_face_score_threshold = gr.Slider(0.0, 1.0, 0.45, label='Score Threshold')
22
 
23
  gr_face_submit = gr.Button(value='Submit', variant='primary')
24
 
 
34
  outputs=[gr_face_output_image],
35
  )
36
 
37
+ with gr.Tab('Head Detection'):
38
+ with gr.Row():
39
+ with gr.Column():
40
+ gr_head_input_image = gr.Image(type='pil', label='Original Image')
41
+ gr_head_model = gr.Dropdown(_HEAD_MODELS, value=_DEFAULT_HEAD_MODEL, label='Model')
42
+ gr_head_infer_size = gr.Slider(480, 960, value=640, step=32, label='Max Infer Size')
43
+ with gr.Row():
44
+ gr_head_iou_threshold = gr.Slider(0.0, 1.0, 0.7, label='IOU Threshold')
45
+ gr_head_score_threshold = gr.Slider(0.0, 1.0, 0.25, label='Score Threshold')
46
+
47
+ gr_head_submit = gr.Button(value='Submit', variant='primary')
48
+
49
+ with gr.Column():
50
+ gr_head_output_image = gr.Image(type='pil', label="Labeled")
51
+
52
+ gr_head_submit.click(
53
+ _gr_detect_heads,
54
+ inputs=[
55
+ gr_head_input_image, gr_head_model,
56
+ gr_head_infer_size, gr_head_score_threshold, gr_head_iou_threshold,
57
+ ],
58
+ outputs=[gr_head_output_image],
59
+ )
60
+
61
  with gr.Tab('Person Detection'):
62
  with gr.Row():
63
  with gr.Column():
face.py CHANGED
@@ -23,11 +23,11 @@ def _open_face_detect_model(model_name):
23
  ))
24
 
25
 
26
- _LABELS = ['head']
27
 
28
 
29
  def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
30
- conf_threshold: float = 0.25, iou_threshold: float = 0.7) \
31
  -> List[Tuple[Tuple[int, int, int, int], str, float]]:
32
  image = load_image(image, mode='RGB')
33
  new_image, old_size, new_size = _image_preprocess(image, max_infer_size)
@@ -38,6 +38,6 @@ def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
38
 
39
 
40
  def _gr_detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
41
- conf_threshold: float = 0.25, iou_threshold: float = 0.7):
42
  ret = detect_faces(image, model_name, max_infer_size, conf_threshold, iou_threshold)
43
  return detection_visualize(image, ret, _LABELS)
 
23
  ))
24
 
25
 
26
+ _LABELS = ['face']
27
 
28
 
29
  def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
30
+ conf_threshold: float = 0.45, iou_threshold: float = 0.7) \
31
  -> List[Tuple[Tuple[int, int, int, int], str, float]]:
32
  image = load_image(image, mode='RGB')
33
  new_image, old_size, new_size = _image_preprocess(image, max_infer_size)
 
38
 
39
 
40
  def _gr_detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
41
+ conf_threshold: float = 0.45, iou_threshold: float = 0.7):
42
  ret = detect_faces(image, model_name, max_infer_size, conf_threshold, iou_threshold)
43
  return detection_visualize(image, ret, _LABELS)
head.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import lru_cache
2
+ from typing import List, Tuple
3
+
4
+ from huggingface_hub import hf_hub_download
5
+ from imgutils.data import ImageTyping, load_image, rgb_encode
6
+
7
+ from onnx_ import _open_onnx_model
8
+ from plot import detection_visualize
9
+ from yolo_ import _image_preprocess, _data_postprocess
10
+
11
+ _HEAD_MODELS = [
12
+ 'head_detect_best_n.onnx',
13
+ 'head_detect_best_s.onnx',
14
+ ]
15
+ _DEFAULT_HEAD_MODEL = _HEAD_MODELS[0]
16
+
17
+
18
+ @lru_cache()
19
+ def _open_head_detect_model(model_name):
20
+ return _open_onnx_model(hf_hub_download(
21
+ 'deepghs/imgutils-models',
22
+ f'head_detect/{model_name}'
23
+ ))
24
+
25
+
26
+ _LABELS = ['head']
27
+
28
+
29
+ def detect_heads(image: ImageTyping, model_name: str, max_infer_size=640,
30
+ conf_threshold: float = 0.45, iou_threshold: float = 0.7) \
31
+ -> List[Tuple[Tuple[int, int, int, int], str, float]]:
32
+ image = load_image(image, mode='RGB')
33
+ new_image, old_size, new_size = _image_preprocess(image, max_infer_size)
34
+
35
+ data = rgb_encode(new_image)[None, ...]
36
+ output, = _open_head_detect_model(model_name).run(['output0'], {'images': data})
37
+ return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, _LABELS)
38
+
39
+
40
+ def _gr_detect_heads(image: ImageTyping, model_name: str, max_infer_size=640,
41
+ conf_threshold: float = 0.45, iou_threshold: float = 0.7):
42
+ ret = detect_heads(image, model_name, max_infer_size, conf_threshold, iou_threshold)
43
+ return detection_visualize(image, ret, _LABELS)