Skip to content

Instantly share code, notes, and snippets.

@mohit-0212
Created February 23, 2018 21:54
Show Gist options
  • Save mohit-0212/31ffa1312b5c6c7a24e9cf9f845b51a3 to your computer and use it in GitHub Desktop.
Save mohit-0212/31ffa1312b5c6c7a24e9cf9f845b51a3 to your computer and use it in GitHub Desktop.
Detecting intro from video frames
import cv2
import numpy as np
import matplotlib.pyplot as plt
vid_frame = cv2.VideoCapture('/path for/video file/')
length = vid_frame.get(cv2.CAP_PROP_FRAME_COUNT)
fps = vid_frame.get(cv2.CAP_PROP_FPS)
num_frames = int(fps*300) #number of frames in first 5 minutes
time = [i/fps for i in range(1,num_frames+1)]
intensity = []
for i in range(num_frames):
success, image = vid_frame.read()
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
if success:
total = np.sum(np.sum(image, axis = 0))
intensity.append(total/(image.shape[0]*image.shape[1]))
vid_seq = []
for i in range(len(intensity)):
if time[i]>=15:
vid_seq.append(intensity[i])
else:
vid_seq.append(255) #max intensity value appended for the first 15 seconds to ignore any initial black screens
black_index = np.argpartition(np.array(vid_seq), 1)[:1][0]
time = str(int(time[black_index]/60))+" min: "+ str(int(time[black_index]%60)) + " sec" #gives the intro end time in the format x min: y sec
print time
# plt.plot(time, intensity)
# plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment