简体   繁体   English

Python OpenCV videocapture 不从源捕获视频

[英]Python OpenCV videocapture not capturing video from source

so I haven't used python in a while so it's not very pretty.所以我有一段时间没有使用 python 所以它不是很漂亮。 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.我首先对我将使用的一些目录进行硬编码,源目录是视频文件所在的位置,imageOutput 是一个目录,每个视频都有一个子目录,其中包含捕获的图像文件。

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到目前为止一切正常,下一步是循环遍历源目录中的每个视频,每 n 秒(大约)拍摄一帧并将它们作为图像保存在 imageOutput 目录中

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;我已经尝试了很多不同的方法来让它工作,目前,它正在运行,这是它运行的 output 日志; 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.我尝试使用 im.show('image', image) 在循环中的不同点查看图像,这会打开一个停止响应的灰色图像。 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?日志显示 opencv 在读取视频捕获时表示成功是真的,但是无法显示屏幕截图可能意味着它不起作用?

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!它确实有效,但是,我未能在保存图像的 output 目录中创建目录,如果目录不存在,似乎 opencv 不会创建目录!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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