Skip to content

Instantly share code, notes, and snippets.

@skipperkongen
Created October 11, 2018 11:49
Show Gist options
  • Save skipperkongen/acdc90a4eb4ea91fb6128805820fbd02 to your computer and use it in GitHub Desktop.
Save skipperkongen/acdc90a4eb4ea91fb6128805820fbd02 to your computer and use it in GitHub Desktop.
Convert video frames to features
# Create X, y series
import cv2
import numpy as np
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
class VGGFramePreprocessor():
def __init__(self, vgg_model):
self.vgg_model = vgg_model
def process(self, frame):
img_data = cv2.resize(frame,(224,224))
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
x = self.vgg_model.predict(img_data).flatten()
x = np.expand_dims(x, axis=0)
return x
def get_video_frames(video_path):
vidcap = cv2.VideoCapture(video_path)
success, frame = vidcap.read()
while success:
yield frame
success,frame = vidcap.read()
vidcap.release()
frame_preprocessor = VGGFramePreprocessor(VGG16(weights='imagenet', include_top=False))
# Load movies and transform frames to features
movies = []
X = []
y = []
for video_path in glob.glob('data/*.avi'):
print('preprocessing', video_path)
positive = CLASSES[POS_IDX] in video_path
_X = np.concatenate([frame_preprocessor.process(frame) for frame in get_video_frames(video_path)])
_y = np.array(_X.shape[0] * [[int(not positive), int(positive)]])
X.append(_X)
y.append(_y)
X = np.concatenate(X)
y = np.concatenate(y)
print(X.shape)
print(y.shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment