taesiri hysts HF Staff commited on
Commit
277053a
·
0 Parent(s):

Duplicate from Gradio-Blocks/ViTPose

Browse files

Co-authored-by: hysts <hysts@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.jpg filter=lfs diff=lfs merge=lfs -text
2
+ *.tar filter=lfs diff=lfs merge=lfs -text
3
+ *.7z filter=lfs diff=lfs merge=lfs -text
4
+ *.arrow filter=lfs diff=lfs merge=lfs -text
5
+ *.bin filter=lfs diff=lfs merge=lfs -text
6
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
7
+ *.ftz filter=lfs diff=lfs merge=lfs -text
8
+ *.gz filter=lfs diff=lfs merge=lfs -text
9
+ *.h5 filter=lfs diff=lfs merge=lfs -text
10
+ *.joblib filter=lfs diff=lfs merge=lfs -text
11
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pb filter=lfs diff=lfs merge=lfs -text
18
+ *.pt filter=lfs diff=lfs merge=lfs -text
19
+ *.pth filter=lfs diff=lfs merge=lfs -text
20
+ *.rar filter=lfs diff=lfs merge=lfs -text
21
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
22
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
23
+ *.tflite filter=lfs diff=lfs merge=lfs -text
24
+ *.tgz filter=lfs diff=lfs merge=lfs -text
25
+ *.wasm filter=lfs diff=lfs merge=lfs -text
26
+ *.xz filter=lfs diff=lfs merge=lfs -text
27
+ *.zip filter=lfs diff=lfs merge=lfs -text
28
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
29
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ mmdet_configs/configs
.gitmodules ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [submodule "ViTPose"]
2
+ path = ViTPose
3
+ url = https://github.com/ViTAE-Transformer/ViTPose
.pre-commit-config.yaml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ exclude: ^(ViTPose/|mmdet_configs/configs/)
2
+ repos:
3
+ - repo: https://github.com/pre-commit/pre-commit-hooks
4
+ rev: v4.2.0
5
+ hooks:
6
+ - id: check-executables-have-shebangs
7
+ - id: check-json
8
+ - id: check-merge-conflict
9
+ - id: check-shebang-scripts-are-executable
10
+ - id: check-toml
11
+ - id: check-yaml
12
+ - id: double-quote-string-fixer
13
+ - id: end-of-file-fixer
14
+ - id: mixed-line-ending
15
+ args: ['--fix=lf']
16
+ - id: requirements-txt-fixer
17
+ - id: trailing-whitespace
18
+ - repo: https://github.com/myint/docformatter
19
+ rev: v1.4
20
+ hooks:
21
+ - id: docformatter
22
+ args: ['--in-place']
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.10.1
25
+ hooks:
26
+ - id: isort
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.812
29
+ hooks:
30
+ - id: mypy
31
+ args: ['--ignore-missing-imports']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
37
+ - repo: https://github.com/kynan/nbstripout
38
+ rev: 0.5.0
39
+ hooks:
40
+ - id: nbstripout
41
+ args: ['--extra-keys', 'metadata.interpreter metadata.kernelspec cell.metadata.pycharm']
42
+ - repo: https://github.com/nbQA-dev/nbQA
43
+ rev: 1.3.1
44
+ hooks:
45
+ - id: nbqa-isort
46
+ - id: nbqa-yapf
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ViTPose
3
+ emoji: 📊
4
+ colorFrom: yellow
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.21.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: Gradio-Blocks/ViTPose
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
ViTPose ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit 86eced957665e62fa728eea059d5fffb9b94d653
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import pathlib
7
+ import tarfile
8
+
9
+ import gradio as gr
10
+
11
+ from model import AppDetModel, AppPoseModel
12
+
13
+ DESCRIPTION = '# [ViTPose](https://github.com/ViTAE-Transformer/ViTPose)'
14
+
15
+
16
+ def set_example_image(example: list) -> dict:
17
+ return gr.Image.update(value=example[0])
18
+
19
+
20
+ def extract_tar() -> None:
21
+ if pathlib.Path('mmdet_configs/configs').exists():
22
+ return
23
+ with tarfile.open('mmdet_configs/configs.tar') as f:
24
+ f.extractall('mmdet_configs')
25
+
26
+
27
+ extract_tar()
28
+
29
+ det_model = AppDetModel()
30
+ pose_model = AppPoseModel()
31
+
32
+ with gr.Blocks(css='style.css') as demo:
33
+ gr.Markdown(DESCRIPTION)
34
+
35
+ with gr.Box():
36
+ gr.Markdown('## Step 1')
37
+ with gr.Row():
38
+ with gr.Column():
39
+ with gr.Row():
40
+ input_image = gr.Image(label='Input Image', type='numpy')
41
+ with gr.Row():
42
+ detector_name = gr.Dropdown(
43
+ label='Detector',
44
+ choices=list(det_model.MODEL_DICT.keys()),
45
+ value=det_model.model_name)
46
+ with gr.Row():
47
+ detect_button = gr.Button('Detect')
48
+ det_preds = gr.Variable()
49
+ with gr.Column():
50
+ with gr.Row():
51
+ detection_visualization = gr.Image(
52
+ label='Detection Result',
53
+ type='numpy',
54
+ elem_id='det-result')
55
+ with gr.Row():
56
+ vis_det_score_threshold = gr.Slider(
57
+ label='Visualization Score Threshold',
58
+ minimum=0,
59
+ maximum=1,
60
+ step=0.05,
61
+ value=0.5)
62
+ with gr.Row():
63
+ redraw_det_button = gr.Button(value='Redraw')
64
+
65
+ with gr.Row():
66
+ paths = sorted(pathlib.Path('images').rglob('*.jpg'))
67
+ example_images = gr.Examples(examples=[[path.as_posix()]
68
+ for path in paths],
69
+ inputs=input_image)
70
+
71
+ with gr.Box():
72
+ gr.Markdown('## Step 2')
73
+ with gr.Row():
74
+ with gr.Column():
75
+ with gr.Row():
76
+ pose_model_name = gr.Dropdown(
77
+ label='Pose Model',
78
+ choices=list(pose_model.MODEL_DICT.keys()),
79
+ value=pose_model.model_name)
80
+ det_score_threshold = gr.Slider(label='Box Score Threshold',
81
+ minimum=0,
82
+ maximum=1,
83
+ step=0.05,
84
+ value=0.5)
85
+ with gr.Row():
86
+ predict_button = gr.Button('Predict')
87
+ pose_preds = gr.Variable()
88
+ with gr.Column():
89
+ with gr.Row():
90
+ pose_visualization = gr.Image(label='Result',
91
+ type='numpy',
92
+ elem_id='pose-result')
93
+ with gr.Row():
94
+ vis_kpt_score_threshold = gr.Slider(
95
+ label='Visualization Score Threshold',
96
+ minimum=0,
97
+ maximum=1,
98
+ step=0.05,
99
+ value=0.3)
100
+ with gr.Row():
101
+ vis_dot_radius = gr.Slider(label='Dot Radius',
102
+ minimum=1,
103
+ maximum=10,
104
+ step=1,
105
+ value=4)
106
+ with gr.Row():
107
+ vis_line_thickness = gr.Slider(label='Line Thickness',
108
+ minimum=1,
109
+ maximum=10,
110
+ step=1,
111
+ value=2)
112
+ with gr.Row():
113
+ redraw_pose_button = gr.Button('Redraw')
114
+
115
+ detector_name.change(fn=det_model.set_model,
116
+ inputs=detector_name,
117
+ outputs=None)
118
+ detect_button.click(fn=det_model.run,
119
+ inputs=[
120
+ detector_name,
121
+ input_image,
122
+ vis_det_score_threshold,
123
+ ],
124
+ outputs=[
125
+ det_preds,
126
+ detection_visualization,
127
+ ])
128
+ redraw_det_button.click(fn=det_model.visualize_detection_results,
129
+ inputs=[
130
+ input_image,
131
+ det_preds,
132
+ vis_det_score_threshold,
133
+ ],
134
+ outputs=detection_visualization)
135
+
136
+ pose_model_name.change(fn=pose_model.set_model,
137
+ inputs=pose_model_name,
138
+ outputs=None)
139
+ predict_button.click(fn=pose_model.run,
140
+ inputs=[
141
+ pose_model_name,
142
+ input_image,
143
+ det_preds,
144
+ det_score_threshold,
145
+ vis_kpt_score_threshold,
146
+ vis_dot_radius,
147
+ vis_line_thickness,
148
+ ],
149
+ outputs=[
150
+ pose_preds,
151
+ pose_visualization,
152
+ ])
153
+ redraw_pose_button.click(fn=pose_model.visualize_pose_results,
154
+ inputs=[
155
+ input_image,
156
+ pose_preds,
157
+ vis_kpt_score_threshold,
158
+ vis_dot_radius,
159
+ vis_line_thickness,
160
+ ],
161
+ outputs=pose_visualization)
162
+
163
+ demo.queue(api_open=False).launch()
images/README.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ These images are from the following public domain:
2
+
3
+ - https://www.pexels.com/photo/women-in-active-wear-balancing-their-body-while-leaning-by-the-doorway-5770445/
4
+ - https://www.pexels.com/photo/woman-balancing-her-body-on-a-handstand-using-one-hand-5770708/
5
+ - https://www.pexels.com/photo/persons-in-black-shirt-and-pants-690598/
6
+ - https://www.pexels.com/photo/photo-of-woman-doing-a-ballet-dance-1164975/
7
+ - https://www.pexels.com/photo/beautiful-woman-in-a-red-dress-wearing-red-lipstick-7909580/
8
+ - https://www.pexels.com/photo/girl-in-red-jacket-riding-bicycle-5792907/
9
+ - https://www.pexels.com/photo/woman-wearing-a-white-gown-walking-on-grass-field-8574605/
images/pexels-cottonbro-5770445.jpg ADDED

Git LFS Details

  • SHA256: b4548cd4a16238f559a149670c6ad2606b3b2147c92e5a2a380dd12fd922f276
  • Pointer size: 131 Bytes
  • Size of remote file: 379 kB
images/pexels-cottonbro-5770708.jpg ADDED

Git LFS Details

  • SHA256: 951720e6bb6053756ef555e5fcae4b54927582c4974e5908ea1984a9f14d7843
  • Pointer size: 131 Bytes
  • Size of remote file: 478 kB
images/pexels-haste-leart-v-690598.jpg ADDED

Git LFS Details

  • SHA256: 90009cbaceb3c3802d0df460862434e446e5cfad7892986444146ce73a02f61c
  • Pointer size: 131 Bytes
  • Size of remote file: 329 kB
images/pexels-luis-gallegos-alvarez-1164975.jpg ADDED

Git LFS Details

  • SHA256: 05cb7605dbac48915eee1b6ef0de3aba386abb7ab06ef27d58c092df2c76a176
  • Pointer size: 131 Bytes
  • Size of remote file: 553 kB
images/pexels-victoria-borodinova-7909580.jpg ADDED

Git LFS Details

  • SHA256: c05ceaf9c468dd21d24977f2c50e3f3b9b1ba83474d93180f66496635216b573
  • Pointer size: 131 Bytes
  • Size of remote file: 279 kB
