-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_to_frame.py
89 lines (65 loc) · 2.65 KB
/
video_to_frame.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
from natsort import natsorted
import cv2
import numpy as np
import os
from math import sqrt
from glob import glob
from tqdm import tqdm
from PIL import Image
dataset_dir = "/home/negar/Downloads/cropped_bodypatches/"
json_dir = "/home/negar/Downloads/result_GMU/result/"
all_file_names = glob(dataset_dir+"*")
def read_json(file):
pose_seq = []
for r, d, f in os.walk(file):
#sort based on the frame number
for file in natsorted(f):
if file.endswith(".json"):
with open(r+"/"+file, "r") as read_file:
data = read_file.read()
data = data.replace('array', 'np.array').replace("float32","np.float32")
try:
d = eval(data)
except:
return []
# all the key points
body_pose = d[0]["keypoints"]
right_hand = body_pose[91:112]
left_hand = body_pose[112:133]
x_r_hand = int(right_hand[:,0].mean())
y_r_hand = int(right_hand[:,1].mean())
x_l_hand = int(left_hand[:,0].mean())
y_l_hand = int(left_hand[:,1].mean())
pose_seq.append([x_r_hand,y_r_hand,x_l_hand,y_l_hand])
return np.array(pose_seq)
file_cnt = 0
for file in tqdm(all_file_names):
hand_poses = read_json(json_dir+file.split("/")[-1].split(".avi")[-2] )
if(hand_poses == []):
continue
os.mkdir("./frames/"+file.split("/")[-1].split(".avi")[-2])
vidcap = cv2.VideoCapture(file)
success,image = vidcap.read()
count = 0
while success:
try:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
x_r_hand = hand_poses[count][0]
y_r_hand = hand_poses[count][1]
x_l_hand = hand_poses[count][2]
y_l_hand = hand_poses[count][3]
r_hand_patch = image[max(0, (y_r_hand-112)):max(0, (y_r_hand+112)),max(0, (x_r_hand-112)):max(0, (x_r_hand+112))]
r_hand_patch = cv2.resize(r_hand_patch, dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
l_hand_patch = image[max(0, (y_l_hand-112)):max(0, (y_l_hand+112)),max(0, (x_l_hand-112)):max(0, (x_l_hand+112))]
l_hand_patch = cv2.resize(l_hand_patch, dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
im = Image.fromarray(r_hand_patch)
im.save("./frames/"+file.split("/")[-1].split(".avi")[-2] +"/frame%d_r.jpg" % count)
im = Image.fromarray(l_hand_patch)
im.save("./frames/"+file.split("/")[-1].split(".avi")[-2] +"/frame%d_l.jpg" % count)
success,image = vidcap.read()
count += 1
except:
print(file)
success,image = vidcap.read()
count += 1
continue