narugo1992 commited on
Commit
89047b5
1 Parent(s): 17d3ab1

dev(narugo): add censor point detection

Browse files
Files changed (3) hide show
  1. app.py +26 -1
  2. censor.py +42 -0
  3. face.py +3 -3
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
 
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
@@ -18,7 +19,7 @@ if __name__ == '__main__':
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
 
@@ -82,6 +83,30 @@ if __name__ == '__main__':
82
  outputs=[gr_person_output_image],
83
  )
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  with gr.Tab('Manbits Detection'):
86
  with gr.Row():
87
  with gr.Column():
 
2
 
3
  import gradio as gr
4
 
5
+ from censor import _CENSOR_MODELS, _DEFAULT_CENSOR_MODEL, _gr_detect_censors
6
  from face import _FACE_MODELS, _DEFAULT_FACE_MODEL, _gr_detect_faces
7
  from head import _gr_detect_heads, _HEAD_MODELS, _DEFAULT_HEAD_MODEL
8
  from manbits import _MANBIT_MODELS, _DEFAULT_MANBIT_MODEL, _gr_detect_manbits
 
19
  gr_face_infer_size = gr.Slider(480, 960, value=640, step=32, label='Max Infer Size')
20
  with gr.Row():
21
  gr_face_iou_threshold = gr.Slider(0.0, 1.0, 0.7, label='IOU Threshold')
22
+ gr_face_score_threshold = gr.Slider(0.0, 1.0, 0.25, label='Score Threshold')
23
 
24
  gr_face_submit = gr.Button(value='Submit', variant='primary')
25
 
 
83
  outputs=[gr_person_output_image],
84
  )
85
 
86
+ with gr.Tab('Censor Point Detection'):
87
+ with gr.Row():
88
+ with gr.Column():
89
+ gr_censor_input_image = gr.Image(type='pil', label='Original Image')
90
+ gr_censor_model = gr.Dropdown(_CENSOR_MODELS, value=_DEFAULT_CENSOR_MODEL, label='Model')
91
+ gr_censor_infer_size = gr.Slider(480, 960, value=640, step=32, label='Max Infer Size')
92
+ with gr.Row():
93
+ gr_censor_iou_threshold = gr.Slider(0.0, 1.0, 0.5, label='IOU Threshold')
94
+ gr_censor_score_threshold = gr.Slider(0.0, 1.0, 0.35, label='Score Threshold')
95
+
96
+ gr_censor_submit = gr.Button(value='Submit', variant='primary')
97
+
98
+ with gr.Column():
99
+ gr_censor_output_image = gr.Image(type='pil', label="Labeled")
100
+
101
+ gr_censor_submit.click(
102
+ _gr_detect_censors,
103
+ inputs=[
104
+ gr_censor_input_image, gr_censor_model,
105
+ gr_censor_infer_size, gr_censor_score_threshold, gr_censor_iou_threshold,
106
+ ],
107
+ outputs=[gr_censor_output_image],
108
+ )
109
+
110
  with gr.Tab('Manbits Detection'):
111
  with gr.Row():
112
  with gr.Column():
censor.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ _CENSOR_MODELS = [
12
+ 'censor_detect_v0.7_s',
13
+ ]
14
+ _DEFAULT_CENSOR_MODEL = _CENSOR_MODELS[0]
15
+
16
+
17
+ @lru_cache()
18
+ def _open_censor_detect_model(model_name):
19
+ return _open_onnx_model(hf_hub_download(
20
+ f'deepghs/anime_censor_detection',
21
+ f'{model_name}/model.onnx'
22
+ ))
23
+
24
+
25
+ _LABELS = ['nipple_f', 'penis', 'pussy']
26
+
27
+
28
+ def detect_censors(image: ImageTyping, model_name: str, max_infer_size=640,
29
+ conf_threshold: float = 0.35, iou_threshold: float = 0.5) \
30
+ -> List[Tuple[Tuple[int, int, int, int], str, float]]:
31
+ image = load_image(image, mode='RGB')
32
+ new_image, old_size, new_size = _image_preprocess(image, max_infer_size)
33
+
34
+ data = rgb_encode(new_image)[None, ...]
35
+ output, = _open_censor_detect_model(model_name).run(['output0'], {'images': data})
36
+ return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, _LABELS)
37
+
38
+
39
+ def _gr_detect_censors(image: ImageTyping, model_name: str, max_infer_size=640,
40
+ conf_threshold: float = 0.35, iou_threshold: float = 0.5):
41
+ ret = detect_censors(image, model_name, max_infer_size, conf_threshold, iou_threshold)
42
+ return detection_visualize(image, ret, _LABELS)
face.py CHANGED
@@ -26,7 +26,7 @@ _DEFAULT_FACE_MODEL = _FACE_MODELS[0]
26
  def _open_face_detect_model(model_name):
27
  return _open_onnx_model(hf_hub_download(
28
  f'deepghs/anime_face_detection',
29
- f'{model_name}/model.onnx'
30
  ))
31
 
32
 
@@ -34,7 +34,7 @@ _LABELS = ['face']
34
 
35
 
36
  def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
37
- conf_threshold: float = 0.45, iou_threshold: float = 0.7) \
38
  -> List[Tuple[Tuple[int, int, int, int], str, float]]:
39
  image = load_image(image, mode='RGB')
40
  new_image, old_size, new_size = _image_preprocess(image, max_infer_size)
@@ -45,6 +45,6 @@ def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
45
 
46
 
47
  def _gr_detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
48
- conf_threshold: float = 0.45, iou_threshold: float = 0.7):
49
  ret = detect_faces(image, model_name, max_infer_size, conf_threshold, iou_threshold)
50
  return detection_visualize(image, ret, _LABELS)
 
26
  def _open_face_detect_model(model_name):
27
  return _open_onnx_model(hf_hub_download(
28
  f'deepghs/anime_face_detection',
29
+ f'{model_name}/model.onnx',
30
  ))
31
 
32
 
 
34
 
35
 
36
  def detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
37
+ conf_threshold: float = 0.25, iou_threshold: float = 0.7) \
38
  -> List[Tuple[Tuple[int, int, int, int], str, float]]:
39
  image = load_image(image, mode='RGB')
40
  new_image, old_size, new_size = _image_preprocess(image, max_infer_size)
 
45
 
46
 
47
  def _gr_detect_faces(image: ImageTyping, model_name: str, max_infer_size=640,
48
+ conf_threshold: float = 0.25, iou_threshold: float = 0.7):
49
  ret = detect_faces(image, model_name, max_infer_size, conf_threshold, iou_threshold)
50
  return detection_visualize(image, ret, _LABELS)