keras-video-classifier
keras-video-classifier copied to clipboard
extract_vgg16_features does not work with variable frame rate
I am trying to apply this code to a custom dataset and find out that it will hang at vgg16_feature_extractor.py line 44 forever. After doing some search, I think the problem is that cv2.CAP_PROP_POS_MSEC does not work with variable frame rate. So I changed the code to:
def extract_vgg16_features(model, video_input_file_path, feature_output_file_path):
if os.path.exists(feature_output_file_path):
return np.load(feature_output_file_path)
count = 0
print('Extracting frames from video: ', video_input_file_path)
vidcap = cv2.VideoCapture(video_input_file_path)
fps=int(vidcap.get(cv2.CAP_PROP_FPS))
success, image = vidcap.read()
features = []
success = True
while success:
success, image = vidcap.read()
count = count + 1
# print('Read a new frame: ', success)
if success and count%fps==0:
img = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
input = img_to_array(img)
input = np.expand_dims(input, axis=0)
input = preprocess_input(input)
feature = model.predict(input).ravel()
features.append(feature)
unscaled_features = np.array(features)
np.save(feature_output_file_path, unscaled_features)
return unscaled_features
And it seems this code is working fine.