Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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
调用read()时OpenCV(Python)不更新帧_Python_Opencv_Rtsp - Fatal编程技术网

调用read()时OpenCV(Python)不更新帧

调用read()时OpenCV(Python)不更新帧,python,opencv,rtsp,Python,Opencv,Rtsp,我试图在指定的时间从RTSP提要中提取单个帧 这适用于视频流: vcap = cv2.VideoCapture(RTSP_URL) while(1): ret, frame = vcap.read() cv2.imshow('VIDEO', frame) cv2.waitKey(1) 但是,如果我想每秒拍摄一张图像,并通过这样做来保存它: vcap = cv2.VideoCapture(RTSP_URL) for t in range(60): ret, f

我试图在指定的时间从RTSP提要中提取单个帧

这适用于视频流:

vcap = cv2.VideoCapture(RTSP_URL)

while(1):
    ret, frame = vcap.read()
    cv2.imshow('VIDEO', frame)
    cv2.waitKey(1)
但是,如果我想每秒拍摄一张图像,并通过这样做来保存它:

vcap = cv2.VideoCapture(RTSP_URL)

for t in range(60):
    ret, frame = vcap.read()
    if ret:
        cv2.imwrite("{}.jpg".format(t), frame)
    time.sleep(1);
每个图像看起来都与第一个图像完全相同。在每个实例中,ret==True

(这在一周前对我来说还不错,然后ipython做了一些需要我重新安装的事情)

cv2.waitKey(1000)
如果你不使用
cv2.imshow()
显示图像,它将不会做任何事情。尝试:

vcap = cv2.VideoCapture(RTSP_URL)

for t in range(60):
    ret, frame = vcap.read()
    cv2.imwrite('{}.jpg'.format(t), frame)

    # this will activate the waitKey funciton
    cv2.imshow('preview', frame)
    cv2.waitKey(1000)
另一方面,iPython/jupyter不能很好地使用cv2的
imshow
和整个GUI功能。例如,如果你不能通过按键来打破循环

if (cv2.waitKey(1000) == 27 & 0xff): break;

好吧,在过去的几天里没完没了地搞乱它之后,不管出于什么原因,1秒对于提要来说都不够快

这将有助于:

vcap = cv2.VideoCapture(RTSP_URL)

for t in range(60):
    ret, frame = vcap.read()
    if ret and t % 1000 == 0:
        cv2.imwrite("{}.jpg".format(t), frame)
    time.sleep(0.001)

你是说
cv2.imwrite('t.jpg',frame)
?^^^是的,对不起,我修好了。。。我一直在搬回不同的图书馆。但是我的实际代码中有cv2.imwrite()。您不想在每次迭代中更改图像的名称吗?
t.jpg
。^^^^是的。。。抱歉,我是从内存中写的,不是我代码的实际复制粘贴。只是重现问题的最小块。好吧,如果我用time.sleep(1)替换cv2.waitKey(1000)?我不想展示任何东西,只想等一秒钟。