forked from jazzsaxmafia/video_to_sequence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MVAD_test_preprocessing.py
88 lines (68 loc) · 2.95 KB
/
MVAD_test_preprocessing.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
#-*- coding: utf-8 -*-
'''
According to the paper, the authors extracted upto 80 frames from each video,
they did not mention if they grabbed first 80 frames, or sampled 80 frames with same intervals,
but anyway I did the latter.
'''
import cv2
import os
import ipdb
import numpy as np
import pandas as pd
import skimage
from cnn_util import *
def preprocess_frame(image, target_height=224, target_width=224):
if len(image.shape) == 2:
image = np.tile(image[:,:,None], 3)
elif len(image.shape) == 4:
image = image[:,:,:,0]
image = skimage.img_as_float(image).astype(np.float32)
height, width, rgb = image.shape
if width == height:
resized_image = cv2.resize(image, (target_height,target_width))
elif height < width:
resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_width))
cropping_length = int((resized_image.shape[1] - target_height) / 2)
resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]
else:
resized_image = cv2.resize(image, (target_height, int(height * float(target_width) / width)))
cropping_length = int((resized_image.shape[0] - target_width) / 2)
resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]
return cv2.resize(resized_image, (target_height, target_width))
def main():
num_frames = 80
vgg_model = '/home/yusheng/Workspace/caffe/models/VGG_ILSVRC_16_layers/VGG_ILSVRC_16_layers.caffemodel'
vgg_deploy = '/home/yusheng/Workspace/caffe/models/VGG_ILSVRC_16_layers/VGG_ILSVRC_16_layers_deploy.prototxt'
video_path = '/home2/dataset/M-VAD/split/test_split'
video_save_path = '/home2/dataset/M-VAD/split/test_vgg16_feats'
videos = os.listdir(video_path)
videos = filter(lambda x: x.endswith('avi'), videos)
cnn = CNN(model=vgg_model, deploy=vgg_deploy, width=224, height=224)
for video in videos:
print video
if os.path.exists( os.path.join(video_save_path, video+'.npy') ):
print "Already processed ... "
continue
video_fullpath = os.path.join(video_path, video)
try:
cap = cv2.VideoCapture( video_fullpath )
except:
pass
frame_count = 0
frame_list = []
while True:
ret, frame = cap.read()
if ret is False:
break
frame_list.append(frame)
frame_count += 1
frame_list = np.array(frame_list)
if frame_count > 80:
frame_indices = np.linspace(0, frame_count, num=num_frames, endpoint=False).astype(int)
frame_list = frame_list[frame_indices]
cropped_frame_list = np.array(map(lambda x: preprocess_frame(x), frame_list))
feats = cnn.get_features(cropped_frame_list)
save_full_path = os.path.join(video_save_path, video + '.npy')
np.save(save_full_path, feats)
if __name__=="__main__":
main()