File size: 7,949 Bytes
53420a8
ec80d0f
 
53420a8
ec80d0f
a38a551
ec80d0f
 
 
 
 
 
 
 
 
 
 
 
 
53420a8
1205af7
 
 
53420a8
 
 
 
 
 
 
 
 
 
 
 
 
 
1205af7
53420a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec80d0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53420a8
ec80d0f
a38a551
1613858
 
a38a551
 
 
 
 
 
 
 
 
 
 
ec80d0f
53420a8
 
1205af7
53420a8
 
1613858
cad5271
 
 
 
53420a8
 
6f15095
 
ade7519
 
1613858
 
 
 
 
 
 
 
ade7519
38122ef
 
 
 
 
ade7519
 
 
38122ef
ade7519
 
0bcf596
38122ef
7912fc9
 
a38a551
 
 
 
 
 
 
412b305
 
 
7912fc9
53420a8
 
ade7519
1613858
ade7519
 
 
7912fc9
 
 
 
7a6a025
9ae3428
4050a55
2ed1c97
 
7912fc9
 
 
 
 
3eb5670
 
29f5951
53420a8
1613858
 
 
47994fa
ec80d0f
47994fa
1613858
 
 
 
 
 
53420a8
1613858
ec80d0f
1613858
 
 
 
 
 
 
47994fa
 
901df68
 
5f60e23
16a0676
38122ef
 
 
 
 
 
 
 
 
 
 
1613858
16a0676
53420a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import gradio as gr
import numpy as np
import cv2
from fastapi import FastAPI, Request, Response
from src.body import Body
import json as js

body_estimation = Body('model/body_pose_model.pth')

def pil2cv(image):
    ''' PIL型 -> OpenCV型 '''
    new_image = np.array(image, dtype=np.uint8)
    if new_image.ndim == 2:  # モノクロ
        pass
    elif new_image.shape[2] == 3:  # カラー
        new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
    elif new_image.shape[2] == 4:  # 透過
        new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA)
    return new_image

with open("static/poseEditor.js", "r") as f:
    file_contents = f.read()

app = FastAPI()

@app.middleware("http")
async def some_fastapi_middleware(request: Request, call_next):
    path = request.scope['path']  # get the request route
    response = await call_next(request)
    
    if path == "/":
        response_body = ""
        async for chunk in response.body_iterator:
            response_body += chunk.decode()

        some_javascript = f"""

        <script type="text/javascript" defer>

{file_contents}

        </script>

        """

        response_body = response_body.replace("</body>", some_javascript + "</body>")

        del response.headers["content-length"]

        return Response(
            content=response_body,
            status_code=response.status_code, 
            headers=dict(response.headers),
            media_type=response.media_type
        )

    return response

# make cndidate to json
def candidate_to_json_string(arr):
    a = [f'[{x:.2f}, {y:.2f}]' for x, y, *_ in arr]
    return '[' + ', '.join(a) + ']'

# make subset to json
def subset_to_json_string(arr):
    arr_str = ','.join(['[' + ','.join([f'{num:.2f}' for num in row]) + ']' for row in arr])
    return '[' + arr_str + ']'

def estimate_body(source):
    if source == None:
      return None

    candidate, subset = body_estimation(pil2cv(source))
    return "{ \"candidate\": " + candidate_to_json_string(candidate) + ", \"subset\": " + subset_to_json_string(subset) + " }"
    
def image_changed(image):
  if image == None:
    return "estimation", {}

  if 'openpose' in image.info:
    print("pose found")
    jsonText = image.info['openpose']
    jsonObj = js.loads(jsonText)
    subset = jsonObj['subset']
    return f"""{image.width}px x {image.height}px, {len(subset)} indivisual(s)""", jsonText
  else:
    print("pose not found")
    candidate, subset = body_estimation(pil2cv(image))
    jsonText = "{ \"candidate\": " + candidate_to_json_string(candidate) + ", \"subset\": " + subset_to_json_string(subset) + " }"
    return f"""{image.width}px x {image.height}px, {subset.shape[0]} indivisual(s)""", jsonText

html_text = f"""

    <canvas id="canvas" width="512" height="512"></canvas>

    <script type="text/javascript" defer>{file_contents}</script>

    """

