-
Notifications
You must be signed in to change notification settings - Fork 412
/
video_demo.py
128 lines (107 loc) · 3.86 KB
/
video_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import re
import sys
sys.path.append('.')
import cv2
import math
import time
import scipy
import argparse
import matplotlib
import numpy as np
import pylab as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from collections import OrderedDict
from scipy.ndimage import generate_binary_structure
from scipy.ndimage import gaussian_filter, maximum_filter
from lib.network.rtpose_vgg import get_model
from lib.network import im_transform
from lib.config import update_config, cfg
from evaluate.coco_eval import get_outputs, handle_paf_and_heat
from lib.utils.common import Human, BodyPart, CocoPart, CocoColors, CocoPairsRender, draw_humans
from lib.utils.paf_to_pose import paf_to_pose_cpp
import ffmpeg
def check_rotation(path_video_file):
# this returns meta-data of the video file in form of a dictionary
meta_dict = ffmpeg.probe(path_video_file)
# from the dictionary, meta_dict['streams'][0]['tags']['rotate'] is the key
# we are looking for
rotateCode = None
if int(meta_dict['streams'][0]['tags']['rotate']) == 90:
rotateCode = cv2.ROTATE_90_CLOCKWISE
elif int(meta_dict['streams'][0]['tags']['rotate']) == 180:
rotateCode = cv2.ROTATE_180
elif int(meta_dict['streams'][0]['tags']['rotate']) == 270:
rotateCode = cv2.ROTATE_90_COUNTERCLOCKWISE
return rotateCode
def correct_rotation(frame, rotateCode):
return cv2.rotate(frame, rotateCode)
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', help='experiment configure file name',
default='./experiments/vgg19_368x368_sgd.yaml', type=str)
parser.add_argument('--weight', type=str,
default='pose_model.pth')
parser.add_argument('opts',
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER)
args = parser.parse_args()
# update config file
update_config(cfg, args)
model = get_model('vgg19')
model.load_state_dict(torch.load(args.weight))
model.cuda()
model.float()
model.eval()
rotate_code = cv2.ROTATE_180
if __name__ == "__main__":
video_path = input("Enter video path")
video_capture_dummy = cv2.VideoCapture(video_path)
ret,oriImg = video_capture_dummy.read()
shape_tuple = tuple(oriImg.shape[1::-1])
print("Shape of image is ",shape_tuple)
rotate_code = check_rotation(video_path)
video_capture_dummy.release()
video_capture = cv2.VideoCapture(video_path)
##New stuff
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vid_out = cv2.VideoWriter('output.avi', fourcc, 20.0, shape_tuple)
###
proc_frame_list = []
oriImg_list = []
while True:
# Capture frame-by-frame
try:
ret, oriImg = video_capture.read()
if rotate_code is not None:
oriImg = correct_rotation(oriImg, rotate_code)
oriImg_list.append(oriImg)
cv2.imshow('Video', oriImg)
# vid_out.write(out)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except :
break
video_capture.release()
cv2.destroyAllWindows()
print("Number of frames",len(oriImg_list))
count = 0
for oriImg in oriImg_list:
count+=1
if count%50 == 0:
print(count, "frames processed")
try:
shape_dst = np.min(oriImg.shape[0:2])
print(oriImg.shape)
except:
break
with torch.no_grad():
paf, heatmap, imscale = get_outputs(
oriImg, model, 'rtpose')
humans = paf_to_pose_cpp(heatmap, paf, cfg)
out = draw_humans(oriImg, humans)
vid_out.write(out)
# When everything is done, release the capture