images/pexels-yan-krukov-5792907.jpg ADDED

Git LFS Details

  • SHA256: 0500121b9044cb1d4c7913e48ebe5e2374848d57d6a2905f3b7c9469f959f2fe
  • Pointer size: 131 Bytes
  • Size of remote file: 648 kB
images/pexels-лиза-медведева-8574605.jpg ADDED

Git LFS Details

  • SHA256: 85cf4db499f0c5b11397af648e66178a4e40e6d478f1e6b31ade35e225ff6ceb
  • Pointer size: 131 Bytes
  • Size of remote file: 816 kB
mmdet_configs/LICENSE ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Copyright 2018-2023 OpenMMLab. All rights reserved.
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
179
+
180
+ APPENDIX: How to apply the Apache License to your work.
181
+
182
+ To apply the Apache License to your work, attach the following
183
+ boilerplate notice, with the fields enclosed by brackets "[]"
184
+ replaced with your own identifying information. (Don't include
185
+ the brackets!) The text should be enclosed in the appropriate
186
+ comment syntax for the file format. We also recommend that a
187
+ file or class name and description of purpose be included on the
188
+ same "printed page" as the copyright notice for easier
189
+ identification within third-party archives.
190
+
191
+ Copyright 2018-2023 OpenMMLab.
192
+
193
+ Licensed under the Apache License, Version 2.0 (the "License");
194
+ you may not use this file except in compliance with the License.
195
+ You may obtain a copy of the License at
196
+
197
+ http://www.apache.org/licenses/LICENSE-2.0
198
+
199
+ Unless required by applicable law or agreed to in writing, software
200
+ distributed under the License is distributed on an "AS IS" BASIS,
201
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202
+ See the License for the specific language governing permissions and
203
+ limitations under the License.
mmdet_configs/README.md ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ `configs.tar` is a tarball of https://github.com/open-mmlab/mmdetection/tree/v2.24.1/configs.
2
+ The license file of the mmdetection is also included in this directory.
mmdet_configs/configs.tar ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d2091e07da6b74a6cd694e895b653485f7ce9d5d17738a415ca77a56940b989
3
+ size 3389440
model.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import pathlib
5
+ import shlex
6
+ import subprocess
7
+ import sys
8
+
9
+ if os.getenv('SYSTEM') == 'spaces':
10
+ import mim
11
+
12
+ mim.uninstall('mmcv-full', confirm_yes=True)
13
+ mim.install('mmcv-full==1.5.0', is_yes=True)
14
+
15
+ subprocess.run(shlex.split('pip uninstall -y opencv-python'))
16
+ subprocess.run(shlex.split('pip uninstall -y opencv-python-headless'))
17
+ subprocess.run(shlex.split('pip install opencv-python-headless==4.5.5.64'))
18
+
19
+ import huggingface_hub
20
+ import numpy as np
21
+ import torch
22
+ import torch.nn as nn
23
+
24
+ app_dir = pathlib.Path(__file__).parent
25
+ submodule_dir = app_dir / 'ViTPose'
26
+ sys.path.insert(0, submodule_dir.as_posix())
27
+
28
+ from mmdet.apis import inference_detector, init_detector
29
+ from mmpose.apis import (inference_top_down_pose_model, init_pose_model,
30
+ process_mmdet_results, vis_pose_result)
31
+
32
+ HF_TOKEN = os.getenv('HF_TOKEN')
33
+
34
+
35
+ class DetModel:
36
+ MODEL_DICT = {
37
+ 'YOLOX-tiny': {
38
+ 'config':
39
+ 'mmdet_configs/configs/yolox/yolox_tiny_8x8_300e_coco.py',
40
+ 'model':
41
+ 'https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_tiny_8x8_300e_coco/yolox_tiny_8x8_300e_coco_20211124_171234-b4047906.pth',
42
+ },
43
+ 'YOLOX-s': {
44
+ 'config':
45
+ 'mmdet_configs/configs/yolox/yolox_s_8x8_300e_coco.py',
46
+ 'model':
47
+ 'https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_s_8x8_300e_coco/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth',
48
+ },
49
+ 'YOLOX-l': {
50
+ 'config':
51
+ 'mmdet_configs/configs/yolox/yolox_l_8x8_300e_coco.py',
52
+ 'model':
53
+ 'https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_l_8x8_300e_coco/yolox_l_8x8_300e_coco_20211126_140236-d3bd2b23.pth',
54
+ },
55
+ 'YOLOX-x': {
56
+ 'config':
57
+ 'mmdet_configs/configs/yolox/yolox_x_8x8_300e_coco.py',
58
+ 'model':
59
+ 'https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_x_8x8_300e_coco/yolox_x_8x8_300e_coco_20211126_140254-1ef88d67.pth',
60
+ },
61
+ }
62
+
63
+ def __init__(self):
64
+ self.device = torch.device(
65
+ 'cuda:0' if torch.cuda.is_available() else 'cpu')
66
+ self._load_all_models_once()
67
+ self.model_name = 'YOLOX-l'
68
+ self.model = self._load_model(self.model_name)
69
+
70
+ def _load_all_models_once(self) -> None:
71
+ for name in self.MODEL_DICT:
72
+ self._load_model(name)
73
+
74
+ def _load_model(self, name: str) -> nn.Module:
75
+ dic = self.MODEL_DICT[name]
76
+ return init_detector(dic['config'], dic['model'], device=self.device)
77
+
78
+ def set_model(self, name: str) -> None:
79
+ if name == self.model_name:
80
+ return
81
+ self.model_name = name
82
+ self.model = self._load_model(name)
83
+
84
+ def detect_and_visualize(
85
+ self, image: np.ndarray,
86
+ score_threshold: float) -> tuple[list[np.ndarray], np.ndarray]:
87
+ out = self.detect(image)
88
+ vis = self.visualize_detection_results(image, out, score_threshold)
89
+ return out, vis
90
+
91
+ def detect(self, image: np.ndarray) -> list[np.ndarray]:
92
+ image = image[:, :, ::-1] # RGB -> BGR
93
+ out = inference_detector(self.model, image)
94
+ return out
95
+
96
+ def visualize_detection_results(
97
+ self,
98
+ image: np.ndarray,
99
+ detection_results: list[np.ndarray],
100
+ score_threshold: float = 0.3) -> np.ndarray:
101
+ person_det = [detection_results[0]] + [np.array([]).reshape(0, 5)] * 79
102
+
103
+ image = image[:, :, ::-1] # RGB -> BGR
104
+ vis = self.model.show_result(image,
105
+ person_det,
106
+ score_thr=score_threshold,
107
+ bbox_color=None,
108
+ text_color=(200, 200, 200),
109
+ mask_color=None)
110
+ return vis[:, :, ::-1] # BGR -> RGB
111
+
112
+
113
+ class AppDetModel(DetModel):
114
+ def run(self, model_name: str, image: np.ndarray,
115
+ score_threshold: float) -> tuple[list[np.ndarray], np.ndarray]:
116
+ self.set_model(model_name)
117
+ return self.detect_and_visualize(image, score_threshold)
118
+
119
+
120
+ class PoseModel:
121
+ MODEL_DICT = {
122
+ 'ViTPose-B (single-task train)': {
123
+ 'config':
124
+ 'ViTPose/configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/ViTPose_base_coco_256x192.py',
125
+ 'model': 'models/vitpose-b.pth',
126
+ },
127
+ 'ViTPose-L (single-task train)': {
128
+ 'config':
129
+ 'ViTPose/configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/ViTPose_large_coco_256x192.py',
130
+ 'model': 'models/vitpose-l.pth',
131
+ },
132
+ 'ViTPose-B (multi-task train, COCO)': {
133
+ 'config':
134
+ 'ViTPose/configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/ViTPose_base_coco_256x192.py',
135
+ 'model': 'models/vitpose-b-multi-coco.pth',
136
+ },
137
+ 'ViTPose-L (multi-task train, COCO)': {
138
+ 'config':
139
+ 'ViTPose/configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/ViTPose_large_coco_256x192.py',
140
+ 'model': 'models/vitpose-l-multi-coco.pth',
141
+ },
142
+ }
143
+
144
+ def __init__(self):
145
+ self.device = torch.device(
146
+ 'cuda:0' if torch.cuda.is_available() else 'cpu')
147
+ self.model_name = 'ViTPose-B (multi-task train, COCO)'
148
+ self.model = self._load_model(self.model_name)
149
+
150
+ def _load_all_models_once(self) -> None:
151
+ for name in self.MODEL_DICT:
152
+ self._load_model(name)
153
+
154
+ def _load_model(self, name: str) -> nn.Module:
155
+ dic = self.MODEL_DICT[name]
156
+ ckpt_path = huggingface_hub.hf_hub_download('hysts/ViTPose',
157
+ dic['model'],
158
+ use_auth_token=HF_TOKEN)
159
+ model = init_pose_model(dic['config'], ckpt_path, device=self.device)
160
+ return model
161
+
162
+ def set_model(self, name: str) -> None:
163
+ if name == self.model_name:
164
+ return
165
+ self.model_name = name
166
+ self.model = self._load_model(name)
167
+
168
+ def predict_pose_and_visualize(
169
+ self,
170
+ image: np.ndarray,
171
+ det_results: list[np.ndarray],
172
+ box_score_threshold: float,
173
+ kpt_score_threshold: float,
174
+ vis_dot_radius: int,
175
+ vis_line_thickness: int,
176
+ ) -> tuple[list[dict[str, np.ndarray]], np.ndarray]:
177
+ out = self.predict_pose(image, det_results, box_score_threshold)
178
+ vis = self.visualize_pose_results(image, out, kpt_score_threshold,
179
+ vis_dot_radius, vis_line_thickness)
180
+ return out, vis
181
+
182
+ def predict_pose(
183
+ self,
184
+ image: np.ndarray,
185
+ det_results: list[np.ndarray],
186
+ box_score_threshold: float = 0.5) -> list[dict[str, np.ndarray]]:
187
+ image = image[:, :, ::-1] # RGB -> BGR
188
+ person_results = process_mmdet_results(det_results, 1)
189
+ out, _ = inference_top_down_pose_model(self.model,
190
+ image,
191
+ person_results=person_results,
192
+ bbox_thr=box_score_threshold,
193
+ format='xyxy')
194
+ return out
195
+
196
+ def visualize_pose_results(self,
197
+ image: np.ndarray,
198
+ pose_results: list[np.ndarray],
199
+ kpt_score_threshold: float = 0.3,
200
+ vis_dot_radius: int = 4,
201
+ vis_line_thickness: int = 1) -> np.ndarray:
202
+ image = image[:, :, ::-1] # RGB -> BGR
203
+ vis = vis_pose_result(self.model,
204
+ image,
205
+ pose_results,
206
+ kpt_score_thr=kpt_score_threshold,
207
+ radius=vis_dot_radius,
208
+ thickness=vis_line_thickness)
209
+ return vis[:, :, ::-1] # BGR -> RGB
210
+
211
+
212
+ class AppPoseModel(PoseModel):
213
+ def run(
214
+ self, model_name: str, image: np.ndarray,
215
+ det_results: list[np.ndarray], box_score_threshold: float,
216
+ kpt_score_threshold: float, vis_dot_radius: int,
217
+ vis_line_thickness: int
218
+ ) -> tuple[list[dict[str, np.ndarray]], np.ndarray]:
219
+ self.set_model(model_name)
220
+ return self.predict_pose_and_visualize(image, det_results,
221
+ box_score_threshold,
222
+ kpt_score_threshold,
223
+ vis_dot_radius,
224
+ vis_line_thickness)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ mmcv-full==1.5.0
2
+ mmdet==2.24.1
3
+ mmpose==0.25.1
4
+ numpy==1.23.5
5
+ opencv-python-headless==4.5.5.64
6
+ openmim==0.1.5
7
+ timm==0.5.4
8
+ torch==1.11.0
9
+ torchvision==0.12.0
style.css ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+ div#det-result {
5
+ max-width: 600px;
6
+ max-height: 600px;
7
+ }
8
+ div#pose-result {
9
+ max-width: 600px;
10
+ max-height: 600px;
11
+ }
12
+ img#visitor-badge {
13
+ display: block;
14
+ margin: auto;
15
+ }