# #code for sampling image from the video @ 5fps
# #to be used only once for sampling video
import cv2
import math # for mathematical operations
import matplotlib.pyplot as plt # for plotting the images
count = 0
videoFile = '../Test Tom and Jerry.mp4'
cap = cv2.VideoCapture(videoFile) # capturing the video from the given path
frameRate = cap.get(5) #frame rate
print(frameRate)
x=1
while(cap.isOpened()):
print(True)
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(frameRate) == 0):
filename ="\Train\test%d.jpg" % count;count+=1
cv2.imwrite(filename, frame)
print("*", end="")
cap.release()
cv2.destroyAllWindows()
print ("Done!")
videoFile = "/full/path/to/Test Tom and Jerry.mp4"
because code may run in different folder then you expect and then relative path may not find file. And cv2 doesn’t raise error when it can’t read file but it returns False in cap.isOpened() and in ret, frame = cap.read()
You can check "Current Working Directory" with
import os
print( os.getcwd() )
You could also check if you have problem with other files and with different compressions. This way you can recognize if this is not problem with codec.
If you run code in script (not in Jupyter, IPython or other console) then you can get full path to folder with your script and use it to generate full path to other files
import os
BASE = os.path.dirname(os.path.abspath(__file__))
videoFile = os.path.join(BASE, "../Test Tom and Jerry.mp4")
By the way:
"\t" has special meaning in string - it means key <tab> - so path "\Test\test%d.jpg" means "\Test<tab>est%d.jpg". Use / instead of \ like /Test/test%d.jpg or double \\ like "\\Train\\test%d.jpg"or use prefix r for "raw" string like r"\Train\test%d.jpg"