简体   繁体   中英

Python OpenCV videocapture not capturing video from source

so I haven't used python in a while so it's not very pretty. I start by hardcoding some of the directories I'll be using, source directory is where the video files are located, and imageOutput is a directory which will have a subdirectory for each video, containing the image files captured.

self.path = os.getcwd().replace(os.sep, '/')
self.sourceDirectory = self.path + "/SourceFiles"
self.imageOutput = self.path + "/Temp"

I get all the videos into some lists

os.chdir(self.sourceDirectory)
    for file in glob.glob('*.mp4'):
        self.videoList.append(file)
        self.videoCount += 1
self.videoList.sort(key=lambda f: int(re.sub('\D', '', f)))

this all works so far, the next step is meant to loop through each video in the source directory, and take frames every n seconds (roughly) and save them as an image in the imageOutput directory

for i in range(self.videoCount):
    imageCount = 0
    print("Generating images from " + self.videoList[i])

    videoCapture = cv2.VideoCapture(self.sourceDirectory + "/" + self.videoList[i])
    success, image = videoCapture.read()
    cv2.imshow('image', image)
    fps = videoCapture.get(cv2.CAP_PROP_FPS)
    print("raw fps:" + str(int(fps)))
    multiplier = int(fps) * int(self.capRate)
    print("CANCEL MULTIPLIER = " + str(multiplier))
    print("Capture : " + self.sourceDirectory + "/" + self.videoList[i] + " " + str(success) + " at " + str(
        fps) + " FPS")

    while success:
        frameId = int(round(videoCapture.get(1)))
        success, image = videoCapture.read()
        if frameId % int(multiplier) < 1:
            cv2.imwrite(self.imageOutput + "/" + str(i) + "/frame%d.jpg" % imageCount, image)
            print("saved as " + self.imageOutput + "/" + str(i) + "/frame%d.jpg" % imageCount)
            imageCount += 1

    videoCapture.release()
    print(self.videoList[i] + " is Complete with " + str(imageCount) + " images")
print("Finalized Video Processing")

I've tried a lot of different things to get this to work, currently, it runs and here's an output log of it doing its thing; however it loops forever just with increasing numbers

Generating images from chapter44.mp4
raw fps:29
CANCEL MULTIPLIER = 29
Capture : C:/Users/Isaac/PycharmProjects/Panovid/SourceFiles/chapter44.mp4 True at 29.97002997002997 FPS
saved as C:/Users/Isaac/PycharmProjects/Panovid/Temp/0/frame0.jpg
saved as C:/Users/Isaac/PycharmProjects/Panovid/Temp/0/frame1.jpg
saved as C:/Users/Isaac/PycharmProjects/Panovid/Temp/0/frame2.jpg
saved as C:/Users/Isaac/PycharmProjects/Panovid/Temp/0/frame3.jpg
 

I've tried viewing the image at various points in the loop with im.show('image', image), which opens a grey image that stops responding. The log shows that opencv says the success was true when reading the video capture, however not being able to show a screen-cap might mean it's not working?

It does work, however, I had failed to create the directories within the output directory where the images are saved, seems opencv doesn't create directories if they do not exist!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM