gr state on human3d
Browse files- main_noweb.py +116 -121
main_noweb.py
CHANGED
|
@@ -32,9 +32,9 @@ print("[INFO]: Imported modules!")
|
|
| 32 |
human = MMPoseInferencer("simcc_mobilenetv2_wo-deconv-8xb64-210e_coco-256x192") # simcc_mobilenetv2_wo-deconv-8xb64-210e_coco-256x192 dekr_hrnet-w32_8xb10-140e_coco-512x512
|
| 33 |
hand = MMPoseInferencer("hand")
|
| 34 |
#model3d = gr.State()
|
| 35 |
-
human3d = MMPoseInferencer(device=device,
|
| 36 |
pose3d="human3d",
|
| 37 |
-
scope="mmpose")
|
| 38 |
|
| 39 |
|
| 40 |
#"https://github.com/open-mmlab/mmpose/blob/main/configs/body_3d_keypoint/pose_lift/h36m/pose-lift_simplebaseline3d_8xb64-200e_h36m.py",
|
|
@@ -94,16 +94,14 @@ def check_extension(video):
|
|
| 94 |
return video
|
| 95 |
|
| 96 |
|
| 97 |
-
def pose3d(video, kpt_threshold, ):
|
| 98 |
|
| 99 |
video = check_extension(video)
|
| 100 |
print(device)
|
| 101 |
|
| 102 |
#human3d = MMPoseInferencer(device=device, pose3d="human3d", scope="mmpose")#"pose-lift_videopose3d-243frm-supv-cpn-ft_8xb128-200e_h36m")
|
| 103 |
|
| 104 |
-
print("HUMAN 3d downloaded!!")
|
| 105 |
-
human3dst = gr.State(value=human3d)
|
| 106 |
-
|
| 107 |
# Define new unique folder
|
| 108 |
add_dir = str(uuid.uuid4())
|
| 109 |
vis_out_dir = os.path.join("/".join(video.split("/")[:-1]), add_dir)
|
|
@@ -111,7 +109,7 @@ def pose3d(video, kpt_threshold, ):
|
|
| 111 |
os.makedirs(add_dir)
|
| 112 |
print(check_fps(video))
|
| 113 |
#video = human3d.preprocess(video, batch_size=8)
|
| 114 |
-
result_generator =
|
| 115 |
vis_out_dir = add_dir,
|
| 116 |
radius = 8,
|
| 117 |
thickness = 5,
|
|
@@ -158,11 +156,11 @@ def pose2d(video, kpt_threshold):
|
|
| 158 |
|
| 159 |
return "".join(out_file), "".join(kpoints)
|
| 160 |
|
| 161 |
-
def pose3dbatch(video, kpt_threshold):
|
| 162 |
kpoints=[]
|
| 163 |
outvids=[]
|
| 164 |
-
for v, t in zip(video, kpt_threshold):
|
| 165 |
-
vname, kname = pose3d(v, t)
|
| 166 |
outvids.append(vname)
|
| 167 |
kpoints.append(kname)
|
| 168 |
return [outvids]#kpoints, outvids
|
|
@@ -195,61 +193,60 @@ def pose2dhand(video, kpt_threshold):
|
|
| 195 |
|
| 196 |
return "".join(out_file), "".join(kpoints)
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
with
|
| 202 |
-
with gr.
|
| 203 |
-
with gr.
|
| 204 |
-
with gr.
|
| 205 |
-
with gr.
|
| 206 |
-
with gr.
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
gr.Markdown("Download the .json file that contains the keypoint positions for each frame in the video.")
|
| 224 |
-
jsonoutput = gr.File(file_types=[".json"])
|
| 225 |
-
gr.Markdown("""There are multiple ways to interact with these keypoints.
|
| 226 |
-
\n The example below shows how you can calulate the angle on the elbow for example.
|
| 227 |
-
\n Copy the code into your own preferred interpreter and experiment with the keypoint file.
|
| 228 |
-
\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'. """)
|
| 229 |
-
gr.Code(
|
| 230 |
-
value="""
|
| 231 |
-
|
| 232 |
# Importing packages needed
|
| 233 |
import json
|
| 234 |
import numpy as np
|
| 235 |
|
| 236 |
# First we load the data
|
| 237 |
with open(file_path, 'r') as json_file:
|
| 238 |
-
|
| 239 |
|
| 240 |
# The we define a function for calculating angles
|
| 241 |
def calculate_angle(a, b, c):
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
|
| 250 |
-
angle = 360-angle
|
| 251 |
-
|
| 252 |
-
return angle
|
| 253 |
|
| 254 |
|
| 255 |
# COCO keypoint indices
|
|
@@ -266,78 +263,76 @@ wrist_point = data[0]['instances'][0]['keypoints'][wrist_index]
|
|
| 266 |
angle = calculate_angle(shoulder_point, elbow_point, wrist_point)
|
| 267 |
print("Angle is: ", angle)
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
with gr.Tab("General information"):
|
| 278 |
-
gr.Markdown("""
|
| 279 |
-
\n # Information about the models
|
| 280 |
-
|
| 281 |
-
\n ## Pose models:
|
| 282 |
-
|
| 283 |
-
\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.
|
| 284 |
-
|
| 285 |
-
\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.
|
| 286 |
-
|
| 287 |
-
\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.
|
| 288 |
-
|
| 289 |
-
\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.
|
| 290 |
-
|
| 291 |
-
\n The keypoints in the 2D pose model has the following order:
|
| 292 |
-
|
| 293 |
-
\n ```
|
| 294 |
-
0: Nose
|
| 295 |
-
1: Left Eye
|
| 296 |
-
2: Right Eye
|
| 297 |
-
3: Left Ear
|
| 298 |
-
4: Right Ear
|
| 299 |
-
5: Left Shoulder
|
| 300 |
-
6: Right Shoulder
|
| 301 |
-
7: Left Elbow
|
| 302 |
-
8: Right Elbow
|
| 303 |
-
9: Left Wrist
|
| 304 |
-
10: Right Wrist
|
| 305 |
-
11: Left Hip
|
| 306 |
-
12: Right Hip
|
| 307 |
-
13: Left Knee
|
| 308 |
-
14: Right Knee
|
| 309 |
-
15: Left Ankle
|
| 310 |
-
16: Right Ankle
|
| 311 |
-
```
|
| 312 |
-
|
| 313 |
-
\n Below, you can see a visualization of the poses of the 2d, 3d and hand keypoint locations: """)
|
| 314 |
-
gr.Image("./cocoposes.png", width="200")
|
| 315 |
-
gr.Image("./cocohand.png", width="200")
|
| 316 |
-
|
| 317 |
|
| 318 |
-
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
if __name__ == "__main__":
|
| 340 |
-
block = UI()
|
| 341 |
block.queue(max_size=60,
|
| 342 |
concurrency_count=40, # When you increase the concurrency_count parameter in queue(), max_threads() in launch() is automatically increased as well.
|
| 343 |
#max_size=25, # Maximum number of requests that the queue processes
|
|
|
|
| 32 |
human = MMPoseInferencer("simcc_mobilenetv2_wo-deconv-8xb64-210e_coco-256x192") # simcc_mobilenetv2_wo-deconv-8xb64-210e_coco-256x192 dekr_hrnet-w32_8xb10-140e_coco-512x512
|
| 33 |
hand = MMPoseInferencer("hand")
|
| 34 |
#model3d = gr.State()
|
| 35 |
+
human3d = gr.State(MMPoseInferencer(device=device,
|
| 36 |
pose3d="human3d",
|
| 37 |
+
scope="mmpose"))
|
| 38 |
|
| 39 |
|
| 40 |
#"https://github.com/open-mmlab/mmpose/blob/main/configs/body_3d_keypoint/pose_lift/h36m/pose-lift_simplebaseline3d_8xb64-200e_h36m.py",
|
|
|
|
| 94 |
return video
|
| 95 |
|
| 96 |
|
| 97 |
+
def pose3d(video, kpt_threshold, model):
|
| 98 |
|
| 99 |
video = check_extension(video)
|
| 100 |
print(device)
|
| 101 |
|
| 102 |
#human3d = MMPoseInferencer(device=device, pose3d="human3d", scope="mmpose")#"pose-lift_videopose3d-243frm-supv-cpn-ft_8xb128-200e_h36m")
|
| 103 |
|
| 104 |
+
print("HUMAN 3d downloaded!!")
|
|
|
|
|
|
|
| 105 |
# Define new unique folder
|
| 106 |
add_dir = str(uuid.uuid4())
|
| 107 |
vis_out_dir = os.path.join("/".join(video.split("/")[:-1]), add_dir)
|
|
|
|
| 109 |
os.makedirs(add_dir)
|
| 110 |
print(check_fps(video))
|
| 111 |
#video = human3d.preprocess(video, batch_size=8)
|
| 112 |
+
result_generator = model(video,
|
| 113 |
vis_out_dir = add_dir,
|
| 114 |
radius = 8,
|
| 115 |
thickness = 5,
|
|
|
|
| 156 |
|
| 157 |
return "".join(out_file), "".join(kpoints)
|
| 158 |
|
| 159 |
+
def pose3dbatch(video, kpt_threshold, model):
|
| 160 |
kpoints=[]
|
| 161 |
outvids=[]
|
| 162 |
+
for v, t in zip(video, kpt_threshold, model):
|
| 163 |
+
vname, kname = pose3d(v, t, model)
|
| 164 |
outvids.append(vname)
|
| 165 |
kpoints.append(kname)
|
| 166 |
return [outvids]#kpoints, outvids
|
|
|
|
| 193 |
|
| 194 |
return "".join(out_file), "".join(kpoints)
|
| 195 |
|
| 196 |
+
block = gr.Blocks()
|
| 197 |
+
|
| 198 |
+
with block:
|
| 199 |
+
with gr.Column():
|
| 200 |
+
with gr.Tab("Upload video"):
|
| 201 |
+
with gr.Column():
|
| 202 |
+
with gr.Row():
|
| 203 |
+
with gr.Column():
|
| 204 |
+
with gr.Row():
|
| 205 |
+
video_input = gr.Video(source="upload", type="filepath", height=256, width=192)
|
| 206 |
+
# Insert slider with kpt_thr
|
| 207 |
+
with gr.Column():
|
| 208 |
+
gr.Markdown("Drag the keypoint threshold to filter out lower probability keypoints:")
|
| 209 |
+
file_kpthr = gr.Slider(0, 1, value=0.3, label='Keypoint threshold')
|
| 210 |
+
with gr.Row():
|
| 211 |
+
submit_pose_file = gr.Button("Make 2d pose estimation")
|
| 212 |
+
submit_pose3d_file = gr.Button("Make 3d pose estimation")
|
| 213 |
+
submit_hand_file = gr.Button("Make 2d hand estimation")
|
| 214 |
+
|
| 215 |
+
with gr.Row():
|
| 216 |
+
video_output1 = gr.PlayableVideo(label = "Estimate human 2d poses", show_label=True, height=256)
|
| 217 |
+
video_output2 = gr.PlayableVideo(label = "Estimate human 3d poses", show_label=True, height=256)
|
| 218 |
+
video_output3 = gr.PlayableVideo(label = "Estimate human hand poses", show_label=True, height=256)
|
| 219 |
+
|
| 220 |
+
gr.Markdown("Download the .json file that contains the keypoint positions for each frame in the video.")
|
| 221 |
+
jsonoutput = gr.File(file_types=[".json"])
|
| 222 |
+
gr.Markdown("""There are multiple ways to interact with these keypoints.
|
| 223 |
+
\n The example below shows how you can calulate the angle on the elbow for example.
|
| 224 |
+
\n Copy the code into your own preferred interpreter and experiment with the keypoint file.
|
| 225 |
+
\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'. """)
|
| 226 |
+
gr.Code(
|
| 227 |
+
value="""
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# Importing packages needed
|
| 230 |
import json
|
| 231 |
import numpy as np
|
| 232 |
|
| 233 |
# First we load the data
|
| 234 |
with open(file_path, 'r') as json_file:
|
| 235 |
+
data = json.load(json_file)
|
| 236 |
|
| 237 |
# The we define a function for calculating angles
|
| 238 |
def calculate_angle(a, b, c):
|
| 239 |
+
a = np.array(a) # First point
|
| 240 |
+
b = np.array(b) # Middle point
|
| 241 |
+
c = np.array(c) # End point
|
| 242 |
+
|
| 243 |
+
radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
|
| 244 |
+
angle = np.abs(radians*180.0/np.pi)
|
| 245 |
+
|
| 246 |
+
if angle >180.0:
|
| 247 |
+
angle = 360-angle
|
| 248 |
|
| 249 |
+
return angle
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
|
| 252 |
# COCO keypoint indices
|
|
|
|
| 263 |
angle = calculate_angle(shoulder_point, elbow_point, wrist_point)
|
| 264 |
print("Angle is: ", angle)
|
| 265 |
|
| 266 |
+
""",
|
| 267 |
+
language="python",
|
| 268 |
+
interactive=False,
|
| 269 |
+
show_label=False,
|
| 270 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
|
|
|
| 272 |
|
| 273 |
+
|
| 274 |
+
with gr.Tab("General information"):
|
| 275 |
+
gr.Markdown("""
|
| 276 |
+
\n # Information about the models
|
| 277 |
+
|
| 278 |
+
\n ## Pose models:
|
| 279 |
+
|
| 280 |
+
\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.
|
| 281 |
+
|
| 282 |
+
\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.
|
| 283 |
+
|
| 284 |
+
\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.
|
| 285 |
+
|
| 286 |
+
\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.
|
| 287 |
+
|
| 288 |
+
\n The keypoints in the 2D pose model has the following order:
|
| 289 |
+
|
| 290 |
+
\n ```
|
| 291 |
+
0: Nose
|
| 292 |
+
1: Left Eye
|
| 293 |
+
2: Right Eye
|
| 294 |
+
3: Left Ear
|
| 295 |
+
4: Right Ear
|
| 296 |
+
5: Left Shoulder
|
| 297 |
+
6: Right Shoulder
|
| 298 |
+
7: Left Elbow
|
| 299 |
+
8: Right Elbow
|
| 300 |
+
9: Left Wrist
|
| 301 |
+
10: Right Wrist
|
| 302 |
+
11: Left Hip
|
| 303 |
+
12: Right Hip
|
| 304 |
+
13: Left Knee
|
| 305 |
+
14: Right Knee
|
| 306 |
+
15: Left Ankle
|
| 307 |
+
16: Right Ankle
|
| 308 |
+
```
|
| 309 |
+
|
| 310 |
+
\n Below, you can see a visualization of the poses of the 2d, 3d and hand keypoint locations: """)
|
| 311 |
+
gr.Image("./cocoposes.png", width="200")
|
| 312 |
+
gr.Image("./cocohand.png", width="200")
|
| 313 |
+
|
| 314 |
+
|
| 315 |
|
| 316 |
+
|
| 317 |
+
# From file
|
| 318 |
+
submit_pose_file.click(fn=pose2d,
|
| 319 |
+
inputs= [video_input, file_kpthr],
|
| 320 |
+
outputs = [video_output1, jsonoutput],
|
| 321 |
+
queue=True)
|
| 322 |
+
|
| 323 |
+
submit_pose3d_file.click(fn=pose3dbatch,
|
| 324 |
+
inputs= [video_input, file_kpthr, human3d],
|
| 325 |
+
outputs = video_output2,#[video_output2, jsonoutput],
|
| 326 |
+
batch=True,
|
| 327 |
+
max_batch_size=16,
|
| 328 |
+
queue=True) # Sometimes it worked with queue false? But still slow
|
| 329 |
+
|
| 330 |
+
submit_hand_file.click(fn=pose2dhand,
|
| 331 |
+
inputs= [video_input, file_kpthr],
|
| 332 |
+
outputs = [video_output3, jsonoutput],
|
| 333 |
+
queue=True)
|
| 334 |
|
| 335 |
if __name__ == "__main__":
|
|
|
|
| 336 |
block.queue(max_size=60,
|
| 337 |
concurrency_count=40, # When you increase the concurrency_count parameter in queue(), max_threads() in launch() is automatically increased as well.
|
| 338 |
#max_size=25, # Maximum number of requests that the queue processes
|