python - Python OpenCV videocapture 不从源捕获视频
问题描述
所以我有一段时间没有使用 python 所以它不是很漂亮。我首先对我将使用的一些目录进行硬编码,源目录是视频文件所在的位置,imageOutput 是一个目录,其中包含每个视频的子目录,其中包含捕获的图像文件。
self.path = os.getcwd().replace(os.sep, '/')
self.sourceDirectory = self.path + "/SourceFiles"
self.imageOutput = self.path + "/Temp"
我把所有的视频都放到了一些列表中
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)))
到目前为止,这一切都有效,下一步是遍历源目录中的每个视频,并每隔 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")
我已经尝试了很多不同的方法来让它工作,目前,它正在运行,这是它做它的输出日志;但是它只会随着数字的增加而永远循环
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
我尝试使用 im.show('image', image) 在循环中的各个点查看图像,这会打开一个停止响应的灰色图像。日志显示opencv在读取视频捕获时表示成功,但是无法显示屏幕截图可能意味着它不起作用?
解决方案
它确实有效,但是,我未能在保存图像的输出目录中创建目录,如果目录不存在,opencv 似乎不会创建目录!
推荐阅读
- jfreechart - JFreeChart,系列不相互绘制
- html - 侧边栏背景图片不显示?
- go - Web 服务器和监听 nat 同时
- reactjs - 使用 SSR React 安装 Material UI
- sql - 无法使用 PostgreSQL 删除重复行
- javascript - 如何修复 JavaScript fetch API PUT 的 400 Bad Request
- php - 如何显示存储图像 Laravel 5.2
- laravel - 无效的日期时间格式:1366 不正确的整数值:行中列“food_item”的“chapati”
- drupal - 是否可以上传自定义图像或使用 Drupal 主题提供的默认图像,例如徽标图像?
- python - 如何仅更改字典列表中最后一个重复的值?