This question already has answers here:
Not getting a continuous video output from webcam in openCV

(1个答案)



Difference in output with waitKey(0) and waitKey(1)

(5个答案)


在4个月前关闭。




我想用实时摄像头进给显示轮廓,但只显示一个框架。
我的目标是这样的:https://www.youtube.com/watch?v = Fchzk1lDt7Q&t = 757s
代码:
import cv2
import numpy
import math


windowName = "OBJECT_MOVEMENT"
cv2.namedWindow(windowName)

cap = cv2.VideoCapture(0)

if cap.isOpened():
ret, frame = cap.read()
else:
    ret = False
    print("False operation.")
while ret:
    ret, frame=cap.read()
    imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    finalIm = cv2.drawContours(frame, contours, -1, (0,255,0), 1)

    cnt = contours[0]
    M = cv2.moments(cnt)
    contours_sizes = [(cv2.contourArea(cnt), cnt) for cnt in contours]
    biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]

    cX = int(((M["m10"])+1) / ((M["m00"])+1))
    cY = int(((M["m10"])+1) / ((M["m00"])+1))

    cv2.imshow("Countours",frame)
    if cv2.waitKey(0) == ord('q'):
        break
    cap.release()

最佳答案

使用cv2.waitKey(1)代替cv2.waitKey(0)

if cv2.waitKey(1) == ord('q'):
        break
同样,在while循环之外编写cap.release()。
cap.release()

关于python - Opencv无法显示实时网络摄像机录像。我的代码有什么问题? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62704550/

10-10 05:16