OpenCV 不会流式传输/更新我的视频。如何更新 imshow 窗口? [复制]

Posted

技术标签:

【中文标题】OpenCV 不会流式传输/更新我的视频。如何更新 imshow 窗口? [复制]【英文标题】:OpenCV wont stream/update my video. How do I update the iimshow windows? [duplicate] 【发布时间】:2020-06-24 17:52:43 【问题描述】:

我很难让我的视频供稿正常工作。我正在尝试大致按照本教程进行简单的对象检测,但遇到了一个问题。出于某种原因,imshow 窗口没有更新,它们只是继续显示第一帧。知道为什么吗?我正在使用 cv2.VideoCapture 并在每个循环中更新帧。

据我所知,帧正在成功更新,就好像我把手靠近相机一样,我可以看到帧的输出值下降到 [0,0,0,]ish,当我把它拿走,当颜色重新出现时,它们会弹回来。

这是我的代码:

# Imports
from imutils.video import VideoStream
import numpy as np
import cv2
import imutils
import time

# CONSTANTS
MIN_AREA = 500

vs = cv2.VideoCapture(0)
#vs = VideoStream(src=0).start()
time.sleep(2)
firstFrame = None
secondFrame = None

while True:
    frame = vs.read()
    if frame[0] is False: # If read returned False, there was no frame to grab.
        print("Error getting frame")
        exit()
    else: # Gets the image
        frame = frame[1]
        
    #Resize to make the image less intensive to process
    frame = imutils.resize(frame, width=500)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert to gray to make the image easier to run through Gaussian blur.
    gray = cv2.GaussianBlur(gray, (21, 21), 0) # Smooths out the pixels to get rid of any high variation between pixel intensities in a given region (x, x)
    # Makes sure I am always comparing the last 2 frames in
    if firstFrame is None:
        print("Entered 1st")
        firstFrame = gray
        continue
    elif secondFrame is None:
        print("Entered 2nd")
        secondFrame = gray
    else:
        print("Entered else")
        firstFrame = secondFrame
        secondFrame = gray;
    
    # Compute Abs diffrence between current frame and first frame.
    frameDelta = cv2.absdiff(firstFrame,secondFrame) # Simple subtraction of pixel intensities
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] # Thresholding the frameDelta. Only showing changes greater than x pixels, given by 2nd parameter argument.

    thresh = cv2.dilate(thresh, None, iterations=2)
    contours = cv2.findContours(thresh.copy(), cv2. RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)
    
    # Loop over the contours.
    # If the current contour is too small, ignore it
    for c in contours:
        if cv2.contourArea(c) < MIN_AREA:
            continue
        # Else a bounding box is drawn around it
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    
    # Showing frames
    cv2.imshow("Normal",frame)
    cv2.imshow("Thresh",thresh)
    cv2.imshow("Frame Delta", frameDelta)

vs.release()
cv2.destroyAllWindows()

【问题讨论】:

【参考方案1】:

找到答案了!

显然,我需要添加此代码

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

虽然我不知道为什么?

【讨论】:

这是因为 OpenCV HighGUI 库需要时间来处理并将图像绘制到屏幕上。通过不拨打cv2.waitKey() 电话,窗口永远不会更新。我已经标记了一个副本供您查看。

以上是关于OpenCV 不会流式传输/更新我的视频。如何更新 imshow 窗口? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何将 openCV 视频流式传输到 HTML 网页?

将 openCV C++ 视频流式传输到浏览器

如何使用 opencv VideoCapture 方法获取实时帧?

OpenCV:将视频流式传输到网页浏览器/HTML页面

从 localHost 端口 (http://192.168.1.1:8080) 在 openCv 中流式传输实时视频

是否可以使用 OpenCV 将视频从 https://(例如 YouTube)流式传输到 python 中?