with gr.Blocks(css="""button { min-width: 80px; }""") as demo:
  gr.Markdown(f"""

## This project is no longer being updated. Please use [PoseMaker2](https://huggingface.co/spaces/jonigata/PoseMaker2) instead.

### (That project uses MMPose for pose estimation.)

""")
  with gr.Row():
    with gr.Column(scale=1):
      width = gr.Slider(label="Width", minimum=512, maximum=1024, step=64, value=512, interactive=True)
      height = gr.Slider(label="Height", minimum=512, maximum=1024, step=64, value=512, interactive=True)
      with gr.Accordion(label="Pose estimation", open=False):
        source = gr.Image(type="pil")
        estimationResult = gr.Markdown("""estimation""")
        with gr.Row():
          with gr.Column(min_width=80):
            applySizeBtn = gr.Button(value="Apply size")
          with gr.Column(min_width=80):
            replaceBtn = gr.Button(value="Replace")
          with gr.Column(min_width=80):
            importBtn = gr.Button(value="Import")
      with gr.Accordion(label="Json", open=False):
        with gr.Row():
          with gr.Column(min_width=80):
            replaceWithJsonBtn = gr.Button(value="Replace")
          with gr.Column(min_width=80):
            importJsonBtn = gr.Button(value="Import")
        gr.Markdown("""

| inout            | how to                                                                               |

| -----------------| ----------------------------------------------------------------------------------------- |

| Import | Paste json to "Json source" and click "Read", edit the width/height, then click "Replace" or "Import". |

| Export | click "Save" and "Copy to clipboard" of "Json" section.                                             |

""")
        json = gr.JSON(label="Json")
        jsonSource = gr.Textbox(label="Json source", lines=10)
      with gr.Accordion(label="Notes", open=False):
        gr.Markdown("""

#### How to bring pose to ControlNet

1. Press **Save** button

2. **Drag** the file placed at the bottom left corder of browser

3. **Drop** the file into ControlNet



#### Points to note for pseudo-3D rotation

When performing pseudo-3D rotation on the X and Y axes, the projection is converted to 2D and Z-axis information is lost when the mouse button is released. This means that if you finish dragging while the shape is collapsed, you may not be able to restore it to its original state. In such a case, please use the "undo" function.



#### Reuse pose image

Pose image generated by this tool has pose data in the image itself. You can reuse pose information by loading it as the image source instead of a regular image.

""")
    with gr.Column(scale=2):
      html = gr.HTML(html_text)
      with gr.Row():
        with gr.Column(scale=1, min_width=60):
          saveBtn = gr.Button(value="Save")
        with gr.Column(scale=7):
          gr.Markdown("""

- "ctrl + drag" to **scale**

- "alt + drag" to **move**

- "shift + drag" to **rotate** (move right first, release shift, then up or down)

- "space + drag" to **range-move**

- "[", "]" or "Alt + wheel" or "Space + wheel" to shrink or expand **range**

- "ctrl + Z", "shift + ctrl + Z" to **undo**, **redo**

- "ctrl + E" **add** new person

- "D + click" to **delete** person

- "Q + click" to **cut off** limb

- "X + drag" to **x-axis** pseudo-3D rotation

- "C + drag" to **y-axis** pseudo-3D rotation

- "R + click" to **repair**



When using Q, X, C, R, pressing and dont release until the operation is complete.



[Contact us for feature requests or bug reports (anonymous)](https://t.co/UC3jJOJJtS)

""")

  width.change(fn=None, inputs=[width], _js="(w) => { resizeCanvas(w,null); }")
  height.change(fn=None, inputs=[height], _js="(h) => { resizeCanvas(null,h); }")

  source.change(
    fn = image_changed,
    inputs = [source],
    outputs = [estimationResult, json])
  applySizeBtn.click(
    fn = lambda x: (x.width, x.height),
    inputs = [source], 
    outputs = [width, height])
  replaceBtn.click(
    fn = None,
    inputs = [json],
    outputs = [],
    _js="(json) => { initializeEditor(); importPose(json); return []; }")
  importBtn.click(
    fn = None,
    inputs = [json],
    outputs = [],
    _js="(json) => { importPose(json); return []; }")

  saveBtn.click(
    fn = None,
    inputs = [], outputs = [json],
    _js="() => { return [savePose()]; }")
  jsonSource.change(
    fn = lambda x: x,
    inputs = [jsonSource], outputs = [json])
  replaceWithJsonBtn.click(
    fn = None,
    inputs = [json],
    outputs = [],
    _js="(json) => { initializeEditor(); importPose(json); return []; }")
  importJsonBtn.click(
    fn = None,
    inputs = [json],
    outputs = [],
    _js="(json) => { importPose(json); return []; }")
  demo.load(fn=None, inputs=[], outputs=[], _js="() => { initializeEditor(); importPose(); return []; }")
    
gr.mount_gradio_app(app, demo, path="/")