Skip to content

Instantly share code, notes, and snippets.

@Shreyz-max
Created March 15, 2021 10:55
Show Gist options
  • Save Shreyz-max/73169c09d4e0694905e8bd43b7086d0f to your computer and use it in GitHub Desktop.
Save Shreyz-max/73169c09d4e0694905e8bd43b7086d0f to your computer and use it in GitHub Desktop.
extract features
def model_cnn_load():
model = VGG16(weights="imagenet", include_top=True, input_shape=(224, 224, 3))
out = model.layers[-2].output
model_final = Model(inputs=model.input, outputs=out)
return model_final
def load_image(path):
img = cv2.imread(path)
img = cv2.resize(img, (224, 224))
return img
def extract_features(video, model):
"""
:param video: The video whose frames are to be extracted to convert into a numpy array
:param model: the pretrained vgg16 model
:return: numpy array of size 4096x80
"""
video_id = video.split(".")[0]
print(video_id)
print(f'Processing video {video}')
image_list = video_to_frames(video)
samples = np.round(np.linspace(
0, len(image_list) - 1, 80))
image_list = [image_list[int(sample)] for sample in samples]
images = np.zeros((len(image_list), 224, 224, 3))
for i in range(len(image_list)):
img = load_image(image_list[i])
images[i] = img
images = np.array(images)
fc_feats = model.predict(images, batch_size=128)
img_feats = np.array(fc_feats)
# cleanup
shutil.rmtree(os.path.join(config.test_path, 'temporary_images'))
return img_feats
def extract_feats_pretrained_cnn():
"""
saves the numpy features from all the videos
"""
model = model_cnn_load()
print('Model loaded')
if not os.path.isdir(os.path.join(config.test_path, 'feat')):
os.mkdir(os.path.join(config.test_path, 'feat'))
video_list = os.listdir(os.path.join(config.test_path, 'video'))
for video in video_list:
outfile = os.path.join(config.test_path, 'features_dir', video.split(".")[0] + '.npy')
img_feats = extract_features(video, model)
np.save(outfile, img_feats)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment