-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_RGB_feats.py
86 lines (65 loc) · 2.8 KB
/
extract_RGB_feats.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
#-*- coding: utf-8 -*-
import cv2
import os
import ipdb
import numpy as np
import pandas as pd
import skimage
from cnn_util import *
from keras.applications.vgg19 import VGG19
from keras.models.import Model
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/chenxp/caffe/models/vgg/VGG_ILSVRC_16_layers.caffemodel'
# vgg_deploy = '/home/chenxp/caffe/models/vgg/VGG_ILSVRC_16_layers_deploy.prototxt'
video_path = 'C:\\Users\\JSHwang\\Desktop\\2020-2TA\\ML_Chapter_4\\seq2seq_video2text\\testvideo'
video_save_path = './rgb_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 idx, video in enumerate(videos):
print idx, video
if os.path.exists( os.path.join(video_save_path, video) ):
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()