narugo commited on
Commit
595be82
β€’
1 Parent(s): 03c2334

dev(narugo): code upgrade

Browse files
Files changed (6) hide show
  1. README.md +1 -1
  2. app2.py +19 -0
  3. detection/__init__.py +2 -0
  4. detection/base.py +117 -0
  5. detection/eyes.py +33 -0
  6. requirements.txt +3 -3
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸ“ˆ
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 3.18.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app2.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+
5
+ from detection import EyesDetection
6
+
7
+ _GLOBAL_CSS = """
8
+ .limit-height {
9
+ max-height: 55vh;
10
+ }
11
+ """
12
+
13
+ if __name__ == '__main__':
14
+ with gr.Blocks(css=_GLOBAL_CSS) as demo:
15
+ with gr.Tabs():
16
+ with gr.Tab('Eyes Detection'):
17
+ EyesDetection().make_ui()
18
+
19
+ demo.queue(os.cpu_count()).launch()
detection/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ from .base import ObjectDetection, DeepGHSObjectDetection
2
+ from .eyes import EyesDetection
detection/base.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os.path
2
+ from functools import lru_cache
3
+ from typing import List, Tuple
4
+
5
+ import gradio as gr
6
+ from hbutils.color import rnd_colors
7
+ from hfutils.operate import get_hf_fs
8
+ from hfutils.utils import hf_fs_path, parse_hf_fs_path
9
+ from imgutils.data import ImageTyping
10
+
11
+
12
+ class ObjectDetection:
13
+ @lru_cache()
14
+ def get_default_model(self) -> str:
15
+ return self._get_default_model()
16
+
17
+ def _get_default_model(self) -> str:
18
+ raise NotImplementedError
19
+
20
+ @lru_cache()
21
+ def list_models(self) -> List[str]:
22
+ return self._list_models()
23
+
24
+ def _list_models(self) -> List[str]:
25
+ raise NotImplementedError
26
+
27
+ @lru_cache()
28
+ def get_default_iou_and_score(self, model_name: str) -> Tuple[float, float]:
29
+ return self._get_default_iou_and_score(model_name)
30
+
31
+ def _get_default_iou_and_score(self, model_name: str) -> Tuple[float, float]:
32
+ raise NotImplementedError
33
+
34
+ @lru_cache()
35
+ def get_labels(self, model_name: str) -> List[str]:
36
+ return self._get_labels(model_name)
37
+
38
+ def _get_labels(self, model_name: str) -> List[str]:
39
+ raise NotImplementedError
40
+
41
+ def detect(self, image: ImageTyping, model_name: str,
42
+ iou_threshold: float = 0.7, score_threshold: float = 0.25) \
43
+ -> List[Tuple[Tuple[float, float, float, float], str, float]]:
44
+ raise NotImplementedError
45
+
46
+ def _gr_detect(self, image: ImageTyping, model_name: str,
47
+ iou_threshold: float = 0.7, score_threshold: float = 0.25) \
48
+ -> gr.AnnotatedImage:
49
+ labels = self.get_labels(model_name=model_name)
50
+ _colors = list(map(str, rnd_colors(len(labels))))
51
+ _color_map = dict(zip(labels, _colors))
52
+ return gr.AnnotatedImage(
53
+ value=(image, [
54
+ (bbox, label) for bbox, label, _ in
55
+ self.detect(image, model_name, iou_threshold, score_threshold)
56
+ ]),
57
+ color_map=_color_map,
58
+ label='Labeled',
59
+ )
60
+
61
+ def make_ui(self):
62
+ with gr.Row():
63
+ with gr.Column():
64
+ default_model_name = self.get_default_model()
65
+ model_list = self.list_models()
66
+ gr_input_image = gr.Image(type='pil', label='Original Image')
67
+ gr_model = gr.Dropdown(model_list, value=default_model_name, label='Model')
68
+ with gr.Row():
69
+ iou, score = self.get_default_iou_and_score(default_model_name)
70
+ gr_iou_threshold = gr.Slider(0.0, 1.0, iou, label='IOU Threshold')
71
+ gr_score_threshold = gr.Slider(0.0, 1.0, score, label='Score Threshold')
72
+
73
+ gr_submit = gr.Button(value='Submit', variant='primary')
74
+
75
+ with gr.Column():
76
+ gr_output_image = gr.AnnotatedImage(label="Labeled")
77
+
78
+ gr_submit.click(
79
+ self._gr_detect,
80
+ inputs=[
81
+ gr_input_image,
82
+ gr_model,
83
+ gr_iou_threshold,
84
+ gr_score_threshold,
85
+ ],
86
+ outputs=[gr_output_image],
87
+ )
88
+
89
+
90
+ class DeepGHSObjectDetection(ObjectDetection):
91
+ def __init__(self, repo_id: str):
92
+ self._repo_id = repo_id
93
+
94
+ def _get_default_model(self) -> str:
95
+ raise NotImplementedError
96
+
97
+ def _list_models(self) -> List[str]:
98
+ hf_fs = get_hf_fs()
99
+ return [
100
+ os.path.dirname(parse_hf_fs_path(path).filename)
101
+ for path in hf_fs.glob(hf_fs_path(
102
+ repo_id=self._repo_id,
103
+ repo_type='model',
104
+ filename='*/model.onnx'
105
+ ))
106
+ ]
107
+
108
+ def _get_default_iou_and_score(self, model_name: str) -> Tuple[float, float]:
109
+ raise NotImplementedError
110
+
111
+ def _get_labels(self, model_name: str) -> List[str]:
112
+ raise NotImplementedError
113
+
114
+ def detect(self, image: ImageTyping, model_name: str,
115
+ iou_threshold: float = 0.7, score_threshold: float = 0.25) \
116
+ -> List[Tuple[Tuple[float, float, float, float], str, float]]:
117
+ raise NotImplementedError
detection/eyes.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from typing import List, Tuple
3
+
4
+ from imgutils.data import ImageTyping
5
+ from imgutils.detect.eye import detect_eyes, _LABELS
6
+
7
+ from .base import DeepGHSObjectDetection
8
+
9
+
10
+ def _parse_model_name(model_name: str):
11
+ matching = re.fullmatch(r'^eye_detect_(?P<version>[\s\S]+?)_(?P<level>[\s\S]+?)$', model_name)
12
+ return matching.group('version'), matching.group('level')
13
+
14
+
15
+ class EyesDetection(DeepGHSObjectDetection):
16
+ def __init__(self):
17
+ DeepGHSObjectDetection.__init__(self, repo_id='deepghs/anime_eye_detection')
18
+
19
+ def _get_default_model(self) -> str:
20
+ return 'eye_detect_v1.0_s'
21
+
22
+ def _get_default_iou_and_score(self, model_name: str) -> Tuple[float, float]:
23
+ return 0.3, 0.3
24
+
25
+ def _get_labels(self, model_name: str) -> List[str]:
26
+ return _LABELS
27
+
28
+ def detect(self, image: ImageTyping, model_name: str,
29
+ iou_threshold: float = 0.7, score_threshold: float = 0.25) \
30
+ -> List[Tuple[Tuple[float, float, float, float], str, float]]:
31
+ version, level = _parse_model_name(model_name)
32
+ return detect_eyes(image, level=level, version=version,
33
+ conf_threshold=score_threshold, iou_threshold=iou_threshold)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==3.18.0
2
  numpy
3
  pillow
4
  onnxruntime
@@ -7,5 +7,5 @@ scikit-image
7
  pandas
8
  opencv-python>=4.6.0
9
  hbutils>=0.9.0
10
- dghs-imgutils>=0.0.1
11
- httpx==0.23.0
 
1
+ gradio>=4.44.0
2
  numpy
3
  pillow
4
  onnxruntime
 
7
  pandas
8
  opencv-python>=4.6.0
9
  hbutils>=0.9.0
10
+ dghs-imgutils>=0.5.0
11
+ httpx