영상을 프레임별 이미지로 변환

조회수 395회

os.walk 사용해서 디렉토리에있는 모든 영상을 불러와 리스트로 저장해 각각의 영상을 순회하면서 프레임별 이미지로 변환하려고 하는데 첫번쨰 파일 실행후

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' 

이런 에러가 뜨고 멈추는데 혹시 이건 어떻게 해결해야할까요?

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)


# #프레임을 저장할 디렉토리를 생성
    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()#프레임별 사진 추출
        cv2.imwrite(altfile[:-4]+"/frame%d.png" % count, image)
        print('Saved frame number :', str(int(video.get(1))))
        count += 1
    video.release()

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)