ui function and no playable out
Browse files- main_noweb.py +118 -116
main_noweb.py
CHANGED
@@ -106,7 +106,7 @@ def pose3d(video, kpt_threshold):
|
|
106 |
#return_vis=True,
|
107 |
radius = 5,
|
108 |
thickness=4,
|
109 |
-
rebase_keypoint_height=
|
110 |
kpt_thr=kpt_threshold,
|
111 |
device=device,
|
112 |
pred_out_dir = add_dir
|
@@ -177,145 +177,147 @@ def pose2dhand(video, kpt_threshold):
|
|
177 |
|
178 |
return "".join(out_file)
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
with
|
183 |
-
with gr.
|
184 |
-
with gr.
|
185 |
-
with gr.
|
186 |
-
with gr.
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
gr.Markdown("Download the .json file that contains the keypoint positions for each frame in the video.")
|
202 |
-
jsonoutput = gr.File(file_types=[".json"])
|
203 |
-
gr.Markdown("""There are multiple ways to interact with these keypoints.
|
204 |
-
\n The example below shows how you can calulate the angle on the elbow for example.
|
205 |
-
\n Copy the code into your own preferred interpreter and experiment with the keypoint file.
|
206 |
-
\n If you choose to run the code, start by installing the packages json and numpy. The complete overview of the keypoint indices can be seen in the tab 'General information'. """)
|
207 |
-
gr.Code(
|
208 |
-
value="""
|
209 |
-
# Importing packages needed
|
210 |
-
import json
|
211 |
-
import numpy as np
|
212 |
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
|
226 |
-
angle = np.abs(radians*180.0/np.pi)
|
227 |
-
|
228 |
-
if angle >180.0:
|
229 |
-
angle = 360-angle
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
|
|
|
|
236 |
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
wrist_index = 9
|
241 |
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
245 |
|
246 |
-
|
247 |
-
|
|
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
interactive=False,
|
252 |
-
show_label=False,
|
253 |
-
)
|
254 |
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
|
257 |
-
with gr.Tab("General information"):
|
258 |
-
gr.Markdown("""
|
259 |
-
\n # Information about the models
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
\n The 3D pose model is used for estimating the 3D coordinates of human body joints from an image or a video frame. The model uses a combination of 2D pose estimation and depth estimation to infer the 3D joint locations.
|
270 |
-
|
271 |
-
\n The keypoints in the 2D pose model has the following order:
|
272 |
-
|
273 |
-
\n ```
|
274 |
-
0: Nose
|
275 |
-
1: Left Eye
|
276 |
-
2: Right Eye
|
277 |
-
3: Left Ear
|
278 |
-
4: Right Ear
|
279 |
-
5: Left Shoulder
|
280 |
-
6: Right Shoulder
|
281 |
-
7: Left Elbow
|
282 |
-
8: Right Elbow
|
283 |
-
9: Left Wrist
|
284 |
-
10: Right Wrist
|
285 |
-
11: Left Hip
|
286 |
-
12: Right Hip
|
287 |
-
13: Left Knee
|
288 |
-
14: Right Knee
|
289 |
-
15: Left Ankle
|
290 |
-
16: Right Ankle
|
291 |
-
```
|
292 |
-
""")
|
293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
submit_pose3d_file.click(fn=pose3d,
|
303 |
inputs= [video_input, file_kpthr],
|
304 |
-
outputs =
|
305 |
queue=True)
|
306 |
-
|
307 |
-
submit_hand_file.click(fn=pose2dhand,
|
308 |
-
inputs= [video_input, file_kpthr],
|
309 |
-
outputs = video_output3,
|
310 |
-
queue=True)
|
311 |
|
312 |
if __name__ == "__main__":
|
|
|
313 |
block.queue(
|
314 |
#concurrency_count=40, # When you increase the concurrency_count parameter in queue(), max_threads() in launch() is automatically increased as well.
|
315 |
#max_size=25, # Maximum number of requests that the queue processes
|
316 |
api_open = False # When creating a Gradio demo, you may want to restrict all traffic to happen through the user interface as opposed to the programmatic API that is automatically created for your Gradio demo.
|
317 |
).launch(
|
318 |
-
#enable_queue=True,
|
319 |
server_name="0.0.0.0",
|
320 |
server_port=7860,
|
321 |
auth=("novouser", "bstad2023")
|
|
|
106 |
#return_vis=True,
|
107 |
radius = 5,
|
108 |
thickness=4,
|
109 |
+
rebase_keypoint_height=True,
|
110 |
kpt_thr=kpt_threshold,
|
111 |
device=device,
|
112 |
pred_out_dir = add_dir
|
|
|
177 |
|
178 |
return "".join(out_file)
|
179 |
|
180 |
+
def UI():
|
181 |
+
block = gr.Blocks()
|
182 |
+
with block:
|
183 |
+
with gr.Column():
|
184 |
+
with gr.Tab("Upload video"):
|
185 |
+
with gr.Column():
|
186 |
+
with gr.Row():
|
187 |
+
with gr.Column():
|
188 |
+
video_input = gr.Video(source="upload", type="filepath", height=512, width=512)
|
189 |
+
# Insert slider with kpt_thr
|
190 |
+
file_kpthr = gr.Slider(0, 1, value=0.3, label='Keypoint threshold')
|
191 |
+
with gr.Row():
|
192 |
+
submit_pose_file = gr.Button("Make 2d pose estimation")
|
193 |
+
submit_pose3d_file = gr.Button("Make 3d pose estimation")
|
194 |
+
submit_hand_file = gr.Button("Make 2d hand estimation")
|
195 |
+
submit_detect_file = gr.Button("Detect and track objects")
|
196 |
+
|
197 |
+
with gr.Row():
|
198 |
+
video_output1 = gr.PlayableVideo(height=512, label = "Estimate human 2d poses", show_label=True)
|
199 |
+
video_output2 = gr.Video(height=512, label = "Estimate human 3d poses", show_label=True)
|
200 |
+
video_output3 = gr.PlayableVideo(height=512, label = "Estimate human hand poses", show_label=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
+
gr.Markdown("Download the .json file that contains the keypoint positions for each frame in the video.")
|
203 |
+
jsonoutput = gr.File(file_types=[".json"])
|
204 |
+
gr.Markdown("""There are multiple ways to interact with these keypoints.
|
205 |
+
\n The example below shows how you can calulate the angle on the elbow for example.
|
206 |
+
\n Copy the code into your own preferred interpreter and experiment with the keypoint file.
|
207 |
+
\n If you choose to run the code, start by installing the packages json and numpy. The complete overview of the keypoint indices can be seen in the tab 'General information'. """)
|
208 |
+
gr.Code(
|
209 |
+
value="""
|
210 |
+
# Importing packages needed
|
211 |
+
import json
|
212 |
+
import numpy as np
|
213 |
+
|
214 |
+
kpointlist=[]
|
215 |
|
216 |
+
# First we load the data
|
217 |
+
with open(file_path, 'r') as json_file:
|
218 |
+
data = json.load(json_file)
|
219 |
|
220 |
+
# The we define a function for calculating angles
|
221 |
+
def calculate_angle(a, b, c):
|
222 |
+
a = np.array(a) # First point
|
223 |
+
b = np.array(b) # Middle point
|
224 |
+
c = np.array(c) # End point
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
+
radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
|
227 |
+
angle = np.abs(radians*180.0/np.pi)
|
228 |
+
|
229 |
+
if angle >180.0:
|
230 |
+
angle = 360-angle
|
231 |
+
|
232 |
+
return angle
|
233 |
|
234 |
+
# We select the first identified person in the first frame (zero index) as an example
|
235 |
+
# To calculate the angle of the right elbow we take the point before and after and according to the indices that will be 6 (right shoulder) and 9 (right wrist)
|
236 |
+
predictions = data['predictions'][0] # Assuming batch_size is 1
|
|
|
237 |
|
238 |
+
# COCO keypoint indices
|
239 |
+
shoulder_index = 6
|
240 |
+
elbow_index = 8
|
241 |
+
wrist_index = 9
|
242 |
|
243 |
+
shoulder_point = data[0]['instances'][0]['keypoints'][shoulder_index]
|
244 |
+
elbow_point = data[0]['instances'][0]['keypoints'][elbow_index]
|
245 |
+
wrist_point = data[0]['instances'][0]['keypoints'][wrist_index]
|
246 |
|
247 |
+
angle = calculate_angle(shoulder_point, elbow_point, wrist_point)
|
248 |
+
print("Angle is: ", angle)
|
|
|
|
|
|
|
249 |
|
250 |
+
""",
|
251 |
+
language="python",
|
252 |
+
interactive=False,
|
253 |
+
show_label=False,
|
254 |
+
)
|
255 |
|
256 |
|
|
|
|
|
|
|
257 |
|
258 |
+
with gr.Tab("General information"):
|
259 |
+
gr.Markdown("""
|
260 |
+
\n # Information about the models
|
261 |
|
262 |
+
\n ## Pose models:
|
263 |
+
|
264 |
+
\n All the pose estimation models come from the library [MMpose](https://github.com/open-mmlab/mmpose). It is a library for human pose estimation that provides pre-trained models for 2D and 3D pose estimation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
+
\n The 2D pose model is used for estimating the 2D coordinates of human body joints from an image or a video frame. The model uses a convolutional neural network (CNN) to predict the joint locations and their confidence scores.
|
267 |
+
|
268 |
+
\n The 2D hand model is a specialized version of the 2D pose model that is designed for hand pose estimation. It uses a similar CNN architecture to the 2D pose model but is trained specifically for detecting the joints in the hand.
|
269 |
+
|
270 |
+
\n The 3D pose model is used for estimating the 3D coordinates of human body joints from an image or a video frame. The model uses a combination of 2D pose estimation and depth estimation to infer the 3D joint locations.
|
271 |
+
|
272 |
+
\n The keypoints in the 2D pose model has the following order:
|
273 |
+
|
274 |
+
\n ```
|
275 |
+
0: Nose
|
276 |
+
1: Left Eye
|
277 |
+
2: Right Eye
|
278 |
+
3: Left Ear
|
279 |
+
4: Right Ear
|
280 |
+
5: Left Shoulder
|
281 |
+
6: Right Shoulder
|
282 |
+
7: Left Elbow
|
283 |
+
8: Right Elbow
|
284 |
+
9: Left Wrist
|
285 |
+
10: Right Wrist
|
286 |
+
11: Left Hip
|
287 |
+
12: Right Hip
|
288 |
+
13: Left Knee
|
289 |
+
14: Right Knee
|
290 |
+
15: Left Ankle
|
291 |
+
16: Right Ankle
|
292 |
+
```
|
293 |
+
""")
|
294 |
+
|
295 |
+
|
296 |
+
|
297 |
+
# From file
|
298 |
+
submit_pose_file.click(fn=pose2d,
|
299 |
+
inputs= [video_input, file_kpthr],
|
300 |
+
outputs = [video_output1, jsonoutput],
|
301 |
+
queue=True)
|
302 |
|
303 |
+
submit_pose3d_file.click(fn=pose3d,
|
304 |
+
inputs= [video_input, file_kpthr],
|
305 |
+
outputs = video_output2,
|
306 |
+
queue=True)
|
307 |
+
|
308 |
+
submit_hand_file.click(fn=pose2dhand,
|
|
|
|
|
309 |
inputs= [video_input, file_kpthr],
|
310 |
+
outputs = video_output3,
|
311 |
queue=True)
|
312 |
+
return block
|
|
|
|
|
|
|
|
|
313 |
|
314 |
if __name__ == "__main__":
|
315 |
+
block = UI()
|
316 |
block.queue(
|
317 |
#concurrency_count=40, # When you increase the concurrency_count parameter in queue(), max_threads() in launch() is automatically increased as well.
|
318 |
#max_size=25, # Maximum number of requests that the queue processes
|
319 |
api_open = False # When creating a Gradio demo, you may want to restrict all traffic to happen through the user interface as opposed to the programmatic API that is automatically created for your Gradio demo.
|
320 |
).launch(
|
|
|
321 |
server_name="0.0.0.0",
|
322 |
server_port=7860,
|
323 |
auth=("novouser", "bstad2023")
|