jonigata commited on
Commit
1205af7
1 Parent(s): b44298e
Files changed (2) hide show
  1. app.py +5 -1
  2. static/poseEditor.js +165 -0
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
2
  from fastapi import FastAPI, Request, Response
3
 
 
 
 
4
  app = FastAPI()
5
 
6
  @app.middleware("http")
@@ -15,7 +18,7 @@ async def some_fastapi_middleware(request: Request, call_next):
15
 
16
  some_javascript = f"""
17
  <script type="text/javascript" defer>
18
- console.log("middleware");
19
  </script>
20
  """
21
 
@@ -39,6 +42,7 @@ def greet(source):
39
 
40
  html_text = f"""
41
  <canvas id="canvas" width="512" height="512"></canvas>
 
42
  """
43
 
44
  with gr.Blocks() as demo:
 
1
  import gradio as gr
2
  from fastapi import FastAPI, Request, Response
3
 
4
+ with open("static/poseEditor.js", "r") as f:
5
+ file_contents = f.read()
6
+
7
  app = FastAPI()
8
 
9
  @app.middleware("http")
 
18
 
19
  some_javascript = f"""
20
  <script type="text/javascript" defer>
21
+ {file_contents}
22
  </script>
23
  """
24
 
 
42
 
43
  html_text = f"""
44
  <canvas id="canvas" width="512" height="512"></canvas>
45
+ <script type="text/javascript" defer>{file_contents}</script>
46
  """
47
 
48
  with gr.Blocks() as demo:
