-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_video_1.py
195 lines (161 loc) · 5.89 KB
/
test_video_1.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import torch
import os
import time
from PIL import Image
from models import resnext
from utils.opts import parse_opts
import torch.nn as nn
import pims
from mytransforms.spatial_transforms import (
Compose, Normalize, Scale, CenterCrop, CornerCrop, MultiScaleCornerCrop,
MultiScaleRandomCrop, RandomHorizontalFlip, ToTensor)
import cv2
#import pylab
#import imageio
#import skimage
import numpy as np
#pylab.switch_backend('agg')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#设置只可见gpu1
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
with Image.open(f) as img:
print("image",type(img))
return img.convert('RGB')
def video_loader(root_path, frame_indices,transform=None):
clip = []
for i in frame_indices:
image_path = os.path.join(root_path, '{:05d}.jpg'.format(i))
if os.path.exists(image_path):
clip.append(pil_loader(image_path))
else:
return False
if transform is not None:
transform.randomize_parameters()
clip = [transform(img) for img in clip]
clip = torch.stack(clip, 0).permute(1, 0, 2, 3)
return clip
# def make_data(frames_indexes,)
#frames:4d-tensor
def test(frames,model):
with torch.no_grad():
output = model(frames)
#print("output",output)
output = torch.sigmoid(output) > 0.5
indexs = (output==1).nonzero()
return indexs
def predict(model,sindex):
start_time = time.time()
opt = parse_opts()
model = resnext.resnet101(
num_classes=opt.n_finetune_classes,
shortcut_type=opt.resnet_shortcut,
cardinality=opt.resnext_cardinality,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
model = model.cuda()
model = nn.DataParallel(model, device_ids=None)
model.load_state_dict(torch.load('./trained_models/best.pth.tar')['state_dict'])
duration = (time.time() - start_time)*1000
print('restore time %.3f ms' % duration)
model.eval()
rgb_mean = [0.485, 0.456, 0.406]
rgb_std = [0.229, 0.224, 0.225]
opt.scales=[1]
transform_val = Compose([
MultiScaleCornerCrop(opt.scales, opt.sample_size, crop_positions=['c']),
ToTensor(),
Normalize(rgb_mean, rgb_std),
])
start_time = time.time()
clip = video_loader(root_path='/home/zengh/Dataset/AIChallenger/train/group5/567700300',frame_indices=range(3,19),transform=transform_val)
clip = clip.unsqueeze(0)
#print("clip",clip)
duration = (time.time() - start_time)*1000
print('pic time %.3f ms' % duration)
#print("clip",clip.shape)
start_time = time.time()
indexes = test(clip, model)
duration = (time.time() - start_time)*1000
print('pre time %.3f ms' % duration)
'''
def main():
start_time = time.time()
#998875634.mp4 998104369.mp4 997020967.mp4
video_path = '/home/zengh/Dataset/AIChallenger/group5/998875634.mp4'
if os.path.exists(video_path):
print("exists!")
vid = imageio.get_reader(video_path, 'ffmpeg')
duration = (time.time() - start_time) * 1000
print('imageio video time %.3f ms' % duration) '''
def main():
#994513477.mp4 995153247.mp4 996259932.mp4
start_time = time.time()
video_path = '/home/zengh/Dataset/AIChallenger/group5/995153247.mp4'
if os.path.exists(video_path):
print("exists!")
cap = cv2.VideoCapture(video_path) #15ms
duration = (time.time() - start_time) * 1000
print('1 time %.3f ms' % duration)
start_time = time.time()
print(id(cv2.CAP_PROP_POS_FRAMES))
#cap.set(cv2.CAP_PROP_POS_FRAMES,50) #40ms
#print("id",id(cv2.CAP_PROP_POS_FRAMES))
duration = (time.time() - start_time) * 1000
print('2 time %.3f ms' % duration)
start_time = time.time()
ret, frame = cap.read() #1ms
duration = (time.time() - start_time) * 1000
#print("ret",ret)
print('3 time %.3f ms' % duration)
'''
count = 1
frames = []
while(1):
ret, frame = cap.read()
if frame is None:
break
if count % 5 == 0:
frames.append(frame)
count = count + 1'''
#v = pims.Video('/home/zengh/Dataset/AIChallenger/group5/982006190.mp4')
#duration = (time.time() - start_time) * 1000
#print('cv video time %.3f ms' % duration)
opt = parse_opts()
start_time = time.time()
model = resnext.resnet101(
num_classes=opt.n_finetune_classes,
shortcut_type=opt.resnet_shortcut,
cardinality=opt.resnext_cardinality,
sample_size=opt.sample_size,
sample_duration=opt.sample_duration)
model = model.cuda()
model = nn.DataParallel(model, device_ids=None)
model.load_state_dict(torch.load('./trained_models/best.pth10.tar')['state_dict'])
duration = (time.time() - start_time)*1000
print('restore time %.3f ms' % duration)
#model = nn.DataParallel(model)
model.eval()
rgb_mean = [0.485, 0.456, 0.406]
rgb_std = [0.229, 0.224, 0.225]
opt.scales=[1]
transform_val = Compose([
MultiScaleCornerCrop(opt.scales, opt.sample_size, crop_positions=['c']),
ToTensor(),
Normalize(rgb_mean, rgb_std),
])
start_time = time.time()
clip = video_loader(root_path='/home/zengh/Dataset/AIChallenger/train/group5/567700300',frame_indices=range(3,19),transform=transform_val)
clip = clip.unsqueeze(0)
print("clip",clip)
duration = (time.time() - start_time)*1000
print('pic time %.3f ms' % duration)
#print("clip",clip.shape)
start_time = time.time()
indexes = test(clip, model)
duration = (time.time() - start_time)*1000
print('pre time %.3f ms' % duration)
if __name__ == '__main__':
main()