-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_video.py
163 lines (136 loc) · 5.82 KB
/
test_video.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import argparse
import cv2
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from eval import ToTensor, Normalize
from model import EventDetector
import numpy as np
import time
import os
import torch.nn.functional as F
event_names = {
0: 'Address',
1: 'Toe-up',
2: 'Mid-backswing (arm parallel)',
3: 'Top',
4: 'Mid-downswing (arm parallel)',
5: 'Impact',
6: 'Mid-follow-through (shaft parallel)',
7: 'Finish'
}
class SampleVideo(Dataset):
def __init__(self, path, input_size=160, transform=None):
self.path = path
self.input_size = input_size
self.transform = transform
def __len__(self):
return 1
def __getitem__(self, idx):
cap = cv2.VideoCapture(self.path)
frame_size = [cap.get(cv2.CAP_PROP_FRAME_HEIGHT), cap.get(cv2.CAP_PROP_FRAME_WIDTH)]
ratio = self.input_size / max(frame_size)
new_size = tuple([int(x * ratio) for x in frame_size])
delta_w = self.input_size - new_size[1]
delta_h = self.input_size - new_size[0]
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
left, right = delta_w // 2, delta_w - (delta_w // 2)
# preprocess and return frames
images = []
for pos in range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))):
_, img = cap.read()
resized = cv2.resize(img, (new_size[1], new_size[0]))
b_img = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=[0.406 * 255, 0.456 * 255, 0.485 * 255]) # ImageNet means (BGR)
b_img_rgb = cv2.cvtColor(b_img, cv2.COLOR_BGR2RGB)
images.append(b_img_rgb)
cap.release()
labels = np.zeros(len(images)) # only for compatibility with transforms
sample = {'images': np.asarray(images), 'labels': np.asarray(labels)}
if self.transform:
sample = self.transform(sample)
return sample
def write_video(out_filename, frames):
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# 동영상 출력 객체 생성
video_writer_new = cv2.VideoWriter(out_filename, fourcc, 30, (frames[0].shape[1], frames[0].shape[0]))
for frame in frames:
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
video_writer_new.write(frame)
video_writer_new.release()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', help='Path to video that you want to test', default='test_video.mp4')
parser.add_argument('-s', '--seq-length', type=int, help='Number of frames to use per forward pass', default=64)
parser.add_argument('-o', '--out_video', help='', default='')
parser.add_argument('-g', '--gpu_id', type=str, help='', default=0)
args = parser.parse_args()
seq_length = args.seq_length
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
print('Preparing video: {}'.format(args.path))
ds = SampleVideo(args.path, transform=transforms.Compose([ToTensor(),
Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])]))
dl = DataLoader(ds, batch_size=1, shuffle=False, drop_last=False)
model = EventDetector(pretrain=True,
width_mult=1.,
lstm_layers=1,
lstm_hidden=256,
bidirectional=True,
dropout=False)
try:
save_dict = torch.load('models/swingnet_1800.pth.tar', map_location=torch.device('cpu'))
except:
print(
"Model weights not found. Download model weights and place in 'models' folder. See README for instructions")
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
# print(save_dict)
model.load_state_dict(save_dict['model_state_dict'])
model.to(device)
model.eval()
print("Loaded model weights")
print('Testing...')
start_time = time.time()
for sample in dl:
images = sample['images']
# full samples do not fit into GPU memory so evaluate sample in 'seq_length' batches
batch = 0
print(images.shape)
while batch * seq_length < images.shape[1]:
if (batch + 1) * seq_length > images.shape[1]:
image_batch = images[:, batch * seq_length:, :, :, :]
else:
image_batch = images[:, batch * seq_length:(batch + 1) * seq_length, :, :, :]
print(image_batch.shape)
# logits = model(image_batch.cuda())
logits = model(image_batch)
if batch == 0:
probs = F.softmax(logits.data, dim=1).cpu().numpy()
else:
probs = np.append(probs, F.softmax(logits.data, dim=1).cpu().numpy(), 0)
batch += 1
# 코드 실행 후 시간 기록
end_time = time.time()
# 코드 실행 시간 계산
execution_time = end_time - start_time
print("inference time:", execution_time, "sec")
events = np.argmax(probs, axis=0)[:-1]
print('Predicted event frames: {}'.format(events))
cap = cv2.VideoCapture(args.path)
confidence = []
for i, e in enumerate(events):
confidence.append(probs[e, i])
print('Condifence: {}'.format([np.round(c, 3) for c in confidence]))
frames = []
for i, e in enumerate(events):
cap.set(cv2.CAP_PROP_POS_FRAMES, e)
_, img = cap.read()
cv2.putText(img, '{:.3f}'.format(confidence[i]), (20, 20), cv2.FONT_HERSHEY_DUPLEX, 0.75, (0, 0, 255))
# if args.d:
# cv2.imshow(event_names[i], img)
# else:
frames.append(img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
write_video(args.out_video, frames)