Lia*_*con 5 python opencv in-memory video-streaming cv2
我想知道是否可以使用VideoWriterPython 中的 OpenCV类“流式传输”数据?
通常为了处理内存中的数据,否则我会使用 BytesIO(或 StringIO)。
我尝试使用 BytesIO 失败了:
import cv2
from io import BytesIO
stream = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('x264')
data = BytesIO()
# added these to try to make data appear more like a string
data.name = 'stream.{}'.format('av1')
data.__str__ = lambda x: x.name
try:
video = cv2.VideoWriter(data, fourcc=fourcc, fps=30., frameSize=(640, 480))
start = data.tell()
# Check if camera opened successfully
if (stream.isOpened() == False):
print("Unable to read camera feed", file=sys.stderr)
exit(1)
# record loop
while True:
_, frame = stream.read()
video.write(frame)
data.seek(start)
# do stuff with frame bytes
# ...
data.seek(start)
finally:
try:
video.release()
except:
pass
finally:
stream.release()
Run Code Online (Sandbox Code Playgroud)
但是,BytesIO我最终得到了以下消息,而不是编写对象:
Traceback (most recent call last):
File "video_server.py", line 54, in talk_to_client
video = cv2.VideoWriter(data, fourcc=fourcc, fps=fps, frameSize=(width, height))
TypeError: Required argument 'apiPreference' (pos 2) not found
Run Code Online (Sandbox Code Playgroud)
...因此,当我将 VideoWriter 调用修改为cv2.VideoWriter(data, apiPreference=0, fourcc=fourcc, fps=30., frameSize=(640, 480))(我读到 0 表示自动,但我也尝试过cv2.CAP_FFMPEG)时,我反而收到以下错误:
Traceback (most recent call last):
File "video_server.py", line 54, in talk_to_client
video = cv2.VideoWriter(data, apiPreference=0, fourcc=fourcc, fps=fps, frameSize=(width, height))
TypeError: bad argument type for built-in operation
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,是否可以使用cv2.VideoWriter内存中的类编写编码视频,如果可以,它是如何完成的?
在这一点上,我没有任何想法,因此非常欢迎任何帮助:-)
不幸的是,OpenCV 不支持对内存进行编码(或解码)。您必须写入(或读取)磁盘,VideoWriter(或 VideoCapture)才能工作。