Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,127 +1,127 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
-
from fastapi import FastAPI, Request, Response
|
5 |
-
from src.body import Body
|
6 |
-
|
7 |
-
body_estimation = Body('model/body_pose_model.pth')
|
8 |
-
|
9 |
-
def pil2cv(image):
|
10 |
-
''' PIL型 -> OpenCV型 '''
|
11 |
-
new_image = np.array(image, dtype=np.uint8)
|
12 |
-
if new_image.ndim == 2: # モノクロ
|
13 |
-
pass
|
14 |
-
elif new_image.shape[2] == 3: # カラー
|
15 |
-
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
|
16 |
-
elif new_image.shape[2] == 4: # 透過
|
17 |
-
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA)
|
18 |
-
return new_image
|
19 |
-
|
20 |
-
with open("static/poseEditor.js", "r") as f:
|
21 |
-
file_contents = f.read()
|
22 |
-
|
23 |
-
app = FastAPI()
|
24 |
-
|
25 |
-
@app.middleware("http")
|
26 |
-
async def some_fastapi_middleware(request: Request, call_next):
|
27 |
-
path = request.scope['path'] # get the request route
|
28 |
-
response = await call_next(request)
|
29 |
-
|
30 |
-
if path == "/":
|
31 |
-
response_body = ""
|
32 |
-
async for chunk in response.body_iterator:
|
33 |
-
response_body += chunk.decode()
|
34 |
-
|
35 |
-
some_javascript = f"""
|
36 |
-
<script type="text/javascript" defer>
|
37 |
-
{file_contents}
|
38 |
-
</script>
|
39 |
-
"""
|
40 |
-
|
41 |
-
response_body = response_body.replace("</body>", some_javascript + "</body>")
|
42 |
-
|
43 |
-
del response.headers["content-length"]
|
44 |
-
|
45 |
-
return Response(
|
46 |
-
content=response_body,
|
47 |
-
status_code=response.status_code,
|
48 |
-
headers=dict(response.headers),
|
49 |
-
media_type=response.media_type
|
50 |
-
)
|
51 |
-
|
52 |
-
return response
|
53 |
-
|
54 |
-
# make cndidate to json
|
55 |
-
def candidate_to_json_string(arr):
|
56 |
-
a = [f'[{x:.2f}, {y:.2f}]' for x, y, *_ in arr]
|
57 |
-
return '[' + ', '.join(a) + ']'
|
58 |
-
|
59 |
-
# make subset to json
|
60 |
-
def subset_to_json_string(arr):
|
61 |
-
arr_str = ','.join(['[' + ','.join([f'{num:.2f}' for num in row]) + ']' for row in arr])
|
62 |
-
return '[' + arr_str + ']'
|
63 |
-
|
64 |
-
def estimate_body(source):
|
65 |
-
if source == None:
|
66 |
-
return None
|
67 |
-
|
68 |
-
candidate, subset = body_estimation(pil2cv(source))
|
69 |
-
return "{ \"candidate\": " + candidate_to_json_string(candidate) + ", \"subset\": " + subset_to_json_string(subset) + " }"
|
70 |
-
|
71 |
-
def image_changed(image):
|
72 |
-
if (image == None):
|
73 |
-
return {}, 512, 512
|
74 |
-
json = estimate_body(image)
|
75 |
-
return json, image.width, image.height
|
76 |
-
|
77 |
-
html_text = f"""
|
78 |
-
<canvas id="canvas" width="512" height="512"></canvas>
|
79 |
-
<script type="text/javascript" defer>{file_contents}</script>
|
80 |
-
"""
|
81 |
-
|
82 |
-
with gr.Blocks() as demo:
|
83 |
-
gr.Markdown("""### Usage
|
84 |
-
|
85 |
-
Choose one of the following methods to edit the pose:
|
86 |
-
|
87 |
-
| Style | Description |
|
88 |
-
| -----------------| ----------------------------------------------------------------------------------------- |
|
89 |
-
| Pose recognition | Upload an image and click "Start edit". |
|
90 |
-
| Input json | Input json to "Json source" and click "Input Json", edit the width/height, then click "Start edit". |
|
91 |
-
| Free style | Edit the width/height, then click "Start edit". |
|
92 |
-
|
93 |
-
To save the pose image, click "Save".
|
94 |
-
To export the pose data, click "Save" and "Copy to clipboard" of "Json" section.
|
95 |
-
""")
|
96 |
-
with gr.Row():
|
97 |
-
with gr.Column(scale=1):
|
98 |
-
source = gr.Image(type="pil")
|
99 |
-
width = gr.Slider(label="Width", mininmum=512, maximum=
|
100 |
-
height = gr.Slider(label="Height", mininmum=512, maximum=
|
101 |
-
startBtn = gr.Button(value="Start edit")
|
102 |
-
json = gr.JSON(label="Json", lines=10)
|
103 |
-
jsonInput = gr.Textbox(label="Json source", lines=10)
|
104 |
-
jsonInputBtn = gr.Button(value="Input Json")
|
105 |
-
with gr.Column(scale=2):
|
106 |
-
html = gr.HTML(html_text)
|
107 |
-
saveBtn = gr.Button(value="Save")
|
108 |
-
gr.HTML("<ul><li>ctrl + drag to scale</li><li>alt + drag to translate</li><li>shift + drag to rotate(move right first, then up or down)</li></ul>")
|
109 |
-
|
110 |
-
source.change(
|
111 |
-
fn = image_changed,
|
112 |
-
inputs = [source],
|
113 |
-
outputs = [json, width, height])
|
114 |
-
startBtn.click(
|
115 |
-
fn = None,
|
116 |
-
inputs = [json, width, height],
|
117 |
-
outputs = [],
|
118 |
-
_js="(json, w, h) => { initializePose(json,w,h); return []; }")
|
119 |
-
saveBtn.click(
|
120 |
-
fn = None,
|
121 |
-
inputs = [], outputs = [json],
|
122 |
-
_js="() => { return [savePose()]; }")
|
123 |
-
jsonInputBtn.click(
|
124 |
-
fn = lambda x: x,
|
125 |
-
inputs = [jsonInput], outputs = [json])
|
126 |
-
|
127 |
-
gr.mount_gradio_app(app, demo, path="/")
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from fastapi import FastAPI, Request, Response
|
5 |
+
from src.body import Body
|
6 |
+
|
7 |
+
body_estimation = Body('model/body_pose_model.pth')
|
8 |
+
|
9 |
+
def pil2cv(image):
|
10 |
+
''' PIL型 -> OpenCV型 '''
|
11 |
+
new_image = np.array(image, dtype=np.uint8)
|
12 |
+
if new_image.ndim == 2: # モノクロ
|
13 |
+
pass
|
14 |
+
elif new_image.shape[2] == 3: # カラー
|
15 |
+
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
|
16 |
+
elif new_image.shape[2] == 4: # 透過
|
17 |
+
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA)
|
18 |
+
return new_image
|
19 |
+
|
20 |
+
with open("static/poseEditor.js", "r") as f:
|
21 |
+
file_contents = f.read()
|
22 |
+
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
@app.middleware("http")
|
26 |
+
async def some_fastapi_middleware(request: Request, call_next):
|
27 |
+
path = request.scope['path'] # get the request route
|
28 |
+
response = await call_next(request)
|
29 |
+
|
30 |
+
if path == "/":
|
31 |
+
response_body = ""
|
32 |
+
async for chunk in response.body_iterator:
|
33 |
+
response_body += chunk.decode()
|
34 |
+
|
35 |
+
some_javascript = f"""
|
36 |
+
<script type="text/javascript" defer>
|
37 |
+
{file_contents}
|
38 |
+
</script>
|
39 |
+
"""
|
40 |
+
|
41 |
+
response_body = response_body.replace("</body>", some_javascript + "</body>")
|
42 |
+
|
43 |
+
del response.headers["content-length"]
|
44 |
+
|
45 |
+
return Response(
|
46 |
+
content=response_body,
|
47 |
+
status_code=response.status_code,
|
48 |
+
headers=dict(response.headers),
|
49 |
+
media_type=response.media_type
|
50 |
+
)
|
51 |
+
|
52 |
+
return response
|
53 |
+
|
54 |
+
# make cndidate to json
|
55 |
+
def candidate_to_json_string(arr):
|
56 |
+
a = [f'[{x:.2f}, {y:.2f}]' for x, y, *_ in arr]
|
57 |
+
return '[' + ', '.join(a) + ']'
|
58 |
+
|
59 |
+
# make subset to json
|
60 |
+
def subset_to_json_string(arr):
|
61 |
+
arr_str = ','.join(['[' + ','.join([f'{num:.2f}' for num in row]) + ']' for row in arr])
|
62 |
+
return '[' + arr_str + ']'
|
63 |
+
|
64 |
+
def estimate_body(source):
|
65 |
+
if source == None:
|
66 |
+
return None
|
67 |
+
|
68 |
+
candidate, subset = body_estimation(pil2cv(source))
|
69 |
+
return "{ \"candidate\": " + candidate_to_json_string(candidate) + ", \"subset\": " + subset_to_json_string(subset) + " }"
|
70 |
+
|
71 |
+
def image_changed(image):
|
72 |
+
if (image == None):
|
73 |
+
return {}, 512, 512
|
74 |
+
json = estimate_body(image)
|
75 |
+
return json, image.width, image.height
|
76 |
+
|
77 |
+
html_text = f"""
|
78 |
+
<canvas id="canvas" width="512" height="512"></canvas>
|
79 |
+
<script type="text/javascript" defer>{file_contents}</script>
|
80 |
+
"""
|
81 |
+
|
82 |
+
with gr.Blocks() as demo:
|
83 |
+
gr.Markdown("""### Usage
|
84 |
+
|
85 |
+
Choose one of the following methods to edit the pose:
|
86 |
+
|
87 |
+
| Style | Description |
|
88 |
+
| -----------------| ----------------------------------------------------------------------------------------- |
|
89 |
+
| Pose recognition | Upload an image and click "Start edit". |
|
90 |
+
| Input json | Input json to "Json source" and click "Input Json", edit the width/height, then click "Start edit". |
|
91 |
+
| Free style | Edit the width/height, then click "Start edit". |
|
92 |
+
|
93 |
+
To save the pose image, click "Save".
|
94 |
+
To export the pose data, click "Save" and "Copy to clipboard" of "Json" section.
|
95 |
+
""")
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column(scale=1):
|
98 |
+
source = gr.Image(type="pil")
|
99 |
+
width = gr.Slider(label="Width", mininmum=512, maximum=1920, step=64, value=512, key="Width", interactive=True)
|
100 |
+
height = gr.Slider(label="Height", mininmum=512, maximum=1080, step=64, value=512, key="Height", interactive=True)
|
101 |
+
startBtn = gr.Button(value="Start edit")
|
102 |
+
json = gr.JSON(label="Json", lines=10)
|
103 |
+
jsonInput = gr.Textbox(label="Json source", lines=10)
|
104 |
+
jsonInputBtn = gr.Button(value="Input Json")
|
105 |
+
with gr.Column(scale=2):
|
106 |
+
html = gr.HTML(html_text)
|
107 |
+
saveBtn = gr.Button(value="Save")
|
108 |
+
gr.HTML("<ul><li>ctrl + drag to scale</li><li>alt + drag to translate</li><li>shift + drag to rotate(move right first, then up or down)</li></ul>")
|
109 |
+
|
110 |
+
source.change(
|
111 |
+
fn = image_changed,
|
112 |
+
inputs = [source],
|
113 |
+
outputs = [json, width, height])
|
114 |
+
startBtn.click(
|
115 |
+
fn = None,
|
116 |
+
inputs = [json, width, height],
|
117 |
+
outputs = [],
|
118 |
+
_js="(json, w, h) => { initializePose(json,w,h); return []; }")
|
119 |
+
saveBtn.click(
|
120 |
+
fn = None,
|
121 |
+
inputs = [], outputs = [json],
|
122 |
+
_js="() => { return [savePose()]; }")
|
123 |
+
jsonInputBtn.click(
|
124 |
+
fn = lambda x: x,
|
125 |
+
inputs = [jsonInput], outputs = [json])
|
126 |
+
|
127 |
+
gr.mount_gradio_app(app, demo, path="/")
|