Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PoseNet integration for faster processing #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "joints_detectors/posenet"]
path = joints_detectors/posenet
url = https://github.com/StanfordHCI/posenet-pytorch.git
1 change: 1 addition & 0 deletions joints_detectors/posenet
Submodule posenet added at d296b6
53 changes: 53 additions & 0 deletions tools/posenet_realtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import cv2
from tqdm import tqdm
import numpy as np
from argparse import ArgumentParser
import ipdb;pdb=ipdb.set_trace
import time

from tools.utils_posenet import getPosenetModel, getKptsFromImage
poseModel = getPosenetModel()
interface2D = getKptsFromImage
from tools.utils import videopose_model_load as Model3Dload
model3D = Model3Dload()
from tools.utils import interface as VideoPoseInterface
interface3D = VideoPoseInterface
from tools.utils import draw_3Dimg, draw_2Dimg, videoInfo, resize_img

def main(VideoName):
cap, cap_length = videoInfo(VideoName)
kpt2Ds = []
for i in tqdm(range(cap_length)):
_, frame = cap.read()
frame, W, H = resize_img(frame)

try:
t0 = time.time()
joint2D = interface2D(poseModel, frame)
print('PoseNet comsume {:0.3f} s'.format(time.time() - t0))
except Exception as e:
print(e)
continue

if i == 0:
for _ in range(30):
kpt2Ds.append(joint2D)
elif i < 30:
kpt2Ds.append(joint2D)
kpt2Ds.pop(0)
else:
kpt2Ds.append(joint2D)

joint3D = interface3D(model3D, np.array(kpt2Ds), W, H)
joint3D_item = joint3D[-1] #(17, 3)
draw_3Dimg(joint3D_item, frame, display=1, kpt2D=joint2D)

if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-video", "--video_input", help="input video file name", default="/home/xyliu/Videos/sports/dance.mp4")
args = parser.parse_args()
VideoName = args.video_input
print('Input Video Name is ', VideoName)
main(VideoName)

38 changes: 38 additions & 0 deletions tools/utils_posenet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import sys

import torch
import numpy as np
path = os.path.split(os.path.realpath(__file__))[0]
posenet_path = os.path.join(path, '../joints_detectors/posenet/')
sys.path.insert(0, posenet_path)
import posenet

def getPosenetModel():
model = posenet.load_model(101)
model = model.cuda()
return model

def getKptsFromImage(model, input_image):
output_stride = model.output_stride
with torch.no_grad():
input_image, _, _ = posenet.utils._process_input(input_image, 1 / 3.0, output_stride)
input_image = torch.Tensor(input_image)
input_image = input_image.cuda()

heatmaps_result, offsets_result, displacement_fwd_result, displacement_bwd_result = model(input_image)

pose_scores, keypoint_scores, keypoint_coords = posenet.decode_multiple_poses(
heatmaps_result.squeeze(0),
offsets_result.squeeze(0),
displacement_fwd_result.squeeze(0),
displacement_bwd_result.squeeze(0),
output_stride=output_stride,
max_pose_detections=10,
min_pose_score=0.15)

#keypoint_coords *= output_scale
joint2 = np.zeros(keypoint_coords[0].shape)
joint2[:,0] = keypoint_coords[0][:,1].copy()
joint2[:,1] = keypoint_coords[0][:,0].copy()
return joint2