Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 可以打开cv';s的录像机是在一个单独的过程中编写的吗?_Python 3.x_Opencv_Python Multiprocessing - Fatal编程技术网

Python 3.x 可以打开cv';s的录像机是在一个单独的过程中编写的吗?

Python 3.x 可以打开cv';s的录像机是在一个单独的过程中编写的吗?,python-3.x,opencv,python-multiprocessing,Python 3.x,Opencv,Python Multiprocessing,我正在尝试用一个单独的过程将视频保存到磁盘。程序将创建一个图像缓冲区以保存在原始进程上。录制完成后,它将文件名和图像缓冲区传递给第二个进程,该进程将制作自己的VideoWriter并保存文件。然而,当第二个进程调用write时,什么也没有发生。它挂起并且不输出任何错误 我检查了录像机是否已经打开,并且它已经打开了。我试着将代码移动到原始流程,看看它是否在那里工作,它是否工作。我不知道这是我需要在新流程中初始化的设置,还是与VideoWriter的工作方式有关 这是我的密码 def stop_录制

我正在尝试用一个单独的过程将视频保存到磁盘。程序将创建一个图像缓冲区以保存在原始进程上。录制完成后,它将文件名和图像缓冲区传递给第二个进程,该进程将制作自己的VideoWriter并保存文件。然而,当第二个进程调用write时,什么也没有发生。它挂起并且不输出任何错误

我检查了录像机是否已经打开,并且它已经打开了。我试着将代码移动到原始流程,看看它是否在那里工作,它是否工作。我不知道这是我需要在新流程中初始化的设置,还是与VideoWriter的工作方式有关

这是我的密码

def stop_录制(自):
“”“在单独的进程中停止录制”“”
如果self.\u file\u dump\u进程为无:
self.\u parent\u conn,child\u conn=multiprocessing.Pipe()
self.\u file\u dump\u process=multiprocessing.process(
target=self.file\u dump\u循环,args=(子连接,self.log))
self.\u file\u dump\u process.daemon=True
self.\u文件\u转储\u进程.start()
如果自行录制:
self.\u log.info(“正在停止录制,请稍候…”)
#转储要处理的VideoWriter和图像缓冲区
#在主进程上运行时注释掉
self.\u parent\u conn.send([self.\u record\u filename,self.\u img\u buffer])
“”“在主进程上运行时在中添加注释”
fourcc=cv2.视频编写器_fourcc(*“MJPG”)
有效_fps=16.0
框架形状=(640480)
record_file=cv2.VideoWriter(self._record_文件名,fourcc,
有效的fps,框架形状,
isColor=1)
对于自身中的img.\u img\u缓冲区:
self.\uuu log.info(“…仍在这里…”)
记录文件写入(img)
#关闭该文件并将其设置为“无”
record_file.release()
self.\uuu log.info(“完成”)
"""
#无论发生什么情况,都要删除整个图像缓冲区
del self.\u img\u缓冲区[:]
自我记录=错误
@静力学方法
def文件转储循环(子连接、父日志):
fourcc=cv2.视频编写器_fourcc(*“MJPG”)
有效_fps=16.0
框架形状=(640480)
尽管如此:
msg=child_conn.recv()
记录\u filename=msg[0]
img_buffer=msg[1]
record_file=cv2.VideoWriter(record_文件名,fourcc,
有效的fps,框架形状,
isColor=1)
对于img_缓冲区中的img:
父日志信息(“…仍在此处…”)
记录文件写入(img)
#关闭该文件并将其设置为“无”
record_file.release()
del img_缓冲区[:]
父日志信息(“完成”)
以下是在一个进程上运行日志时的日志输出:

2019-03-29 16:19:02,469 - image_processor.stop_recording - INFO: Stopping recording. Please wait...
2019-03-29 16:19:02,473 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,515 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,541 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,567 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,592 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,617 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,642 - image_processor.stop_recording - INFO: ...still here...
2019-03-29 16:19:02,670 - image_processor.stop_recording - INFO: done.
下面是在第二个进程上运行日志时的日志输出:

2019-03-29 16:17:27,299 - image_processor.stop_recording - INFO: Stopping recording. Please wait...
2019-03-29 16:17:27,534 - image_processor.file_dump_loop - INFO: ...still here...

我尝试了这一点,并成功地使用了以下代码:

import cv2
cap, imgs = cv2.VideoCapture('exampleVideo.MP4'), []

# This function writes video
def write_video(list_of_images):
    vid_writer = cv2.VideoWriter('/home/stephen/Desktop/re_encode.avi',cv2.VideoWriter_fourcc('M','J','P','G'),120, (640,480))
    for image in list_of_images: vid_writer.write(image)

# Loop to read video and save images to a list
for frame in range(123):
    _, img = cap.read()
    imgs.append(img)
write_video(imgs)

cap.release()
一切都按预期进行,当我检查运行所需的时间时,我发现上面的代码读取视频需要0.13秒,编写视频需要0.43秒。如果我在同一个循环(如下)中读取视频并写入视频,则总处理时间为.56秒(即.13+.43)


首先将图像写入缓冲区(内存中),然后将图像写入视频文件(硬盘上),这有一个很大的缺点。缓冲区保存在RAM上,RAM会很快填满,您可能会遇到内存错误。

我尝试了这一点,并成功地使用了以下代码:

import cv2
cap, imgs = cv2.VideoCapture('exampleVideo.MP4'), []

# This function writes video
def write_video(list_of_images):
    vid_writer = cv2.VideoWriter('/home/stephen/Desktop/re_encode.avi',cv2.VideoWriter_fourcc('M','J','P','G'),120, (640,480))
    for image in list_of_images: vid_writer.write(image)

# Loop to read video and save images to a list
for frame in range(123):
    _, img = cap.read()
    imgs.append(img)
write_video(imgs)

cap.release()
一切都按预期进行,当我检查运行所需的时间时,我发现上面的代码读取视频需要0.13秒,编写视频需要0.43秒。如果我在同一个循环(如下)中读取视频并写入视频,则总处理时间为.56秒(即.13+.43)

首先将图像写入缓冲区(内存中),然后将图像写入视频文件(硬盘上),这有一个很大的缺点。缓冲区保存在RAM上,RAM会很快填满,您可能会遇到内存错误