Spaces:
Running
on
Zero
Running
on
Zero
BoyuanJiang
commited on
Commit
·
a62eecb
1
Parent(s):
3aba7e2
Add application file
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- LICENSE +107 -0
- README.md +1 -1
- app.py +309 -0
- examples/garment/0.jpg +0 -0
- examples/garment/0012.jpg +0 -0
- examples/garment/0023.jpg +0 -0
- examples/garment/0047.jpg +0 -0
- examples/garment/0049.jpg +0 -0
- examples/garment/0317.jpg +0 -0
- examples/garment/0327.jpg +0 -0
- examples/garment/0329.jpg +0 -0
- examples/garment/0362.jpg +0 -0
- examples/garment/1.jpg +0 -0
- examples/garment/10.jpg +0 -0
- examples/garment/11.jpg +0 -0
- examples/garment/12.jpg +0 -0
- examples/garment/2.jpg +0 -0
- examples/garment/3.jpg +0 -0
- examples/garment/4.jpg +0 -0
- examples/garment/5.jpg +0 -0
- examples/garment/6.jpeg +0 -0
- examples/garment/7.jpg +0 -0
- examples/garment/8.jpg +0 -0
- examples/garment/9.png +0 -0
- examples/model/0.jpg +0 -0
- examples/model/0083.jpg +0 -0
- examples/model/0179.jpg +0 -0
- examples/model/0220.jpg +0 -0
- examples/model/0223.jpg +0 -0
- examples/model/0274.jpg +0 -0
- examples/model/0279.jpg +0 -0
- examples/model/0303.jpg +0 -0
- examples/model/0347.jpg +0 -0
- examples/model/1.jpg +0 -0
- examples/model/2.jpg +0 -0
- examples/model/3.png +0 -0
- examples/model/4.jpg +0 -0
- examples/model/5.jpg +0 -0
- examples/model/6.jpg +0 -0
- examples/model/7.jpg +0 -0
- examples/model/8.png +0 -0
- preprocess/dwpose/__init__.py +68 -0
- preprocess/dwpose/onnxdet.py +125 -0
- preprocess/dwpose/onnxpose.py +360 -0
- preprocess/dwpose/util.py +297 -0
- preprocess/dwpose/wholebody.py +46 -0
- preprocess/humanparsing/datasets/__init__.py +0 -0
- preprocess/humanparsing/datasets/datasets.py +201 -0
- preprocess/humanparsing/datasets/simple_extractor_dataset.py +89 -0
- preprocess/humanparsing/datasets/target_generation.py +40 -0
LICENSE
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
|
2 |
+
|
3 |
+
Creative Commons Corporation ("Creative Commons") is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an "as-is" basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible.
|
4 |
+
|
5 |
+
Using Creative Commons Public Licenses
|
6 |
+
|
7 |
+
Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses.
|
8 |
+
|
9 |
+
Considerations for licensors: Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. More considerations for licensors : wiki.creativecommons.org/Considerations_for_licensors
|
10 |
+
|
11 |
+
Considerations for the public: By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensor's permission is not necessary for any reason–for example, because of any applicable exception or limitation to copyright–then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. More considerations for the public : wiki.creativecommons.org/Considerations_for_licensees
|
12 |
+
|
13 |
+
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
|
14 |
+
|
15 |
+
By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions.
|
16 |
+
|
17 |
+
Section 1 – Definitions.
|
18 |
+
|
19 |
+
a. Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image.
|
20 |
+
b. Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License.
|
21 |
+
c. BY-NC-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License.
|
22 |
+
d. Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights.
|
23 |
+
e. Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements.
|
24 |
+
f. Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material.
|
25 |
+
g. License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike.
|
26 |
+
h. Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License.
|
27 |
+
i. Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license.
|
28 |
+
j. Licensor means the individual(s) or entity(ies) granting rights under this Public License.
|
29 |
+
k. NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange.
|
30 |
+
l. Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them.
|
31 |
+
m. Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world.
|
32 |
+
n. You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning.
|
33 |
+
Section 2 – Scope.
|
34 |
+
|
35 |
+
a. License grant.
|
36 |
+
1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to:
|
37 |
+
A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and
|
38 |
+
B. produce, reproduce, and Share Adapted Material for NonCommercial purposes only.
|
39 |
+
2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions.
|
40 |
+
3. Term. The term of this Public License is specified in Section 6(a).
|
41 |
+
4. Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material.
|
42 |
+
5. Downstream recipients.
|
43 |
+
A. Offer from the Licensor – Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License.
|
44 |
+
B. Additional offer from the Licensor – Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter's License You apply.
|
45 |
+
C. No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material.
|
46 |
+
6. No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i).
|
47 |
+
b. Other rights.
|
48 |
+
1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise.
|
49 |
+
2. Patent and trademark rights are not licensed under this Public License.
|
50 |
+
3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes.
|
51 |
+
Section 3 – License Conditions.
|
52 |
+
|
53 |
+
Your exercise of the Licensed Rights is expressly made subject to the following conditions.
|
54 |
+
|
55 |
+
a. Attribution.
|
56 |
+
1. If You Share the Licensed Material (including in modified form), You must:
|
57 |
+
A. retain the following if it is supplied by the Licensor with the Licensed Material:
|
58 |
+
i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated);
|
59 |
+
ii. a copyright notice;
|
60 |
+
iii. a notice that refers to this Public License;
|
61 |
+
iv. a notice that refers to the disclaimer of warranties;
|
62 |
+
v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable;
|
63 |
+
|
64 |
+
B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and
|
65 |
+
C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License.
|
66 |
+
2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information.
|
67 |
+
3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable.
|
68 |
+
b. ShareAlike.In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply.
|
69 |
+
1. The Adapter's License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License.
|
70 |
+
2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material.
|
71 |
+
3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply.
|
72 |
+
Section 4 – Sui Generis Database Rights.
|
73 |
+
|
74 |
+
Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material:
|
75 |
+
|
76 |
+
a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only;
|
77 |
+
b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and
|
78 |
+
c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database.
|
79 |
+
For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights.
|
80 |
+
Section 5 – Disclaimer of Warranties and Limitation of Liability.
|
81 |
+
|
82 |
+
a. Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.
|
83 |
+
b. To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.
|
84 |
+
c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.
|
85 |
+
Section 6 – Term and Termination.
|
86 |
+
|
87 |
+
a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically.
|
88 |
+
b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates:
|
89 |
+
1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or
|
90 |
+
2. upon express reinstatement by the Licensor.
|
91 |
+
For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License.
|
92 |
+
|
93 |
+
c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License.
|
94 |
+
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License.
|
95 |
+
Section 7 – Other Terms and Conditions.
|
96 |
+
|
97 |
+
a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed.
|
98 |
+
b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License.
|
99 |
+
Section 8 – Interpretation.
|
100 |
+
|
101 |
+
a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License.
|
102 |
+
b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions.
|
103 |
+
c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor.
|
104 |
+
d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority.
|
105 |
+
Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the "Licensor." The text of the Creative Commons public licenses is dedicated to the public domain under the CC0 Public Domain Dedication. Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at creativecommons.org/policies, Creative Commons does not authorize the use of the trademark "Creative Commons" or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses.
|
106 |
+
|
107 |
+
Creative Commons may be contacted at creativecommons.org.
|
README.md
CHANGED
@@ -8,7 +8,7 @@ sdk_version: 5.11.0
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: cc-by-nc-sa-4.0
|
11 |
-
short_description:
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: cc-by-nc-sa-4.0
|
11 |
+
short_description: FitDiT is a high-fidelity virtual try-on model.
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import math
|
5 |
+
from preprocess.humanparsing.run_parsing import Parsing
|
6 |
+
from preprocess.dwpose import DWposeDetector
|
7 |
+
from transformers import CLIPVisionModelWithProjection, CLIPImageProcessor
|
8 |
+
import torch
|
9 |
+
import torch.nn as nn
|
10 |
+
from src.pose_guider import PoseGuider
|
11 |
+
from PIL import Image
|
12 |
+
from src.utils_mask import get_mask_location
|
13 |
+
import numpy as np
|
14 |
+
from src.pipeline_stable_diffusion_3_tryon import StableDiffusion3TryOnPipeline
|
15 |
+
from src.transformer_sd3_garm import SD3Transformer2DModel as SD3Transformer2DModel_Garm
|
16 |
+
from src.transformer_sd3_vton import SD3Transformer2DModel as SD3Transformer2DModel_Vton
|
17 |
+
import cv2
|
18 |
+
import random
|
19 |
+
from huggingface_hub import snapshot_download
|
20 |
+
|
21 |
+
example_path = os.path.join(os.path.dirname(__file__), 'examples')
|
22 |
+
|
23 |
+
access_token = os.getenv("HUGGING_FACE_HUB_TOKEN")
|
24 |
+
fitdit_repo = "BoyuanJiang/FitDiT"
|
25 |
+
repo_path = snapshot_download(repo_id=fitdit_repo)
|
26 |
+
|
27 |
+
class FitDiTGenerator:
|
28 |
+
def __init__(self, model_root, device="cuda", with_fp16=False):
|
29 |
+
weight_dtype = torch.float16 if with_fp16 else torch.bfloat16
|
30 |
+
transformer_garm = SD3Transformer2DModel_Garm.from_pretrained(os.path.join(model_root, "transformer_garm"), torch_dtype=weight_dtype)
|
31 |
+
transformer_vton = SD3Transformer2DModel_Vton.from_pretrained(os.path.join(model_root, "transformer_vton"), torch_dtype=weight_dtype)
|
32 |
+
pose_guider = PoseGuider(conditioning_embedding_channels=1536, conditioning_channels=3, block_out_channels=(32, 64, 256, 512))
|
33 |
+
pose_guider.load_state_dict(torch.load(os.path.join(model_root, "pose_guider", "diffusion_pytorch_model.bin")))
|
34 |
+
image_encoder_large = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14", torch_dtype=weight_dtype)
|
35 |
+
image_encoder_bigG = CLIPVisionModelWithProjection.from_pretrained("laion/CLIP-ViT-bigG-14-laion2B-39B-b160k", torch_dtype=weight_dtype)
|
36 |
+
pose_guider.to(device=device, dtype=weight_dtype)
|
37 |
+
image_encoder_large.to(device=device)
|
38 |
+
image_encoder_bigG.to(device=device)
|
39 |
+
self.pipeline = StableDiffusion3TryOnPipeline.from_pretrained(model_root, torch_dtype=weight_dtype, transformer_garm=transformer_garm, transformer_vton=transformer_vton, pose_guider=pose_guider, image_encoder_large=image_encoder_large, image_encoder_bigG=image_encoder_bigG)
|
40 |
+
self.pipeline.to(device)
|
41 |
+
self.dwprocessor = DWposeDetector(model_root=model_root, device=device)
|
42 |
+
self.parsing_model = Parsing(model_root=model_root, device=device)
|
43 |
+
|
44 |
+
@spaces.GPU
|
45 |
+
def generate_mask(self, vton_img, category, offset_top, offset_bottom, offset_left, offset_right):
|
46 |
+
with torch.inference_mode():
|
47 |
+
vton_img = Image.open(vton_img)
|
48 |
+
vton_img_det = resize_image(vton_img)
|
49 |
+
pose_image, keypoints, _, candidate = self.dwprocessor(np.array(vton_img_det)[:,:,::-1])
|
50 |
+
candidate[candidate<0]=0
|
51 |
+
candidate = candidate[0]
|
52 |
+
|
53 |
+
candidate[:, 0]*=vton_img_det.width
|
54 |
+
candidate[:, 1]*=vton_img_det.height
|
55 |
+
|
56 |
+
pose_image = pose_image[:,:,::-1] #rgb
|
57 |
+
pose_image = Image.fromarray(pose_image)
|
58 |
+
model_parse, _ = self.parsing_model(vton_img_det)
|
59 |
+
|
60 |
+
mask, mask_gray = get_mask_location(category, model_parse, \
|
61 |
+
candidate, model_parse.width, model_parse.height, \
|
62 |
+
offset_top, offset_bottom, offset_left, offset_right)
|
63 |
+
mask = mask.resize(vton_img.size)
|
64 |
+
mask_gray = mask_gray.resize(vton_img.size)
|
65 |
+
mask = mask.convert("L")
|
66 |
+
mask_gray = mask_gray.convert("L")
|
67 |
+
masked_vton_img = Image.composite(mask_gray, vton_img, mask)
|
68 |
+
|
69 |
+
im = {}
|
70 |
+
im['background'] = np.array(vton_img.convert("RGBA"))
|
71 |
+
im['layers'] = [np.concatenate((np.array(mask_gray.convert("RGB")), np.array(mask)[:,:,np.newaxis]),axis=2)]
|
72 |
+
im['composite'] = np.array(masked_vton_img.convert("RGBA"))
|
73 |
+
|
74 |
+
return im, pose_image
|
75 |
+
|
76 |
+
@spaces.GPU
|
77 |
+
def process(self, vton_img, garm_img, pre_mask, pose_image, n_steps, image_scale, seed, num_images_per_prompt, resolution):
|
78 |
+
assert resolution in ["768x1024", "1152x1536", "1536x2048"]
|
79 |
+
new_width, new_height = resolution.split("x")
|
80 |
+
new_width = int(new_width)
|
81 |
+
new_height = int(new_height)
|
82 |
+
with torch.inference_mode():
|
83 |
+
garm_img = Image.open(garm_img)
|
84 |
+
vton_img = Image.open(vton_img)
|
85 |
+
|
86 |
+
model_image_size = vton_img.size
|
87 |
+
garm_img, _, _ = pad_and_resize(garm_img, new_width=new_width, new_height=new_height)
|
88 |
+
vton_img, pad_w, pad_h = pad_and_resize(vton_img, new_width=new_width, new_height=new_height)
|
89 |
+
|
90 |
+
mask = pre_mask["layers"][0][:,:,3]
|
91 |
+
mask = Image.fromarray(mask)
|
92 |
+
mask, _, _ = pad_and_resize(mask, new_width=new_width, new_height=new_height, pad_color=(0,0,0))
|
93 |
+
mask = mask.convert("L")
|
94 |
+
pose_image = Image.fromarray(pose_image)
|
95 |
+
pose_image, _, _ = pad_and_resize(pose_image, new_width=new_width, new_height=new_height, pad_color=(0,0,0))
|
96 |
+
if seed==-1:
|
97 |
+
seed = random.randint(0, 2147483647)
|
98 |
+
res = self.pipeline(
|
99 |
+
height=new_height,
|
100 |
+
width=new_width,
|
101 |
+
guidance_scale=image_scale,
|
102 |
+
num_inference_steps=n_steps,
|
103 |
+
generator=torch.Generator("cpu").manual_seed(seed),
|
104 |
+
cloth_image=garm_img,
|
105 |
+
model_image=vton_img,
|
106 |
+
mask=mask,
|
107 |
+
pose_image=pose_image,
|
108 |
+
num_images_per_prompt=num_images_per_prompt
|
109 |
+
).images
|
110 |
+
for idx in range(len(res)):
|
111 |
+
res[idx] = unpad_and_resize(res[idx], pad_w, pad_h, model_image_size[0], model_image_size[1])
|
112 |
+
return res
|
113 |
+
|
114 |
+
|
115 |
+
def pad_and_resize(im, new_width=768, new_height=1024, pad_color=(255, 255, 255), mode=Image.LANCZOS):
|
116 |
+
old_width, old_height = im.size
|
117 |
+
|
118 |
+
ratio_w = new_width / old_width
|
119 |
+
ratio_h = new_height / old_height
|
120 |
+
if ratio_w < ratio_h:
|
121 |
+
new_size = (new_width, round(old_height * ratio_w))
|
122 |
+
else:
|
123 |
+
new_size = (round(old_width * ratio_h), new_height)
|
124 |
+
|
125 |
+
im_resized = im.resize(new_size, mode)
|
126 |
+
|
127 |
+
pad_w = math.ceil((new_width - im_resized.width) / 2)
|
128 |
+
pad_h = math.ceil((new_height - im_resized.height) / 2)
|
129 |
+
|
130 |
+
new_im = Image.new('RGB', (new_width, new_height), pad_color)
|
131 |
+
|
132 |
+
new_im.paste(im_resized, (pad_w, pad_h))
|
133 |
+
|
134 |
+
return new_im, pad_w, pad_h
|
135 |
+
|
136 |
+
def unpad_and_resize(padded_im, pad_w, pad_h, original_width, original_height):
|
137 |
+
width, height = padded_im.size
|
138 |
+
|
139 |
+
left = pad_w
|
140 |
+
top = pad_h
|
141 |
+
right = width - pad_w
|
142 |
+
bottom = height - pad_h
|
143 |
+
|
144 |
+
cropped_im = padded_im.crop((left, top, right, bottom))
|
145 |
+
|
146 |
+
resized_im = cropped_im.resize((original_width, original_height), Image.LANCZOS)
|
147 |
+
|
148 |
+
return resized_im
|
149 |
+
|
150 |
+
def resize_image(img, target_size=768):
|
151 |
+
width, height = img.size
|
152 |
+
|
153 |
+
if width < height:
|
154 |
+
scale = target_size / width
|
155 |
+
else:
|
156 |
+
scale = target_size / height
|
157 |
+
|
158 |
+
new_width = int(round(width * scale))
|
159 |
+
new_height = int(round(height * scale))
|
160 |
+
|
161 |
+
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
|
162 |
+
|
163 |
+
return resized_img
|
164 |
+
|
165 |
+
HEADER = """
|
166 |
+
<h1 style="text-align: center;"> FitDiT: Advancing the Authentic Garment Details for High-fidelity Virtual Try-on </h1>
|
167 |
+
<div style="display: flex; justify-content: center; align-items: center;">
|
168 |
+
<a href="https://github.com/BoyuanJiang/FitDiT" style="margin: 0 2px;">
|
169 |
+
<img src='https://img.shields.io/badge/GitHub-Repo-blue?style=flat&logo=GitHub' alt='GitHub'>
|
170 |
+
</a>
|
171 |
+
<a href="https://arxiv.org/abs/2411.10499" style="margin: 0 2px;">
|
172 |
+
<img src='https://img.shields.io/badge/arXiv-2411.10499-red?style=flat&logo=arXiv&logoColor=red' alt='arxiv'>
|
173 |
+
</a>
|
174 |
+
<a href="http://demo.fitdit.byjiang.com/" style="margin: 0 2px;">
|
175 |
+
<img src='https://img.shields.io/badge/Demo-Gradio-gold?style=flat&logo=Gradio&logoColor=red' alt='Demo'>
|
176 |
+
</a>
|
177 |
+
<a href='https://byjiang.com/FitDiT/' style="margin: 0 2px;">
|
178 |
+
<img src='https://img.shields.io/badge/Webpage-Project-silver?style=flat&logo=&logoColor=orange' alt='webpage'>
|
179 |
+
</a>
|
180 |
+
<a href="https://raw.githubusercontent.com/BoyuanJiang/FitDiT/refs/heads/main/LICENSE" style="margin: 0 2px;">
|
181 |
+
<img src='https://img.shields.io/badge/License-CC BY--NC--SA--4.0-lightgreen?style=flat&logo=Lisence' alt='License'>
|
182 |
+
</a>
|
183 |
+
</div>
|
184 |
+
<br>
|
185 |
+
FitDiT is designed for high-fidelity virtual try-on using Diffusion Transformers (DiT). It can only be used for <b>Non-commercial Use</b>.<br>
|
186 |
+
If you like our work, please star <a href="https://github.com/BoyuanJiang/FitDiT" style="color: blue; text-decoration: underline;">our github repository</a>.
|
187 |
+
"""
|
188 |
+
|
189 |
+
def create_demo(model_path, device, with_fp16):
|
190 |
+
generator = FitDiTGenerator(model_path, device, with_fp16)
|
191 |
+
with gr.Blocks(title="FitDiT") as demo:
|
192 |
+
gr.Markdown(HEADER)
|
193 |
+
with gr.Row():
|
194 |
+
with gr.Column():
|
195 |
+
vton_img = gr.Image(label="Model", sources=None, type="filepath", height=512)
|
196 |
+
|
197 |
+
with gr.Column():
|
198 |
+
garm_img = gr.Image(label="Garment", sources=None, type="filepath", height=512)
|
199 |
+
with gr.Row():
|
200 |
+
with gr.Column():
|
201 |
+
masked_vton_img = gr.ImageEditor(label="masked_vton_img", type="numpy", height=512, interactive=True, brush=gr.Brush(default_color="rgb(127, 127, 127)", colors=[
|
202 |
+
"rgb(128, 128, 128)"
|
203 |
+
]))
|
204 |
+
pose_image = gr.Image(label="pose_image", visible=False, interactive=False)
|
205 |
+
with gr.Column():
|
206 |
+
result_gallery = gr.Gallery(label="Output", elem_id="output-img", interactive=False, columns=[2], rows=[2], object_fit="contain", height="auto")
|
207 |
+
with gr.Row():
|
208 |
+
with gr.Column():
|
209 |
+
offset_top = gr.Slider(label="mask offset top", minimum=-200, maximum=200, step=1, value=0)
|
210 |
+
with gr.Column():
|
211 |
+
offset_bottom = gr.Slider(label="mask offset bottom", minimum=-200, maximum=200, step=1, value=0)
|
212 |
+
with gr.Column():
|
213 |
+
offset_left = gr.Slider(label="mask offset left", minimum=-200, maximum=200, step=1, value=0)
|
214 |
+
with gr.Column():
|
215 |
+
offset_right = gr.Slider(label="mask offset right", minimum=-200, maximum=200, step=1, value=0)
|
216 |
+
with gr.Row():
|
217 |
+
with gr.Column():
|
218 |
+
n_steps = gr.Slider(label="Steps", minimum=15, maximum=30, value=20, step=1)
|
219 |
+
with gr.Column():
|
220 |
+
image_scale = gr.Slider(label="Guidance scale", minimum=1.0, maximum=5.0, value=2, step=0.1)
|
221 |
+
with gr.Column():
|
222 |
+
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, value=-1)
|
223 |
+
with gr.Column():
|
224 |
+
num_images_per_prompt = gr.Slider(label="num_images", minimum=1, maximum=4, step=1, value=1)
|
225 |
+
|
226 |
+
with gr.Row():
|
227 |
+
with gr.Column():
|
228 |
+
example = gr.Examples(
|
229 |
+
label="Model (upper-body)",
|
230 |
+
inputs=vton_img,
|
231 |
+
examples_per_page=7,
|
232 |
+
examples=[
|
233 |
+
os.path.join(example_path, 'model/0279.jpg'),
|
234 |
+
os.path.join(example_path, 'model/0303.jpg'),
|
235 |
+
os.path.join(example_path, 'model/2.jpg'),
|
236 |
+
os.path.join(example_path, 'model/0083.jpg'),
|
237 |
+
])
|
238 |
+
example = gr.Examples(
|
239 |
+
label="Model (upper-body/lower-body)",
|
240 |
+
inputs=vton_img,
|
241 |
+
examples_per_page=7,
|
242 |
+
examples=[
|
243 |
+
os.path.join(example_path, 'model/0.jpg'),
|
244 |
+
os.path.join(example_path, 'model/0179.jpg'),
|
245 |
+
os.path.join(example_path, 'model/0223.jpg'),
|
246 |
+
os.path.join(example_path, 'model/0347.jpg'),
|
247 |
+
])
|
248 |
+
example = gr.Examples(
|
249 |
+
label="Model (dresses)",
|
250 |
+
inputs=vton_img,
|
251 |
+
examples_per_page=7,
|
252 |
+
examples=[
|
253 |
+
os.path.join(example_path, 'model/4.jpg'),
|
254 |
+
os.path.join(example_path, 'model/5.jpg'),
|
255 |
+
os.path.join(example_path, 'model/6.jpg'),
|
256 |
+
os.path.join(example_path, 'model/7.jpg'),
|
257 |
+
])
|
258 |
+
with gr.Column():
|
259 |
+
example = gr.Examples(
|
260 |
+
label="Garment (upper-body)",
|
261 |
+
inputs=garm_img,
|
262 |
+
examples_per_page=7,
|
263 |
+
examples=[
|
264 |
+
os.path.join(example_path, 'garment/12.jpg'),
|
265 |
+
os.path.join(example_path, 'garment/0012.jpg'),
|
266 |
+
os.path.join(example_path, 'garment/0047.jpg'),
|
267 |
+
os.path.join(example_path, 'garment/0049.jpg'),
|
268 |
+
])
|
269 |
+
example = gr.Examples(
|
270 |
+
label="Garment (lower-body)",
|
271 |
+
inputs=garm_img,
|
272 |
+
examples_per_page=7,
|
273 |
+
examples=[
|
274 |
+
os.path.join(example_path, 'garment/0317.jpg'),
|
275 |
+
os.path.join(example_path, 'garment/0327.jpg'),
|
276 |
+
os.path.join(example_path, 'garment/0329.jpg'),
|
277 |
+
os.path.join(example_path, 'garment/0362.jpg'),
|
278 |
+
])
|
279 |
+
example = gr.Examples(
|
280 |
+
label="Garment (dresses)",
|
281 |
+
inputs=garm_img,
|
282 |
+
examples_per_page=7,
|
283 |
+
examples=[
|
284 |
+
os.path.join(example_path, 'garment/8.jpg'),
|
285 |
+
os.path.join(example_path, 'garment/9.png'),
|
286 |
+
os.path.join(example_path, 'garment/10.jpg'),
|
287 |
+
os.path.join(example_path, 'garment/11.jpg'),
|
288 |
+
])
|
289 |
+
with gr.Column():
|
290 |
+
category = gr.Dropdown(label="Garment category", choices=["Upper-body", "Lower-body", "Dresses"], value="Upper-body")
|
291 |
+
resolution = gr.Dropdown(label="Try-on resolution", choices=["768x1024", "1152x1536", "1536x2048"], value="1152x1536")
|
292 |
+
with gr.Column():
|
293 |
+
run_mask_button = gr.Button(value="Step1: Run Mask")
|
294 |
+
run_button = gr.Button(value="Step2: Run Try-on")
|
295 |
+
|
296 |
+
ips1 = [vton_img, category, offset_top, offset_bottom, offset_left, offset_right]
|
297 |
+
ips2 = [vton_img, garm_img, masked_vton_img, pose_image, n_steps, image_scale, seed, num_images_per_prompt, resolution]
|
298 |
+
run_mask_button.click(fn=generator.generate_mask, inputs=ips1, outputs=[masked_vton_img, pose_image])
|
299 |
+
run_button.click(fn=generator.process, inputs=ips2, outputs=[result_gallery])
|
300 |
+
return demo
|
301 |
+
|
302 |
+
if __name__ == "__main__":
|
303 |
+
import argparse
|
304 |
+
parser = argparse.ArgumentParser(description="FitDiT")
|
305 |
+
parser.add_argument("--device", type=str, default="cuda:0", help="Device to use")
|
306 |
+
parser.add_argument("--fp16", action="store_true", help="Load model with fp16, default is bf16")
|
307 |
+
args = parser.parse_args()
|
308 |
+
demo = create_demo(repo_path, args.device, args.fp16)
|
309 |
+
demo.launch(share=True)
|
examples/garment/0.jpg
ADDED
examples/garment/0012.jpg
ADDED
examples/garment/0023.jpg
ADDED
examples/garment/0047.jpg
ADDED
examples/garment/0049.jpg
ADDED
examples/garment/0317.jpg
ADDED
examples/garment/0327.jpg
ADDED
examples/garment/0329.jpg
ADDED
examples/garment/0362.jpg
ADDED
examples/garment/1.jpg
ADDED
examples/garment/10.jpg
ADDED
examples/garment/11.jpg
ADDED
examples/garment/12.jpg
ADDED
examples/garment/2.jpg
ADDED
examples/garment/3.jpg
ADDED
examples/garment/4.jpg
ADDED
examples/garment/5.jpg
ADDED
examples/garment/6.jpeg
ADDED
examples/garment/7.jpg
ADDED
examples/garment/8.jpg
ADDED
examples/garment/9.png
ADDED
examples/model/0.jpg
ADDED
examples/model/0083.jpg
ADDED
examples/model/0179.jpg
ADDED
examples/model/0220.jpg
ADDED
examples/model/0223.jpg
ADDED
examples/model/0274.jpg
ADDED
examples/model/0279.jpg
ADDED
examples/model/0303.jpg
ADDED
examples/model/0347.jpg
ADDED
examples/model/1.jpg
ADDED
examples/model/2.jpg
ADDED
examples/model/3.png
ADDED
examples/model/4.jpg
ADDED
examples/model/5.jpg
ADDED
examples/model/6.jpg
ADDED
examples/model/7.jpg
ADDED
examples/model/8.png
ADDED
preprocess/dwpose/__init__.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Openpose
|
2 |
+
# Original from CMU https://github.com/CMU-Perceptual-Computing-Lab/openpose
|
3 |
+
# 2nd Edited by https://github.com/Hzzone/pytorch-openpose
|
4 |
+
# 3rd Edited by ControlNet
|
5 |
+
# 4th Edited by ControlNet (added face and correct hands)
|
6 |
+
|
7 |
+
import os
|
8 |
+
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
9 |
+
|
10 |
+
import torch
|
11 |
+
import numpy as np
|
12 |
+
from . import util
|
13 |
+
from .wholebody import Wholebody
|
14 |
+
|
15 |
+
def draw_pose(pose, H, W):
|
16 |
+
bodies = pose['bodies']
|
17 |
+
faces = pose['faces']
|
18 |
+
hands = pose['hands']
|
19 |
+
candidate = bodies['candidate']
|
20 |
+
subset = bodies['subset']
|
21 |
+
canvas = np.zeros(shape=(H, W, 3), dtype=np.uint8)
|
22 |
+
|
23 |
+
canvas = util.draw_bodypose(canvas, candidate, subset)
|
24 |
+
|
25 |
+
canvas = util.draw_handpose(canvas, hands)
|
26 |
+
|
27 |
+
canvas = util.draw_facepose(canvas, faces)
|
28 |
+
|
29 |
+
return canvas
|
30 |
+
|
31 |
+
|
32 |
+
class DWposeDetector:
|
33 |
+
def __init__(self, model_root, device):
|
34 |
+
|
35 |
+
self.pose_estimation = Wholebody(model_root, device)
|
36 |
+
|
37 |
+
def __call__(self, oriImg):
|
38 |
+
oriImg = oriImg.copy()
|
39 |
+
H, W, C = oriImg.shape
|
40 |
+
with torch.no_grad():
|
41 |
+
candidate, subset = self.pose_estimation(oriImg)
|
42 |
+
nums, keys, locs = candidate.shape
|
43 |
+
candidate[..., 0] /= float(W)
|
44 |
+
candidate[..., 1] /= float(H)
|
45 |
+
body = candidate[:,:18].copy()
|
46 |
+
body = body.reshape(nums*18, locs)
|
47 |
+
ori_score = subset[:,:18].copy()
|
48 |
+
score = subset[:,:18].copy()
|
49 |
+
for i in range(len(score)):
|
50 |
+
for j in range(len(score[i])):
|
51 |
+
if score[i][j] > 0.3:
|
52 |
+
score[i][j] = int(18*i+j)
|
53 |
+
else:
|
54 |
+
score[i][j] = -1
|
55 |
+
|
56 |
+
un_visible = subset<0.3
|
57 |
+
candidate[un_visible] = -1
|
58 |
+
|
59 |
+
foot = candidate[:,18:24]
|
60 |
+
|
61 |
+
faces = candidate[:,24:92]
|
62 |
+
|
63 |
+
hands = candidate[:,92:113]
|
64 |
+
hands = np.vstack([hands, candidate[:,113:]])
|
65 |
+
|
66 |
+
bodies = dict(candidate=body, subset=score)
|
67 |
+
pose = dict(bodies=bodies, hands=hands, faces=faces)
|
68 |
+
return draw_pose(pose, H, W), body, ori_score, candidate
|
preprocess/dwpose/onnxdet.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
import onnxruntime
|
5 |
+
|
6 |
+
def nms(boxes, scores, nms_thr):
|
7 |
+
"""Single class NMS implemented in Numpy."""
|
8 |
+
x1 = boxes[:, 0]
|
9 |
+
y1 = boxes[:, 1]
|
10 |
+
x2 = boxes[:, 2]
|
11 |
+
y2 = boxes[:, 3]
|
12 |
+
|
13 |
+
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
|
14 |
+
order = scores.argsort()[::-1]
|
15 |
+
|
16 |
+
keep = []
|
17 |
+
while order.size > 0:
|
18 |
+
i = order[0]
|
19 |
+
keep.append(i)
|
20 |
+
xx1 = np.maximum(x1[i], x1[order[1:]])
|
21 |
+
yy1 = np.maximum(y1[i], y1[order[1:]])
|
22 |
+
xx2 = np.minimum(x2[i], x2[order[1:]])
|
23 |
+
yy2 = np.minimum(y2[i], y2[order[1:]])
|
24 |
+
|
25 |
+
w = np.maximum(0.0, xx2 - xx1 + 1)
|
26 |
+
h = np.maximum(0.0, yy2 - yy1 + 1)
|
27 |
+
inter = w * h
|
28 |
+
ovr = inter / (areas[i] + areas[order[1:]] - inter)
|
29 |
+
|
30 |
+
inds = np.where(ovr <= nms_thr)[0]
|
31 |
+
order = order[inds + 1]
|
32 |
+
|
33 |
+
return keep
|
34 |
+
|
35 |
+
def multiclass_nms(boxes, scores, nms_thr, score_thr):
|
36 |
+
"""Multiclass NMS implemented in Numpy. Class-aware version."""
|
37 |
+
final_dets = []
|
38 |
+
num_classes = scores.shape[1]
|
39 |
+
for cls_ind in range(num_classes):
|
40 |
+
cls_scores = scores[:, cls_ind]
|
41 |
+
valid_score_mask = cls_scores > score_thr
|
42 |
+
if valid_score_mask.sum() == 0:
|
43 |
+
continue
|
44 |
+
else:
|
45 |
+
valid_scores = cls_scores[valid_score_mask]
|
46 |
+
valid_boxes = boxes[valid_score_mask]
|
47 |
+
keep = nms(valid_boxes, valid_scores, nms_thr)
|
48 |
+
if len(keep) > 0:
|
49 |
+
cls_inds = np.ones((len(keep), 1)) * cls_ind
|
50 |
+
dets = np.concatenate(
|
51 |
+
[valid_boxes[keep], valid_scores[keep, None], cls_inds], 1
|
52 |
+
)
|
53 |
+
final_dets.append(dets)
|
54 |
+
if len(final_dets) == 0:
|
55 |
+
return None
|
56 |
+
return np.concatenate(final_dets, 0)
|
57 |
+
|
58 |
+
def demo_postprocess(outputs, img_size, p6=False):
|
59 |
+
grids = []
|
60 |
+
expanded_strides = []
|
61 |
+
strides = [8, 16, 32] if not p6 else [8, 16, 32, 64]
|
62 |
+
|
63 |
+
hsizes = [img_size[0] // stride for stride in strides]
|
64 |
+
wsizes = [img_size[1] // stride for stride in strides]
|
65 |
+
|
66 |
+
for hsize, wsize, stride in zip(hsizes, wsizes, strides):
|
67 |
+
xv, yv = np.meshgrid(np.arange(wsize), np.arange(hsize))
|
68 |
+
grid = np.stack((xv, yv), 2).reshape(1, -1, 2)
|
69 |
+
grids.append(grid)
|
70 |
+
shape = grid.shape[:2]
|
71 |
+
expanded_strides.append(np.full((*shape, 1), stride))
|
72 |
+
|
73 |
+
grids = np.concatenate(grids, 1)
|
74 |
+
expanded_strides = np.concatenate(expanded_strides, 1)
|
75 |
+
outputs[..., :2] = (outputs[..., :2] + grids) * expanded_strides
|
76 |
+
outputs[..., 2:4] = np.exp(outputs[..., 2:4]) * expanded_strides
|
77 |
+
|
78 |
+
return outputs
|
79 |
+
|
80 |
+
def preprocess(img, input_size, swap=(2, 0, 1)):
|
81 |
+
if len(img.shape) == 3:
|
82 |
+
padded_img = np.ones((input_size[0], input_size[1], 3), dtype=np.uint8) * 114
|
83 |
+
else:
|
84 |
+
padded_img = np.ones(input_size, dtype=np.uint8) * 114
|
85 |
+
|
86 |
+
r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1])
|
87 |
+
resized_img = cv2.resize(
|
88 |
+
img,
|
89 |
+
(int(img.shape[1] * r), int(img.shape[0] * r)),
|
90 |
+
interpolation=cv2.INTER_LINEAR,
|
91 |
+
).astype(np.uint8)
|
92 |
+
padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img
|
93 |
+
|
94 |
+
padded_img = padded_img.transpose(swap)
|
95 |
+
padded_img = np.ascontiguousarray(padded_img, dtype=np.float32)
|
96 |
+
return padded_img, r
|
97 |
+
|
98 |
+
def inference_detector(session, oriImg):
|
99 |
+
input_shape = (640,640)
|
100 |
+
img, ratio = preprocess(oriImg, input_shape)
|
101 |
+
|
102 |
+
ort_inputs = {session.get_inputs()[0].name: img[None, :, :, :]}
|
103 |
+
output = session.run(None, ort_inputs)
|
104 |
+
predictions = demo_postprocess(output[0], input_shape)[0]
|
105 |
+
|
106 |
+
boxes = predictions[:, :4]
|
107 |
+
scores = predictions[:, 4:5] * predictions[:, 5:]
|
108 |
+
|
109 |
+
boxes_xyxy = np.ones_like(boxes)
|
110 |
+
boxes_xyxy[:, 0] = boxes[:, 0] - boxes[:, 2]/2.
|
111 |
+
boxes_xyxy[:, 1] = boxes[:, 1] - boxes[:, 3]/2.
|
112 |
+
boxes_xyxy[:, 2] = boxes[:, 0] + boxes[:, 2]/2.
|
113 |
+
boxes_xyxy[:, 3] = boxes[:, 1] + boxes[:, 3]/2.
|
114 |
+
boxes_xyxy /= ratio
|
115 |
+
dets = multiclass_nms(boxes_xyxy, scores, nms_thr=0.45, score_thr=0.1)
|
116 |
+
if dets is not None:
|
117 |
+
final_boxes, final_scores, final_cls_inds = dets[:, :4], dets[:, 4], dets[:, 5]
|
118 |
+
isscore = final_scores>0.3
|
119 |
+
iscat = final_cls_inds == 0
|
120 |
+
isbbox = [ i and j for (i, j) in zip(isscore, iscat)]
|
121 |
+
final_boxes = final_boxes[isbbox]
|
122 |
+
else:
|
123 |
+
final_boxes = np.array([])
|
124 |
+
|
125 |
+
return final_boxes
|
preprocess/dwpose/onnxpose.py
ADDED
@@ -0,0 +1,360 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Tuple
|
2 |
+
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import onnxruntime as ort
|
6 |
+
|
7 |
+
def preprocess(
|
8 |
+
img: np.ndarray, out_bbox, input_size: Tuple[int, int] = (192, 256)
|
9 |
+
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
10 |
+
"""Do preprocessing for RTMPose model inference.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
img (np.ndarray): Input image in shape.
|
14 |
+
input_size (tuple): Input image size in shape (w, h).
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
tuple:
|
18 |
+
- resized_img (np.ndarray): Preprocessed image.
|
19 |
+
- center (np.ndarray): Center of image.
|
20 |
+
- scale (np.ndarray): Scale of image.
|
21 |
+
"""
|
22 |
+
# get shape of image
|
23 |
+
img_shape = img.shape[:2]
|
24 |
+
out_img, out_center, out_scale = [], [], []
|
25 |
+
if len(out_bbox) == 0:
|
26 |
+
out_bbox = [[0, 0, img_shape[1], img_shape[0]]]
|
27 |
+
for i in range(len(out_bbox)):
|
28 |
+
x0 = out_bbox[i][0]
|
29 |
+
y0 = out_bbox[i][1]
|
30 |
+
x1 = out_bbox[i][2]
|
31 |
+
y1 = out_bbox[i][3]
|
32 |
+
bbox = np.array([x0, y0, x1, y1])
|
33 |
+
|
34 |
+
# get center and scale
|
35 |
+
center, scale = bbox_xyxy2cs(bbox, padding=1.25)
|
36 |
+
|
37 |
+
# do affine transformation
|
38 |
+
resized_img, scale = top_down_affine(input_size, scale, center, img)
|
39 |
+
|
40 |
+
# normalize image
|
41 |
+
mean = np.array([123.675, 116.28, 103.53])
|
42 |
+
std = np.array([58.395, 57.12, 57.375])
|
43 |
+
resized_img = (resized_img - mean) / std
|
44 |
+
|
45 |
+
out_img.append(resized_img)
|
46 |
+
out_center.append(center)
|
47 |
+
out_scale.append(scale)
|
48 |
+
|
49 |
+
return out_img, out_center, out_scale
|
50 |
+
|
51 |
+
|
52 |
+
def inference(sess: ort.InferenceSession, img: np.ndarray) -> np.ndarray:
|
53 |
+
"""Inference RTMPose model.
|
54 |
+
|
55 |
+
Args:
|
56 |
+
sess (ort.InferenceSession): ONNXRuntime session.
|
57 |
+
img (np.ndarray): Input image in shape.
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
outputs (np.ndarray): Output of RTMPose model.
|
61 |
+
"""
|
62 |
+
all_out = []
|
63 |
+
# build input
|
64 |
+
for i in range(len(img)):
|
65 |
+
input = [img[i].transpose(2, 0, 1)]
|
66 |
+
|
67 |
+
# build output
|
68 |
+
sess_input = {sess.get_inputs()[0].name: input}
|
69 |
+
sess_output = []
|
70 |
+
for out in sess.get_outputs():
|
71 |
+
sess_output.append(out.name)
|
72 |
+
|
73 |
+
# run model
|
74 |
+
outputs = sess.run(sess_output, sess_input)
|
75 |
+
all_out.append(outputs)
|
76 |
+
|
77 |
+
return all_out
|
78 |
+
|
79 |
+
|
80 |
+
def postprocess(outputs: List[np.ndarray],
|
81 |
+
model_input_size: Tuple[int, int],
|
82 |
+
center: Tuple[int, int],
|
83 |
+
scale: Tuple[int, int],
|
84 |
+
simcc_split_ratio: float = 2.0
|
85 |
+
) -> Tuple[np.ndarray, np.ndarray]:
|
86 |
+
"""Postprocess for RTMPose model output.
|
87 |
+
|
88 |
+
Args:
|
89 |
+
outputs (np.ndarray): Output of RTMPose model.
|
90 |
+
model_input_size (tuple): RTMPose model Input image size.
|
91 |
+
center (tuple): Center of bbox in shape (x, y).
|
92 |
+
scale (tuple): Scale of bbox in shape (w, h).
|
93 |
+
simcc_split_ratio (float): Split ratio of simcc.
|
94 |
+
|
95 |
+
Returns:
|
96 |
+
tuple:
|
97 |
+
- keypoints (np.ndarray): Rescaled keypoints.
|
98 |
+
- scores (np.ndarray): Model predict scores.
|
99 |
+
"""
|
100 |
+
all_key = []
|
101 |
+
all_score = []
|
102 |
+
for i in range(len(outputs)):
|
103 |
+
# use simcc to decode
|
104 |
+
simcc_x, simcc_y = outputs[i]
|
105 |
+
keypoints, scores = decode(simcc_x, simcc_y, simcc_split_ratio)
|
106 |
+
|
107 |
+
# rescale keypoints
|
108 |
+
keypoints = keypoints / model_input_size * scale[i] + center[i] - scale[i] / 2
|
109 |
+
all_key.append(keypoints[0])
|
110 |
+
all_score.append(scores[0])
|
111 |
+
|
112 |
+
return np.array(all_key), np.array(all_score)
|
113 |
+
|
114 |
+
|
115 |
+
def bbox_xyxy2cs(bbox: np.ndarray,
|
116 |
+
padding: float = 1.) -> Tuple[np.ndarray, np.ndarray]:
|
117 |
+
"""Transform the bbox format from (x,y,w,h) into (center, scale)
|
118 |
+
|
119 |
+
Args:
|
120 |
+
bbox (ndarray): Bounding box(es) in shape (4,) or (n, 4), formatted
|
121 |
+
as (left, top, right, bottom)
|
122 |
+
padding (float): BBox padding factor that will be multilied to scale.
|
123 |
+
Default: 1.0
|
124 |
+
|
125 |
+
Returns:
|
126 |
+
tuple: A tuple containing center and scale.
|
127 |
+
- np.ndarray[float32]: Center (x, y) of the bbox in shape (2,) or
|
128 |
+
(n, 2)
|
129 |
+
- np.ndarray[float32]: Scale (w, h) of the bbox in shape (2,) or
|
130 |
+
(n, 2)
|
131 |
+
"""
|
132 |
+
# convert single bbox from (4, ) to (1, 4)
|
133 |
+
dim = bbox.ndim
|
134 |
+
if dim == 1:
|
135 |
+
bbox = bbox[None, :]
|
136 |
+
|
137 |
+
# get bbox center and scale
|
138 |
+
x1, y1, x2, y2 = np.hsplit(bbox, [1, 2, 3])
|
139 |
+
center = np.hstack([x1 + x2, y1 + y2]) * 0.5
|
140 |
+
scale = np.hstack([x2 - x1, y2 - y1]) * padding
|
141 |
+
|
142 |
+
if dim == 1:
|
143 |
+
center = center[0]
|
144 |
+
scale = scale[0]
|
145 |
+
|
146 |
+
return center, scale
|
147 |
+
|
148 |
+
|
149 |
+
def _fix_aspect_ratio(bbox_scale: np.ndarray,
|
150 |
+
aspect_ratio: float) -> np.ndarray:
|
151 |
+
"""Extend the scale to match the given aspect ratio.
|
152 |
+
|
153 |
+
Args:
|
154 |
+
scale (np.ndarray): The image scale (w, h) in shape (2, )
|
155 |
+
aspect_ratio (float): The ratio of ``w/h``
|
156 |
+
|
157 |
+
Returns:
|
158 |
+
np.ndarray: The reshaped image scale in (2, )
|
159 |
+
"""
|
160 |
+
w, h = np.hsplit(bbox_scale, [1])
|
161 |
+
bbox_scale = np.where(w > h * aspect_ratio,
|
162 |
+
np.hstack([w, w / aspect_ratio]),
|
163 |
+
np.hstack([h * aspect_ratio, h]))
|
164 |
+
return bbox_scale
|
165 |
+
|
166 |
+
|
167 |
+
def _rotate_point(pt: np.ndarray, angle_rad: float) -> np.ndarray:
|
168 |
+
"""Rotate a point by an angle.
|
169 |
+
|
170 |
+
Args:
|
171 |
+
pt (np.ndarray): 2D point coordinates (x, y) in shape (2, )
|
172 |
+
angle_rad (float): rotation angle in radian
|
173 |
+
|
174 |
+
Returns:
|
175 |
+
np.ndarray: Rotated point in shape (2, )
|
176 |
+
"""
|
177 |
+
sn, cs = np.sin(angle_rad), np.cos(angle_rad)
|
178 |
+
rot_mat = np.array([[cs, -sn], [sn, cs]])
|
179 |
+
return rot_mat @ pt
|
180 |
+
|
181 |
+
|
182 |
+
def _get_3rd_point(a: np.ndarray, b: np.ndarray) -> np.ndarray:
|
183 |
+
"""To calculate the affine matrix, three pairs of points are required. This
|
184 |
+
function is used to get the 3rd point, given 2D points a & b.
|
185 |
+
|
186 |
+
The 3rd point is defined by rotating vector `a - b` by 90 degrees
|
187 |
+
anticlockwise, using b as the rotation center.
|
188 |
+
|
189 |
+
Args:
|
190 |
+
a (np.ndarray): The 1st point (x,y) in shape (2, )
|
191 |
+
b (np.ndarray): The 2nd point (x,y) in shape (2, )
|
192 |
+
|
193 |
+
Returns:
|
194 |
+
np.ndarray: The 3rd point.
|
195 |
+
"""
|
196 |
+
direction = a - b
|
197 |
+
c = b + np.r_[-direction[1], direction[0]]
|
198 |
+
return c
|
199 |
+
|
200 |
+
|
201 |
+
def get_warp_matrix(center: np.ndarray,
|
202 |
+
scale: np.ndarray,
|
203 |
+
rot: float,
|
204 |
+
output_size: Tuple[int, int],
|
205 |
+
shift: Tuple[float, float] = (0., 0.),
|
206 |
+
inv: bool = False) -> np.ndarray:
|
207 |
+
"""Calculate the affine transformation matrix that can warp the bbox area
|
208 |
+
in the input image to the output size.
|
209 |
+
|
210 |
+
Args:
|
211 |
+
center (np.ndarray[2, ]): Center of the bounding box (x, y).
|
212 |
+
scale (np.ndarray[2, ]): Scale of the bounding box
|
213 |
+
wrt [width, height].
|
214 |
+
rot (float): Rotation angle (degree).
|
215 |
+
output_size (np.ndarray[2, ] | list(2,)): Size of the
|
216 |
+
destination heatmaps.
|
217 |
+
shift (0-100%): Shift translation ratio wrt the width/height.
|
218 |
+
Default (0., 0.).
|
219 |
+
inv (bool): Option to inverse the affine transform direction.
|
220 |
+
(inv=False: src->dst or inv=True: dst->src)
|
221 |
+
|
222 |
+
Returns:
|
223 |
+
np.ndarray: A 2x3 transformation matrix
|
224 |
+
"""
|
225 |
+
shift = np.array(shift)
|
226 |
+
src_w = scale[0]
|
227 |
+
dst_w = output_size[0]
|
228 |
+
dst_h = output_size[1]
|
229 |
+
|
230 |
+
# compute transformation matrix
|
231 |
+
rot_rad = np.deg2rad(rot)
|
232 |
+
src_dir = _rotate_point(np.array([0., src_w * -0.5]), rot_rad)
|
233 |
+
dst_dir = np.array([0., dst_w * -0.5])
|
234 |
+
|
235 |
+
# get four corners of the src rectangle in the original image
|
236 |
+
src = np.zeros((3, 2), dtype=np.float32)
|
237 |
+
src[0, :] = center + scale * shift
|
238 |
+
src[1, :] = center + src_dir + scale * shift
|
239 |
+
src[2, :] = _get_3rd_point(src[0, :], src[1, :])
|
240 |
+
|
241 |
+
# get four corners of the dst rectangle in the input image
|
242 |
+
dst = np.zeros((3, 2), dtype=np.float32)
|
243 |
+
dst[0, :] = [dst_w * 0.5, dst_h * 0.5]
|
244 |
+
dst[1, :] = np.array([dst_w * 0.5, dst_h * 0.5]) + dst_dir
|
245 |
+
dst[2, :] = _get_3rd_point(dst[0, :], dst[1, :])
|
246 |
+
|
247 |
+
if inv:
|
248 |
+
warp_mat = cv2.getAffineTransform(np.float32(dst), np.float32(src))
|
249 |
+
else:
|
250 |
+
warp_mat = cv2.getAffineTransform(np.float32(src), np.float32(dst))
|
251 |
+
|
252 |
+
return warp_mat
|
253 |
+
|
254 |
+
|
255 |
+
def top_down_affine(input_size: dict, bbox_scale: dict, bbox_center: dict,
|
256 |
+
img: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
257 |
+
"""Get the bbox image as the model input by affine transform.
|
258 |
+
|
259 |
+
Args:
|
260 |
+
input_size (dict): The input size of the model.
|
261 |
+
bbox_scale (dict): The bbox scale of the img.
|
262 |
+
bbox_center (dict): The bbox center of the img.
|
263 |
+
img (np.ndarray): The original image.
|
264 |
+
|
265 |
+
Returns:
|
266 |
+
tuple: A tuple containing center and scale.
|
267 |
+
- np.ndarray[float32]: img after affine transform.
|
268 |
+
- np.ndarray[float32]: bbox scale after affine transform.
|
269 |
+
"""
|
270 |
+
w, h = input_size
|
271 |
+
warp_size = (int(w), int(h))
|
272 |
+
|
273 |
+
# reshape bbox to fixed aspect ratio
|
274 |
+
bbox_scale = _fix_aspect_ratio(bbox_scale, aspect_ratio=w / h)
|
275 |
+
|
276 |
+
# get the affine matrix
|
277 |
+
center = bbox_center
|
278 |
+
scale = bbox_scale
|
279 |
+
rot = 0
|
280 |
+
warp_mat = get_warp_matrix(center, scale, rot, output_size=(w, h))
|
281 |
+
|
282 |
+
# do affine transform
|
283 |
+
img = cv2.warpAffine(img, warp_mat, warp_size, flags=cv2.INTER_LINEAR)
|
284 |
+
|
285 |
+
return img, bbox_scale
|
286 |
+
|
287 |
+
|
288 |
+
def get_simcc_maximum(simcc_x: np.ndarray,
|
289 |
+
simcc_y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
290 |
+
"""Get maximum response location and value from simcc representations.
|
291 |
+
|
292 |
+
Note:
|
293 |
+
instance number: N
|
294 |
+
num_keypoints: K
|
295 |
+
heatmap height: H
|
296 |
+
heatmap width: W
|
297 |
+
|
298 |
+
Args:
|
299 |
+
simcc_x (np.ndarray): x-axis SimCC in shape (K, Wx) or (N, K, Wx)
|
300 |
+
simcc_y (np.ndarray): y-axis SimCC in shape (K, Wy) or (N, K, Wy)
|
301 |
+
|
302 |
+
Returns:
|
303 |
+
tuple:
|
304 |
+
- locs (np.ndarray): locations of maximum heatmap responses in shape
|
305 |
+
(K, 2) or (N, K, 2)
|
306 |
+
- vals (np.ndarray): values of maximum heatmap responses in shape
|
307 |
+
(K,) or (N, K)
|
308 |
+
"""
|
309 |
+
N, K, Wx = simcc_x.shape
|
310 |
+
simcc_x = simcc_x.reshape(N * K, -1)
|
311 |
+
simcc_y = simcc_y.reshape(N * K, -1)
|
312 |
+
|
313 |
+
# get maximum value locations
|
314 |
+
x_locs = np.argmax(simcc_x, axis=1)
|
315 |
+
y_locs = np.argmax(simcc_y, axis=1)
|
316 |
+
locs = np.stack((x_locs, y_locs), axis=-1).astype(np.float32)
|
317 |
+
max_val_x = np.amax(simcc_x, axis=1)
|
318 |
+
max_val_y = np.amax(simcc_y, axis=1)
|
319 |
+
|
320 |
+
# get maximum value across x and y axis
|
321 |
+
mask = max_val_x > max_val_y
|
322 |
+
max_val_x[mask] = max_val_y[mask]
|
323 |
+
vals = max_val_x
|
324 |
+
locs[vals <= 0.] = -1
|
325 |
+
|
326 |
+
# reshape
|
327 |
+
locs = locs.reshape(N, K, 2)
|
328 |
+
vals = vals.reshape(N, K)
|
329 |
+
|
330 |
+
return locs, vals
|
331 |
+
|
332 |
+
|
333 |
+
def decode(simcc_x: np.ndarray, simcc_y: np.ndarray,
|
334 |
+
simcc_split_ratio) -> Tuple[np.ndarray, np.ndarray]:
|
335 |
+
"""Modulate simcc distribution with Gaussian.
|
336 |
+
|
337 |
+
Args:
|
338 |
+
simcc_x (np.ndarray[K, Wx]): model predicted simcc in x.
|
339 |
+
simcc_y (np.ndarray[K, Wy]): model predicted simcc in y.
|
340 |
+
simcc_split_ratio (int): The split ratio of simcc.
|
341 |
+
|
342 |
+
Returns:
|
343 |
+
tuple: A tuple containing center and scale.
|
344 |
+
- np.ndarray[float32]: keypoints in shape (K, 2) or (n, K, 2)
|
345 |
+
- np.ndarray[float32]: scores in shape (K,) or (n, K)
|
346 |
+
"""
|
347 |
+
keypoints, scores = get_simcc_maximum(simcc_x, simcc_y)
|
348 |
+
keypoints /= simcc_split_ratio
|
349 |
+
|
350 |
+
return keypoints, scores
|
351 |
+
|
352 |
+
|
353 |
+
def inference_pose(session, out_bbox, oriImg):
|
354 |
+
h, w = session.get_inputs()[0].shape[2:]
|
355 |
+
model_input_size = (w, h)
|
356 |
+
resized_img, center, scale = preprocess(oriImg, out_bbox, model_input_size)
|
357 |
+
outputs = inference(session, resized_img)
|
358 |
+
keypoints, scores = postprocess(outputs, model_input_size, center, scale)
|
359 |
+
|
360 |
+
return keypoints, scores
|
preprocess/dwpose/util.py
ADDED
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib
|
4 |
+
import cv2
|
5 |
+
|
6 |
+
|
7 |
+
eps = 0.01
|
8 |
+
|
9 |
+
|
10 |
+
def smart_resize(x, s):
|
11 |
+
Ht, Wt = s
|
12 |
+
if x.ndim == 2:
|
13 |
+
Ho, Wo = x.shape
|
14 |
+
Co = 1
|
15 |
+
else:
|
16 |
+
Ho, Wo, Co = x.shape
|
17 |
+
if Co == 3 or Co == 1:
|
18 |
+
k = float(Ht + Wt) / float(Ho + Wo)
|
19 |
+
return cv2.resize(x, (int(Wt), int(Ht)), interpolation=cv2.INTER_AREA if k < 1 else cv2.INTER_LANCZOS4)
|
20 |
+
else:
|
21 |
+
return np.stack([smart_resize(x[:, :, i], s) for i in range(Co)], axis=2)
|
22 |
+
|
23 |
+
|
24 |
+
def smart_resize_k(x, fx, fy):
|
25 |
+
if x.ndim == 2:
|
26 |
+
Ho, Wo = x.shape
|
27 |
+
Co = 1
|
28 |
+
else:
|
29 |
+
Ho, Wo, Co = x.shape
|
30 |
+
Ht, Wt = Ho * fy, Wo * fx
|
31 |
+
if Co == 3 or Co == 1:
|
32 |
+
k = float(Ht + Wt) / float(Ho + Wo)
|
33 |
+
return cv2.resize(x, (int(Wt), int(Ht)), interpolation=cv2.INTER_AREA if k < 1 else cv2.INTER_LANCZOS4)
|
34 |
+
else:
|
35 |
+
return np.stack([smart_resize_k(x[:, :, i], fx, fy) for i in range(Co)], axis=2)
|
36 |
+
|
37 |
+
|
38 |
+
def padRightDownCorner(img, stride, padValue):
|
39 |
+
h = img.shape[0]
|
40 |
+
w = img.shape[1]
|
41 |
+
|
42 |
+
pad = 4 * [None]
|
43 |
+
pad[0] = 0 # up
|
44 |
+
pad[1] = 0 # left
|
45 |
+
pad[2] = 0 if (h % stride == 0) else stride - (h % stride) # down
|
46 |
+
pad[3] = 0 if (w % stride == 0) else stride - (w % stride) # right
|
47 |
+
|
48 |
+
img_padded = img
|
49 |
+
pad_up = np.tile(img_padded[0:1, :, :]*0 + padValue, (pad[0], 1, 1))
|
50 |
+
img_padded = np.concatenate((pad_up, img_padded), axis=0)
|
51 |
+
pad_left = np.tile(img_padded[:, 0:1, :]*0 + padValue, (1, pad[1], 1))
|
52 |
+
img_padded = np.concatenate((pad_left, img_padded), axis=1)
|
53 |
+
pad_down = np.tile(img_padded[-2:-1, :, :]*0 + padValue, (pad[2], 1, 1))
|
54 |
+
img_padded = np.concatenate((img_padded, pad_down), axis=0)
|
55 |
+
pad_right = np.tile(img_padded[:, -2:-1, :]*0 + padValue, (1, pad[3], 1))
|
56 |
+
img_padded = np.concatenate((img_padded, pad_right), axis=1)
|
57 |
+
|
58 |
+
return img_padded, pad
|
59 |
+
|
60 |
+
|
61 |
+
def transfer(model, model_weights):
|
62 |
+
transfered_model_weights = {}
|
63 |
+
for weights_name in model.state_dict().keys():
|
64 |
+
transfered_model_weights[weights_name] = model_weights['.'.join(weights_name.split('.')[1:])]
|
65 |
+
return transfered_model_weights
|
66 |
+
|
67 |
+
|
68 |
+
def draw_bodypose(canvas, candidate, subset):
|
69 |
+
H, W, C = canvas.shape
|
70 |
+
candidate = np.array(candidate)
|
71 |
+
subset = np.array(subset)
|
72 |
+
|
73 |
+
stickwidth = 4
|
74 |
+
|
75 |
+
limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10], \
|
76 |
+
[10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17], \
|
77 |
+
[1, 16], [16, 18], [3, 17], [6, 18]]
|
78 |
+
|
79 |
+
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \
|
80 |
+
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \
|
81 |
+
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
|
82 |
+
|
83 |
+
for i in range(17):
|
84 |
+
for n in range(len(subset)):
|
85 |
+
index = subset[n][np.array(limbSeq[i]) - 1]
|
86 |
+
if -1 in index:
|
87 |
+
continue
|
88 |
+
Y = candidate[index.astype(int), 0] * float(W)
|
89 |
+
X = candidate[index.astype(int), 1] * float(H)
|
90 |
+
mX = np.mean(X)
|
91 |
+
mY = np.mean(Y)
|
92 |
+
length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5
|
93 |
+
angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
|
94 |
+
polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1)
|
95 |
+
cv2.fillConvexPoly(canvas, polygon, colors[i])
|
96 |
+
|
97 |
+
canvas = (canvas * 0.6).astype(np.uint8)
|
98 |
+
|
99 |
+
for i in range(18):
|
100 |
+
for n in range(len(subset)):
|
101 |
+
index = int(subset[n][i])
|
102 |
+
if index == -1:
|
103 |
+
continue
|
104 |
+
x, y = candidate[index][0:2]
|
105 |
+
x = int(x * W)
|
106 |
+
y = int(y * H)
|
107 |
+
cv2.circle(canvas, (int(x), int(y)), 4, colors[i], thickness=-1)
|
108 |
+
|
109 |
+
return canvas
|
110 |
+
|
111 |
+
|
112 |
+
def draw_handpose(canvas, all_hand_peaks):
|
113 |
+
H, W, C = canvas.shape
|
114 |
+
|
115 |
+
edges = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9], [9, 10], \
|
116 |
+
[10, 11], [11, 12], [0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]
|
117 |
+
|
118 |
+
for peaks in all_hand_peaks:
|
119 |
+
peaks = np.array(peaks)
|
120 |
+
|
121 |
+
for ie, e in enumerate(edges):
|
122 |
+
x1, y1 = peaks[e[0]]
|
123 |
+
x2, y2 = peaks[e[1]]
|
124 |
+
x1 = int(x1 * W)
|
125 |
+
y1 = int(y1 * H)
|
126 |
+
x2 = int(x2 * W)
|
127 |
+
y2 = int(y2 * H)
|
128 |
+
if x1 > eps and y1 > eps and x2 > eps and y2 > eps:
|
129 |
+
cv2.line(canvas, (x1, y1), (x2, y2), matplotlib.colors.hsv_to_rgb([ie / float(len(edges)), 1.0, 1.0]) * 255, thickness=2)
|
130 |
+
|
131 |
+
for i, keyponit in enumerate(peaks):
|
132 |
+
x, y = keyponit
|
133 |
+
x = int(x * W)
|
134 |
+
y = int(y * H)
|
135 |
+
if x > eps and y > eps:
|
136 |
+
cv2.circle(canvas, (x, y), 4, (0, 0, 255), thickness=-1)
|
137 |
+
return canvas
|
138 |
+
|
139 |
+
|
140 |
+
def draw_facepose(canvas, all_lmks):
|
141 |
+
H, W, C = canvas.shape
|
142 |
+
for lmks in all_lmks:
|
143 |
+
lmks = np.array(lmks)
|
144 |
+
for lmk in lmks:
|
145 |
+
x, y = lmk
|
146 |
+
x = int(x * W)
|
147 |
+
y = int(y * H)
|
148 |
+
if x > eps and y > eps:
|
149 |
+
cv2.circle(canvas, (x, y), 3, (255, 255, 255), thickness=-1)
|
150 |
+
return canvas
|
151 |
+
|
152 |
+
|
153 |
+
# detect hand according to body pose keypoints
|
154 |
+
# please refer to https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/hand/handDetector.cpp
|
155 |
+
def handDetect(candidate, subset, oriImg):
|
156 |
+
# right hand: wrist 4, elbow 3, shoulder 2
|
157 |
+
# left hand: wrist 7, elbow 6, shoulder 5
|
158 |
+
ratioWristElbow = 0.33
|
159 |
+
detect_result = []
|
160 |
+
image_height, image_width = oriImg.shape[0:2]
|
161 |
+
for person in subset.astype(int):
|
162 |
+
# if any of three not detected
|
163 |
+
has_left = np.sum(person[[5, 6, 7]] == -1) == 0
|
164 |
+
has_right = np.sum(person[[2, 3, 4]] == -1) == 0
|
165 |
+
if not (has_left or has_right):
|
166 |
+
continue
|
167 |
+
hands = []
|
168 |
+
#left hand
|
169 |
+
if has_left:
|
170 |
+
left_shoulder_index, left_elbow_index, left_wrist_index = person[[5, 6, 7]]
|
171 |
+
x1, y1 = candidate[left_shoulder_index][:2]
|
172 |
+
x2, y2 = candidate[left_elbow_index][:2]
|
173 |
+
x3, y3 = candidate[left_wrist_index][:2]
|
174 |
+
hands.append([x1, y1, x2, y2, x3, y3, True])
|
175 |
+
# right hand
|
176 |
+
if has_right:
|
177 |
+
right_shoulder_index, right_elbow_index, right_wrist_index = person[[2, 3, 4]]
|
178 |
+
x1, y1 = candidate[right_shoulder_index][:2]
|
179 |
+
x2, y2 = candidate[right_elbow_index][:2]
|
180 |
+
x3, y3 = candidate[right_wrist_index][:2]
|
181 |
+
hands.append([x1, y1, x2, y2, x3, y3, False])
|
182 |
+
|
183 |
+
for x1, y1, x2, y2, x3, y3, is_left in hands:
|
184 |
+
# pos_hand = pos_wrist + ratio * (pos_wrist - pos_elbox) = (1 + ratio) * pos_wrist - ratio * pos_elbox
|
185 |
+
# handRectangle.x = posePtr[wrist*3] + ratioWristElbow * (posePtr[wrist*3] - posePtr[elbow*3]);
|
186 |
+
# handRectangle.y = posePtr[wrist*3+1] + ratioWristElbow * (posePtr[wrist*3+1] - posePtr[elbow*3+1]);
|
187 |
+
# const auto distanceWristElbow = getDistance(poseKeypoints, person, wrist, elbow);
|
188 |
+
# const auto distanceElbowShoulder = getDistance(poseKeypoints, person, elbow, shoulder);
|
189 |
+
# handRectangle.width = 1.5f * fastMax(distanceWristElbow, 0.9f * distanceElbowShoulder);
|
190 |
+
x = x3 + ratioWristElbow * (x3 - x2)
|
191 |
+
y = y3 + ratioWristElbow * (y3 - y2)
|
192 |
+
distanceWristElbow = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2)
|
193 |
+
distanceElbowShoulder = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
|
194 |
+
width = 1.5 * max(distanceWristElbow, 0.9 * distanceElbowShoulder)
|
195 |
+
# x-y refers to the center --> offset to topLeft point
|
196 |
+
# handRectangle.x -= handRectangle.width / 2.f;
|
197 |
+
# handRectangle.y -= handRectangle.height / 2.f;
|
198 |
+
x -= width / 2
|
199 |
+
y -= width / 2 # width = height
|
200 |
+
# overflow the image
|
201 |
+
if x < 0: x = 0
|
202 |
+
if y < 0: y = 0
|
203 |
+
width1 = width
|
204 |
+
width2 = width
|
205 |
+
if x + width > image_width: width1 = image_width - x
|
206 |
+
if y + width > image_height: width2 = image_height - y
|
207 |
+
width = min(width1, width2)
|
208 |
+
# the max hand box value is 20 pixels
|
209 |
+
if width >= 20:
|
210 |
+
detect_result.append([int(x), int(y), int(width), is_left])
|
211 |
+
|
212 |
+
'''
|
213 |
+
return value: [[x, y, w, True if left hand else False]].
|
214 |
+
width=height since the network require squared input.
|
215 |
+
x, y is the coordinate of top left
|
216 |
+
'''
|
217 |
+
return detect_result
|
218 |
+
|
219 |
+
|
220 |
+
# Written by Lvmin
|
221 |
+
def faceDetect(candidate, subset, oriImg):
|
222 |
+
# left right eye ear 14 15 16 17
|
223 |
+
detect_result = []
|
224 |
+
image_height, image_width = oriImg.shape[0:2]
|
225 |
+
for person in subset.astype(int):
|
226 |
+
has_head = person[0] > -1
|
227 |
+
if not has_head:
|
228 |
+
continue
|
229 |
+
|
230 |
+
has_left_eye = person[14] > -1
|
231 |
+
has_right_eye = person[15] > -1
|
232 |
+
has_left_ear = person[16] > -1
|
233 |
+
has_right_ear = person[17] > -1
|
234 |
+
|
235 |
+
if not (has_left_eye or has_right_eye or has_left_ear or has_right_ear):
|
236 |
+
continue
|
237 |
+
|
238 |
+
head, left_eye, right_eye, left_ear, right_ear = person[[0, 14, 15, 16, 17]]
|
239 |
+
|
240 |
+
width = 0.0
|
241 |
+
x0, y0 = candidate[head][:2]
|
242 |
+
|
243 |
+
if has_left_eye:
|
244 |
+
x1, y1 = candidate[left_eye][:2]
|
245 |
+
d = max(abs(x0 - x1), abs(y0 - y1))
|
246 |
+
width = max(width, d * 3.0)
|
247 |
+
|
248 |
+
if has_right_eye:
|
249 |
+
x1, y1 = candidate[right_eye][:2]
|
250 |
+
d = max(abs(x0 - x1), abs(y0 - y1))
|
251 |
+
width = max(width, d * 3.0)
|
252 |
+
|
253 |
+
if has_left_ear:
|
254 |
+
x1, y1 = candidate[left_ear][:2]
|
255 |
+
d = max(abs(x0 - x1), abs(y0 - y1))
|
256 |
+
width = max(width, d * 1.5)
|
257 |
+
|
258 |
+
if has_right_ear:
|
259 |
+
x1, y1 = candidate[right_ear][:2]
|
260 |
+
d = max(abs(x0 - x1), abs(y0 - y1))
|
261 |
+
width = max(width, d * 1.5)
|
262 |
+
|
263 |
+
x, y = x0, y0
|
264 |
+
|
265 |
+
x -= width
|
266 |
+
y -= width
|
267 |
+
|
268 |
+
if x < 0:
|
269 |
+
x = 0
|
270 |
+
|
271 |
+
if y < 0:
|
272 |
+
y = 0
|
273 |
+
|
274 |
+
width1 = width * 2
|
275 |
+
width2 = width * 2
|
276 |
+
|
277 |
+
if x + width > image_width:
|
278 |
+
width1 = image_width - x
|
279 |
+
|
280 |
+
if y + width > image_height:
|
281 |
+
width2 = image_height - y
|
282 |
+
|
283 |
+
width = min(width1, width2)
|
284 |
+
|
285 |
+
if width >= 20:
|
286 |
+
detect_result.append([int(x), int(y), int(width)])
|
287 |
+
|
288 |
+
return detect_result
|
289 |
+
|
290 |
+
|
291 |
+
# get max index of 2d array
|
292 |
+
def npmax(array):
|
293 |
+
arrayindex = array.argmax(1)
|
294 |
+
arrayvalue = array.max(1)
|
295 |
+
i = arrayvalue.argmax()
|
296 |
+
j = arrayindex[i]
|
297 |
+
return i, j
|
preprocess/dwpose/wholebody.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import os
|
4 |
+
|
5 |
+
import onnxruntime as ort
|
6 |
+
from .onnxdet import inference_detector
|
7 |
+
from .onnxpose import inference_pose
|
8 |
+
|
9 |
+
class Wholebody:
|
10 |
+
def __init__(self, model_root, device):
|
11 |
+
providers = ['CPUExecutionProvider'
|
12 |
+
] if device == 'cpu' else ['CUDAExecutionProvider']
|
13 |
+
onnx_det = os.path.join(model_root, 'dwpose/yolox_l.onnx')
|
14 |
+
onnx_pose = os.path.join(model_root, 'dwpose/dw-ll_ucoco_384.onnx')
|
15 |
+
|
16 |
+
self.session_det = ort.InferenceSession(path_or_bytes=onnx_det, providers=providers)
|
17 |
+
self.session_pose = ort.InferenceSession(path_or_bytes=onnx_pose, providers=providers)
|
18 |
+
|
19 |
+
def __call__(self, oriImg):
|
20 |
+
det_result = inference_detector(self.session_det, oriImg)
|
21 |
+
keypoints, scores = inference_pose(self.session_pose, det_result, oriImg)
|
22 |
+
|
23 |
+
keypoints_info = np.concatenate(
|
24 |
+
(keypoints, scores[..., None]), axis=-1)
|
25 |
+
# compute neck joint
|
26 |
+
neck = np.mean(keypoints_info[:, [5, 6]], axis=1)
|
27 |
+
# neck score when visualizing pred
|
28 |
+
neck[:, 2:4] = np.logical_and(
|
29 |
+
keypoints_info[:, 5, 2:4] > 0.3,
|
30 |
+
keypoints_info[:, 6, 2:4] > 0.3).astype(int)
|
31 |
+
new_keypoints_info = np.insert(
|
32 |
+
keypoints_info, 17, neck, axis=1)
|
33 |
+
mmpose_idx = [
|
34 |
+
17, 6, 8, 10, 7, 9, 12, 14, 16, 13, 15, 2, 1, 4, 3
|
35 |
+
]
|
36 |
+
openpose_idx = [
|
37 |
+
1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17
|
38 |
+
]
|
39 |
+
new_keypoints_info[:, openpose_idx] = \
|
40 |
+
new_keypoints_info[:, mmpose_idx]
|
41 |
+
keypoints_info = new_keypoints_info
|
42 |
+
|
43 |
+
keypoints, scores = keypoints_info[
|
44 |
+
..., :2], keypoints_info[..., 2]
|
45 |
+
|
46 |
+
return keypoints, scores
|
preprocess/humanparsing/datasets/__init__.py
ADDED
File without changes
|
preprocess/humanparsing/datasets/datasets.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# -*- encoding: utf-8 -*-
|
3 |
+
|
4 |
+
"""
|
5 |
+
@Author : Peike Li
|
6 |
+
@Contact : [email protected]
|
7 |
+
@File : datasets.py
|
8 |
+
@Time : 8/4/19 3:35 PM
|
9 |
+
@Desc :
|
10 |
+
@License : This source code is licensed under the license found in the
|
11 |
+
LICENSE file in the root directory of this source tree.
|
12 |
+
"""
|
13 |
+
|
14 |
+
import os
|
15 |
+
import numpy as np
|
16 |
+
import random
|
17 |
+
import torch
|
18 |
+
import cv2
|
19 |
+
from torch.utils import data
|
20 |
+
from utils.transforms import get_affine_transform
|
21 |
+
|
22 |
+
|
23 |
+
class LIPDataSet(data.Dataset):
|
24 |
+
def __init__(self, root, dataset, crop_size=[473, 473], scale_factor=0.25,
|
25 |
+
rotation_factor=30, ignore_label=255, transform=None):
|
26 |
+
self.root = root
|
27 |
+
self.aspect_ratio = crop_size[1] * 1.0 / crop_size[0]
|
28 |
+
self.crop_size = np.asarray(crop_size)
|
29 |
+
self.ignore_label = ignore_label
|
30 |
+
self.scale_factor = scale_factor
|
31 |
+
self.rotation_factor = rotation_factor
|
32 |
+
self.flip_prob = 0.5
|
33 |
+
self.transform = transform
|
34 |
+
self.dataset = dataset
|
35 |
+
|
36 |
+
list_path = os.path.join(self.root, self.dataset + '_id.txt')
|
37 |
+
train_list = [i_id.strip() for i_id in open(list_path)]
|
38 |
+
|
39 |
+
self.train_list = train_list
|
40 |
+
self.number_samples = len(self.train_list)
|
41 |
+
|
42 |
+
def __len__(self):
|
43 |
+
return self.number_samples
|
44 |
+
|
45 |
+
def _box2cs(self, box):
|
46 |
+
x, y, w, h = box[:4]
|
47 |
+
return self._xywh2cs(x, y, w, h)
|
48 |
+
|
49 |
+
def _xywh2cs(self, x, y, w, h):
|
50 |
+
center = np.zeros((2), dtype=np.float32)
|
51 |
+
center[0] = x + w * 0.5
|
52 |
+
center[1] = y + h * 0.5
|
53 |
+
if w > self.aspect_ratio * h:
|
54 |
+
h = w * 1.0 / self.aspect_ratio
|
55 |
+
elif w < self.aspect_ratio * h:
|
56 |
+
w = h * self.aspect_ratio
|
57 |
+
scale = np.array([w * 1.0, h * 1.0], dtype=np.float32)
|
58 |
+
return center, scale
|
59 |
+
|
60 |
+
def __getitem__(self, index):
|
61 |
+
train_item = self.train_list[index]
|
62 |
+
|
63 |
+
im_path = os.path.join(self.root, self.dataset + '_images', train_item + '.jpg')
|
64 |
+
parsing_anno_path = os.path.join(self.root, self.dataset + '_segmentations', train_item + '.png')
|
65 |
+
|
66 |
+
im = cv2.imread(im_path, cv2.IMREAD_COLOR)
|
67 |
+
h, w, _ = im.shape
|
68 |
+
parsing_anno = np.zeros((h, w), dtype=np.long)
|
69 |
+
|
70 |
+
# Get person center and scale
|
71 |
+
person_center, s = self._box2cs([0, 0, w - 1, h - 1])
|
72 |
+
r = 0
|
73 |
+
|
74 |
+
if self.dataset != 'test':
|
75 |
+
# Get pose annotation
|
76 |
+
parsing_anno = cv2.imread(parsing_anno_path, cv2.IMREAD_GRAYSCALE)
|
77 |
+
if self.dataset == 'train' or self.dataset == 'trainval':
|
78 |
+
sf = self.scale_factor
|
79 |
+
rf = self.rotation_factor
|
80 |
+
s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)
|
81 |
+
r = np.clip(np.random.randn() * rf, -rf * 2, rf * 2) if random.random() <= 0.6 else 0
|
82 |
+
|
83 |
+
if random.random() <= self.flip_prob:
|
84 |
+
im = im[:, ::-1, :]
|
85 |
+
parsing_anno = parsing_anno[:, ::-1]
|
86 |
+
person_center[0] = im.shape[1] - person_center[0] - 1
|
87 |
+
right_idx = [15, 17, 19]
|
88 |
+
left_idx = [14, 16, 18]
|
89 |
+
for i in range(0, 3):
|
90 |
+
right_pos = np.where(parsing_anno == right_idx[i])
|
91 |
+
left_pos = np.where(parsing_anno == left_idx[i])
|
92 |
+
parsing_anno[right_pos[0], right_pos[1]] = left_idx[i]
|
93 |
+
parsing_anno[left_pos[0], left_pos[1]] = right_idx[i]
|
94 |
+
|
95 |
+
trans = get_affine_transform(person_center, s, r, self.crop_size)
|
96 |
+
input = cv2.warpAffine(
|
97 |
+
im,
|
98 |
+
trans,
|
99 |
+
(int(self.crop_size[1]), int(self.crop_size[0])),
|
100 |
+
flags=cv2.INTER_LINEAR,
|
101 |
+
borderMode=cv2.BORDER_CONSTANT,
|
102 |
+
borderValue=(0, 0, 0))
|
103 |
+
|
104 |
+
if self.transform:
|
105 |
+
input = self.transform(input)
|
106 |
+
|
107 |
+
meta = {
|
108 |
+
'name': train_item,
|
109 |
+
'center': person_center,
|
110 |
+
'height': h,
|
111 |
+
'width': w,
|
112 |
+
'scale': s,
|
113 |
+
'rotation': r
|
114 |
+
}
|
115 |
+
|
116 |
+
if self.dataset == 'val' or self.dataset == 'test':
|
117 |
+
return input, meta
|
118 |
+
else:
|
119 |
+
label_parsing = cv2.warpAffine(
|
120 |
+
parsing_anno,
|
121 |
+
trans,
|
122 |
+
(int(self.crop_size[1]), int(self.crop_size[0])),
|
123 |
+
flags=cv2.INTER_NEAREST,
|
124 |
+
borderMode=cv2.BORDER_CONSTANT,
|
125 |
+
borderValue=(255))
|
126 |
+
|
127 |
+
label_parsing = torch.from_numpy(label_parsing)
|
128 |
+
|
129 |
+
return input, label_parsing, meta
|
130 |
+
|
131 |
+
|
132 |
+
class LIPDataValSet(data.Dataset):
|
133 |
+
def __init__(self, root, dataset='val', crop_size=[473, 473], transform=None, flip=False):
|
134 |
+
self.root = root
|
135 |
+
self.crop_size = crop_size
|
136 |
+
self.transform = transform
|
137 |
+
self.flip = flip
|
138 |
+
self.dataset = dataset
|
139 |
+
self.root = root
|
140 |
+
self.aspect_ratio = crop_size[1] * 1.0 / crop_size[0]
|
141 |
+
self.crop_size = np.asarray(crop_size)
|
142 |
+
|
143 |
+
list_path = os.path.join(self.root, self.dataset + '_id.txt')
|
144 |
+
val_list = [i_id.strip() for i_id in open(list_path)]
|
145 |
+
|
146 |
+
self.val_list = val_list
|
147 |
+
self.number_samples = len(self.val_list)
|
148 |
+
|
149 |
+
def __len__(self):
|
150 |
+
return len(self.val_list)
|
151 |
+
|
152 |
+
def _box2cs(self, box):
|
153 |
+
x, y, w, h = box[:4]
|
154 |
+
return self._xywh2cs(x, y, w, h)
|
155 |
+
|
156 |
+
def _xywh2cs(self, x, y, w, h):
|
157 |
+
center = np.zeros((2), dtype=np.float32)
|
158 |
+
center[0] = x + w * 0.5
|
159 |
+
center[1] = y + h * 0.5
|
160 |
+
if w > self.aspect_ratio * h:
|
161 |
+
h = w * 1.0 / self.aspect_ratio
|
162 |
+
elif w < self.aspect_ratio * h:
|
163 |
+
w = h * self.aspect_ratio
|
164 |
+
scale = np.array([w * 1.0, h * 1.0], dtype=np.float32)
|
165 |
+
|
166 |
+
return center, scale
|
167 |
+
|
168 |
+
def __getitem__(self, index):
|
169 |
+
val_item = self.val_list[index]
|
170 |
+
# Load training image
|
171 |
+
im_path = os.path.join(self.root, self.dataset + '_images', val_item + '.jpg')
|
172 |
+
im = cv2.imread(im_path, cv2.IMREAD_COLOR)
|
173 |
+
h, w, _ = im.shape
|
174 |
+
# Get person center and scale
|
175 |
+
person_center, s = self._box2cs([0, 0, w - 1, h - 1])
|
176 |
+
r = 0
|
177 |
+
trans = get_affine_transform(person_center, s, r, self.crop_size)
|
178 |
+
input = cv2.warpAffine(
|
179 |
+
im,
|
180 |
+
trans,
|
181 |
+
(int(self.crop_size[1]), int(self.crop_size[0])),
|
182 |
+
flags=cv2.INTER_LINEAR,
|
183 |
+
borderMode=cv2.BORDER_CONSTANT,
|
184 |
+
borderValue=(0, 0, 0))
|
185 |
+
input = self.transform(input)
|
186 |
+
flip_input = input.flip(dims=[-1])
|
187 |
+
if self.flip:
|
188 |
+
batch_input_im = torch.stack([input, flip_input])
|
189 |
+
else:
|
190 |
+
batch_input_im = input
|
191 |
+
|
192 |
+
meta = {
|
193 |
+
'name': val_item,
|
194 |
+
'center': person_center,
|
195 |
+
'height': h,
|
196 |
+
'width': w,
|
197 |
+
'scale': s,
|
198 |
+
'rotation': r
|
199 |
+
}
|
200 |
+
|
201 |
+
return batch_input_im, meta
|
preprocess/humanparsing/datasets/simple_extractor_dataset.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# -*- encoding: utf-8 -*-
|
3 |
+
|
4 |
+
"""
|
5 |
+
@Author : Peike Li
|
6 |
+
@Contact : [email protected]
|
7 |
+
@File : dataset.py
|
8 |
+
@Time : 8/30/19 9:12 PM
|
9 |
+
@Desc : Dataset Definition
|
10 |
+
@License : This source code is licensed under the license found in the
|
11 |
+
LICENSE file in the root directory of this source tree.
|
12 |
+
"""
|
13 |
+
|
14 |
+
import os
|
15 |
+
import pdb
|
16 |
+
|
17 |
+
import cv2
|
18 |
+
import numpy as np
|
19 |
+
from PIL import Image
|
20 |
+
from torch.utils import data
|
21 |
+
from utils.transforms import get_affine_transform
|
22 |
+
|
23 |
+
|
24 |
+
class SimpleFolderDataset(data.Dataset):
|
25 |
+
def __init__(self, root, input_size=[512, 512], transform=None):
|
26 |
+
self.root = root
|
27 |
+
self.input_size = input_size
|
28 |
+
self.transform = transform
|
29 |
+
self.aspect_ratio = input_size[1] * 1.0 / input_size[0]
|
30 |
+
self.input_size = np.asarray(input_size)
|
31 |
+
self.is_pil_image = False
|
32 |
+
if isinstance(root, Image.Image):
|
33 |
+
self.file_list = [root]
|
34 |
+
self.is_pil_image = True
|
35 |
+
elif os.path.isfile(root):
|
36 |
+
self.file_list = [os.path.basename(root)]
|
37 |
+
self.root = os.path.dirname(root)
|
38 |
+
else:
|
39 |
+
self.file_list = os.listdir(self.root)
|
40 |
+
|
41 |
+
def __len__(self):
|
42 |
+
return len(self.file_list)
|
43 |
+
|
44 |
+
def _box2cs(self, box):
|
45 |
+
x, y, w, h = box[:4]
|
46 |
+
return self._xywh2cs(x, y, w, h)
|
47 |
+
|
48 |
+
def _xywh2cs(self, x, y, w, h):
|
49 |
+
center = np.zeros((2), dtype=np.float32)
|
50 |
+
center[0] = x + w * 0.5
|
51 |
+
center[1] = y + h * 0.5
|
52 |
+
if w > self.aspect_ratio * h:
|
53 |
+
h = w * 1.0 / self.aspect_ratio
|
54 |
+
elif w < self.aspect_ratio * h:
|
55 |
+
w = h * self.aspect_ratio
|
56 |
+
scale = np.array([w, h], dtype=np.float32)
|
57 |
+
return center, scale
|
58 |
+
|
59 |
+
def __getitem__(self, index):
|
60 |
+
if self.is_pil_image:
|
61 |
+
img = np.asarray(self.file_list[index])[:, :, [2, 1, 0]]
|
62 |
+
else:
|
63 |
+
img_name = self.file_list[index]
|
64 |
+
img_path = os.path.join(self.root, img_name)
|
65 |
+
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
|
66 |
+
h, w, _ = img.shape
|
67 |
+
|
68 |
+
# Get person center and scale
|
69 |
+
person_center, s = self._box2cs([0, 0, w - 1, h - 1])
|
70 |
+
r = 0
|
71 |
+
trans = get_affine_transform(person_center, s, r, self.input_size)
|
72 |
+
input = cv2.warpAffine(
|
73 |
+
img,
|
74 |
+
trans,
|
75 |
+
(int(self.input_size[1]), int(self.input_size[0])),
|
76 |
+
flags=cv2.INTER_LINEAR,
|
77 |
+
borderMode=cv2.BORDER_CONSTANT,
|
78 |
+
borderValue=(0, 0, 0))
|
79 |
+
|
80 |
+
input = self.transform(input)
|
81 |
+
meta = {
|
82 |
+
'center': person_center,
|
83 |
+
'height': h,
|
84 |
+
'width': w,
|
85 |
+
'scale': s,
|
86 |
+
'rotation': r
|
87 |
+
}
|
88 |
+
|
89 |
+
return input, meta
|
preprocess/humanparsing/datasets/target_generation.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch.nn import functional as F
|
3 |
+
|
4 |
+
|
5 |
+
def generate_edge_tensor(label, edge_width=3):
|
6 |
+
label = label.type(torch.cuda.FloatTensor)
|
7 |
+
if len(label.shape) == 2:
|
8 |
+
label = label.unsqueeze(0)
|
9 |
+
n, h, w = label.shape
|
10 |
+
edge = torch.zeros(label.shape, dtype=torch.float).cuda()
|
11 |
+
# right
|
12 |
+
edge_right = edge[:, 1:h, :]
|
13 |
+
edge_right[(label[:, 1:h, :] != label[:, :h - 1, :]) & (label[:, 1:h, :] != 255)
|
14 |
+
& (label[:, :h - 1, :] != 255)] = 1
|
15 |
+
|
16 |
+
# up
|
17 |
+
edge_up = edge[:, :, :w - 1]
|
18 |
+
edge_up[(label[:, :, :w - 1] != label[:, :, 1:w])
|
19 |
+
& (label[:, :, :w - 1] != 255)
|
20 |
+
& (label[:, :, 1:w] != 255)] = 1
|
21 |
+
|
22 |
+
# upright
|
23 |
+
edge_upright = edge[:, :h - 1, :w - 1]
|
24 |
+
edge_upright[(label[:, :h - 1, :w - 1] != label[:, 1:h, 1:w])
|
25 |
+
& (label[:, :h - 1, :w - 1] != 255)
|
26 |
+
& (label[:, 1:h, 1:w] != 255)] = 1
|
27 |
+
|
28 |
+
# bottomright
|
29 |
+
edge_bottomright = edge[:, :h - 1, 1:w]
|
30 |
+
edge_bottomright[(label[:, :h - 1, 1:w] != label[:, 1:h, :w - 1])
|
31 |
+
& (label[:, :h - 1, 1:w] != 255)
|
32 |
+
& (label[:, 1:h, :w - 1] != 255)] = 1
|
33 |
+
|
34 |
+
kernel = torch.ones((1, 1, edge_width, edge_width), dtype=torch.float).cuda()
|
35 |
+
with torch.no_grad():
|
36 |
+
edge = edge.unsqueeze(1)
|
37 |
+
edge = F.conv2d(edge, kernel, stride=1, padding=1)
|
38 |
+
edge[edge!=0] = 1
|
39 |
+
edge = edge.squeeze()
|
40 |
+
return edge
|