简体   繁体   English

如何让 cv2.imshow() 显示列表中的帧?

[英]How to get cv2.imshow() to show frames from a list?

I am trying to save the last 50 frames from my webcam into a list and then play those frames back.我正在尝试将网络摄像头的最后 50 帧保存到列表中,然后播放这些帧。 When I try to display the frame the display window shows gray and says it is unresponsive.当我尝试显示框架时,显示窗口显示为灰色并表示它没有响应。 If I show the frame in the while loop it displays but if i try to show the frames from the list i saved them in the above issue occurs.如果我在 while 循环中显示框架,它会显示,但如果我尝试显示列表中的框架,我将它们保存在上述问题中。 This is the code I am running.这是我正在运行的代码。

cap = cv2.VideoCapture(0)
image_list = []
count = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    image_list.append(frame)

    #Display the resulting frame
    #cv2.imshow('frame',frame)   <---  this will show me my live frame by frame capture

    if count >= 50:
        break

    count += 1

# When everything is done, release the capture
cap.release()

for image in image_list:  
    cv2.imshow("frame", image)
    sleep(1)

If you do not use a proper UI Framerwork like tkinter or Qt you have to call如果您不使用像 tkinter 或 Qt 这样的合适的 UI Framerwork,则必须调用

cv2.waitKey(500)

periodically as it is the only way for the Highgui component of OpenCv to process events (and update the display).定期,因为它是 OpenCv 的 Highgui 组件处理事件(和更新显示)的唯一方法。 Otherwise the highgui just "hangs up".否则,highgui 只会“挂断”。

for image in image_list:  
    cv2.imshow("frame", image)
    cv2.waitKey(500)

Excerpt from the docs : 文档摘录:

Note笔记

This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.此函数是 HighGUI 中唯一可以获取和处理事件的方法,因此需要定期调用它以进行正常的事件处理,除非在负责事件处理的环境中使用 HighGUI。

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

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