static/poseEditor.js ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ console.log("hello from poseEditor.js")
2
+ var canvas = null;
3
+ var ctx = null;
4
+
5
+ // candidateの形式:[[x1, y1, score1], [x2, y2, score2], ...]
6
+ const candidate = [
7
+ [235, 158, 0.93167633],
8
+ [234, 220, 0.97106987],
9
+ [193, 222, 0.93366587],
10
+ [138, 263, 0.87655306],
11
+ [89, 308, 0.8868227],
12
+ [276, 220, 0.9038924],
13
+ [325, 264, 0.92930061],
14
+ [375, 309, 0.9217211],
15
+ [207, 347, 0.86410147],
16
+ [203, 433, 0.86538297],
17
+ [199, 523, 0.95236528],
18
+ [261, 347, 0.88489777],
19
+ [262, 430, 0.90848708],
20
+ [261, 522, 0.94749999],
21
+ [227, 148, 0.94189668],
22
+ [245, 148, 0.93967074],
23
+ [208, 158, 0.92053992],
24
+ [258, 154, 0.73533273]
25
+ ];
26
+
27
+ // subsetの形式:[[index1, index2, ..., -1], [index1, index2, ..., -1], ...]
28
+ const subset = [
29
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 33.81122635, 18]
30
+ ];
31
+
32
+ function clearCanvas() {
33
+ var w = canvas.width;
34
+ var h = canvas.height;
35
+ ctx.fillStyle = 'black';
36
+ ctx.fillRect(0, 0, w, h);
37
+ }
38
+
39
+ function resizeCanvas(width, height) {
40
+ canvas.width = width ? width : canvas.width;
41
+ canvas.height = height ? height : canvas.height;
42
+ clearCanvas();
43
+ drawBodyPose(candidate, subset);
44
+ }
45
+
46
+ function drawBodyPose(candidate, subset) {
47
+ const stickwidth = 4;
48
+ const limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10],
49
+ [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17],
50
+ [1, 16], [16, 18], [3, 17], [6, 18]];
51
+
52
+ const colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0],
53
+ [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255],
54
+ [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]];
55
+
56
+ for (let i = 0; i < 17; i++) {
57
+ for (let n = 0; n < subset.length; n++) {
58
+ const index0 = subset[n][limbSeq[i][0]-1];
59
+ const index1 = subset[n][limbSeq[i][1]-1];
60
+ if (index0 === -1 || index1 === -1) {
61
+ continue;
62
+ }
63
+ const [X0, Y0] = candidate[index0].slice(0, 2);
64
+ const [X1, Y1] = candidate[index1].slice(0, 2);
65
+ ctx.beginPath();
66
+ ctx.lineWidth=stickwidth;
67
+ ctx.strokeStyle = `rgb(${colors[i].join(',')})`;
68
+ ctx.moveTo(X0, Y0);
69
+ ctx.lineTo(X1, Y1);
70
+ ctx.stroke();
71
+ }
72
+ }
73
+
74
+ ctx.font = '12px serif';
75
+ for (let i = 0; i < 18; i++) {
76
+ for (let n = 0; n < subset.length; n++) {
77
+ const index = subset[n][i];
78
+ if (index === -1) {
79
+ continue;
80
+ }
81
+ const [x, y] = candidate[index].slice(0, 2);
82
+ ctx.beginPath();
83
+ ctx.arc(x, y, 4, 0, 2 * Math.PI);
84
+ ctx.fillStyle = `rgb(${colors[i].join(',')})`;
85
+ ctx.fill();
86
+ ctx.fillStyle = 'rgb(255,255,255)'
87
+ ctx.fillText(index, x-3, y+4);
88
+ }
89
+ }
90
+ }
91
+
92
+ function getNearestCandidateIndex(x, y) {
93
+ let minDist = Infinity;
94
+ let minIndex = -1;
95
+ for (let i = 0; i < candidate.length; i++) {
96
+ const dist = Math.sqrt((x - candidate[i][0]) ** 2 + (y - candidate[i][1]) ** 2);
97
+ if (dist < minDist) {
98
+ minDist = dist;
99
+ minIndex = i;
100
+ }
101
+ }
102
+ if (16 < minDist) {
103
+ return -1;
104
+ }
105
+ return minIndex;
106
+ }
107
+
108
+ // ドラッグ中に座標を保持するための変数
109
+ let isDragging = false;
110
+ let dragIndex = -1;
111
+ let dragOffsetX = 0;
112
+ let dragOffsetY = 0;
113
+
114
+ function getCanvasPosition(event) {
115
+ const rect = canvas.getBoundingClientRect();
116
+ const x = event.clientX - rect.left;
117
+ const y = event.clientY - rect.top;
118
+ return [x, y];
119
+ }
120
+
121
+ // Canvas要素上でマウスが押された場合に呼び出される関数
122
+ function handleMouseDown(event) {
123
+ const [x, y] = getCanvasPosition(event);
124
+ const index = getNearestCandidateIndex(x, y);
125
+
126
+ // ドラッグ処理の開始
127
+ if (0 <= index) {
128
+ isDragging = true;
129
+ dragIndex = index;
130
+ dragOffsetX = x - candidate[index][0];
131
+ dragOffsetY = y - candidate[index][1];
132
+ }
133
+ }
134
+
135
+ // Canvas要素上でマウスが動いた場合に呼び出される関数
136
+ function handleMouseMove(event) {
137
+ if (!isDragging) {
138
+ return;
139
+ }
140
+
141
+ const [x, y] = getCanvasPosition(event);
142
+
143
+ // candidateの座標を更新
144
+ candidate[dragIndex][0] = x - dragOffsetX;
145
+ candidate[dragIndex][1] = y - dragOffsetY;
146
+
147
+ clearCanvas();
148
+ drawBodyPose(candidate, subset);
149
+ }
150
+
151
+ // Canvas要素上でマウスが離された場合に呼び出される関数
152
+ function handleMouseUp(event) {
153
+ isDragging = false;
154
+ }
155
+
156
+ function initializePose() {
157
+ canvas = document.getElementById('canvas');
158
+ ctx = canvas.getContext('2d');
159
+
160
+ canvas.addEventListener('mousedown', handleMouseDown);
161
+ canvas.addEventListener('mousemove', handleMouseMove);
162
+ canvas.addEventListener('mouseup', handleMouseUp);
163
+
164
+ resizeCanvas(512, 512);
165
+ }