Some frames not extracted from video file in CV2 - python

I am using the following Python code to extract frames from a video file. I know that the total number of frames in my video file is 4375, but only 1900 frames are extracted from this code. Is there something I am missing?
vidcap = cv2.VideoCapture("/path/to/video/file")
success,image = vidcap.read()
count = 0
while(success):
cv2.imwrite("/path/to/store/frames/frame%d.png" %count, image)
success,image = vidcap.read()
count += 1

Related

Extract frames from video into specific folder

I want to extract frames from 3 videos into 3 different folder. Each folder has the frames of their corresponding video file. I am able to access my objective for only the 3rd video. How can I extract the frames for the first 2 videos as well
I have made the folders having names as per the video files till now. Developed the code for frame extraction but can extract only from the last video. Below is my code
import cv2
import glob
from glob import glob
import os
import shutil
def extractFrames(m,n):
if not os.path.exists:
os.makedirs(n)
vid_files=glob(m)
print(vid_files)
for v_f in range(len(vid_files)):
v1=os.path.basename(vid_files[v_f])
print(v1)
vid_name = os.path.splitext(v1)[0]
print(vid_name)
output = n +'\\video_' + vid_name
os.makedirs(output)
print(output)
vidcap = cv2.VideoCapture(vid_files[v_f])
print(vidcap)
success,image = vidcap.read()
seconds = 2
fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier = fps * seconds
count=0
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
image_path = output + "/" + img_name
frameId = int(round(vidcap.get(1)))
success,image = vidcap.read()
if frameId % multiplier == 0:
cv2.imwrite(filename = image_path, img = image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
return output
x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")
z=extractFrames(x,y)
If there are 3 videos namely video1,video2,video3. I want to extract the corresponding frames into their specific folders i.e video1 folder,video2 folder, video3 folder. Currently I am able to extract the frames for only the 3rd video into folder video3. How can I do it for video1 and video2 as well
Your indentation on the part from vidcap = ... down is off. Therefor only the last file in the for-loop is used.
import cv2
import glob
from glob import glob
import os
import shutil
def extractFrames(m,n):
if not os.path.exists:
os.makedirs(n)
vid_files=glob(m)
print(vid_files)
for v_f in range(len(vid_files)):
v1=os.path.basename(vid_files[v_f])
print(v1)
vid_name = os.path.splitext(v1)[0]
print(vid_name)
output = n +'\\video_' + vid_name
os.makedirs(output)
print(output)
vidcap = cv2.VideoCapture(vid_files[v_f])
print(vidcap)
success,image = vidcap.read()
seconds = 2
fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier = fps * seconds
count=0
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
image_path = output + "/" + img_name
frameId = int(round(vidcap.get(1)))
success,image = vidcap.read()
if frameId % multiplier == 0:
cv2.imwrite(filename = image_path, img = image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
return output # indent this less
x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")
z=extractFrames(x,y)

Can't write into a video file . Video file is not creating

I was just trying to read frames from a file and rewriting it into a new file using opencv 3.4.5 in python. But it fails to create the video file .
import cv2
vidcap = cv2.VideoCapture('movie.mov')
success,image = vidcap.read()
height, width, channels = image.shape
print(channels)
video=cv2.VideoWriter('video.avi',-1,1,(width,height))
count = 0
images = []
while success:
images.append(image)
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print(count,len(images))
for i in images:
video.write(i)
cv2.destroyAllWindows()
video.release()
The problem is the way you declare the writer.
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
writer = cv2.VideoWriter("new.avi", fourcc, fps, size)

OpenCV Python VideoWriter wrong fps rate for some codecs

I'm trying to read Videos, resize them and write them with a different codec, using OpenCV for Python3. The original frame rate should stay the same.
This works fine if I'm using MJPG as codec, but for other codecs the frame rate of the output is set to 600 fps. (I tried XVID, DIVX, WMV1, WMV2)
Is it possible to write Videos with those codecs with the original frame rate?
import os
import numpy as np
import cv2
codec = 'XVID'
new_size = (256, 256)
for root, dirs, files in os.walk("UCF-101"):
new_root = root.replace('UCF-101', 'UCF-101_resized_' + codec)
if not os.path.exists(new_root):
os.makedirs(new_root)
for file in files:
cap = cv2.VideoCapture(root + '/' + file)
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*codec)
out = cv2.VideoWriter(new_root + '/' + file, fourcc, fps, new_size, isColor=True)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
frame = cv2.resize(src=frame, dst=frame, dsize=new_size)
out.write(frame)
else:
break
cap.release()
out.release()
print('wrote ' + new_root + '/' + file)
Try changing your file extension to output file name with .mp4, not avi
codec = 'x264'
Replace if ret == True: with if frame is not None:

python sorting filename for opencv

I'm trying to sorting the jpgs (ascending numerically) in my directory to generate a video for opencv, but I'm having a a hard time finding a solution:
images = []
for f in os.listdir('.'):
if f.endswith('.jpg'):
images.append(f)
images[]:
['img_0.jpg', 'img_1.jpg', 'img_10.jpg', 'img_100.jpg', 'img_101.jpg', 'img_102.jpg', ... 'img_99.jpg']
import cv2
vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
You can use Os:
from os import listdir
from os.path import isfile, join
jpgfiles = [f for f in listdir('.') if isfile(join('.', f)) and f.endswith(".txt")]
jpgfiles.sort()

OpenCV - Not able to write images to video files

I have such a loop to write video files from images:
for a in range(len(events)):
c_videos = []
first = events[a][0]
last = events[a][1]
c_videos = video_ids[numpy.where(numpy.logical_and(timestamps >= first, timestamps <= last))]
video_name = "/export/students/sait/9-may-video-dataset/video-" + str(events[a][2]) + ".avi"
video = cv2.VideoWriter(video_name,-1,1,(width,height))
for b in range(len(c_videos)):
img_file = "/export/students/sait/9-may-results/rgb-" + str(c_videos[b]) + ".ppm"
img = cv2.imread(img_file)
video.write(img)
cv2.destroyAllWindows()
video.release()
But I cannot become successful in creating videos. I don't see any created video file under the destination directory.
How can I fix this problem?

Categories

Resources