Converting an image into a frame-by-frame image

Asked 1 years ago, Updated 1 years ago, 381 views

I want to use os.walk to retrieve all the images in the directory, save them as a list, and convert each image into a frame-by-frame image, but after running the first file

cv2.imwrite(altfile[:-4]+"/frame%d.png" % count, image)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite' 

This error appears and stops, so how can I solve this?

import cv2
import os

dir_path = "train_rgb_front_clips/raw_videos"

frame=[]
for (root, directories, files) in os.walk(dir_path):
    for file in files:
        if '.mp4' in file:
            file_path = os.path.join(file)
            frame.append(file_path)

for cycle in range (len(frame)):
    altfile='train_rgb_front_clips/raw_videos/'+frame[cycle] #비디오 경로
    video = cv2.VideoCapture(altfile) 

    if not video.isOpened():
        print("Could not Open :",altfile)

        exit(0)


# #Create a directory to store frames
    try:
        if not os.path.exists(altfile[:-4]):
            os.makedirs(altfile[:-4])
    except OSError:
        print ('Error: Creating directory. ' +  altfile[:-4])

    count = 0

    while(video.isOpened()):
        ret, image = video.read()#Extract pictures by frame
        cv2.imwrite(altfile[:-4]+"/frame%d.png" % count, image)
        print('Saved frame number :', str(int(video.get(1))))
        count += 1
    video.release()

directory python cv2

2023-01-04 06:16

1 Answers

I think you need to make the final path appropriately unique at each execution point.

# The simplest way is to take a picture of the present time without much thought.
import datetime
now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

#ex:     train_rgb_front_clips/20230104112305/raw_videos/1
altfile = 'train_rgb_front_clips/' + now + '/raw_videos/' + frame[cycle]


2023-01-04 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.