openCV:cv2.VideoCapture(0)和cv2.VideoCapture(-1)出现问题

时间:2018-10-22 10:27:46

标签: python python-3.x opencv

cap.release()之后,唯一的相框已关闭,网络摄像头指示灯仍打开

import cv2

cap = cv2.VideoCapture(0)

#cap = cv2.VideoCapture(-1) if i give '-1' instead of '0' then light is getting OFF 
#but camera is not working because i don't have second camera to laptop.


while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cap.isOpened() #returns False
cv2.destroyAllWindows()

通过按“ q ”,框架将关闭,但网络摄像头指示灯仍亮起。

如何关闭网络摄像头? (在关闭python shell之后,它就关闭了。)

如果可能,请告诉我cv2.VideoCapture()类源代码的路径。

1 个答案:

答案 0 :(得分:0)

在环境变量中设置OPENCV_VIDEOIO_PRIORITY_MSMF=0。似乎opencv库中有实例泄漏。如果您在Windows上,则可以在setx中使用cmd来设置值setx OPENCV_VIDEOIO_PRIORITY_MSMF 0

此问题的参考:here

而且问题似乎也fixed。因此,请尝试更新您的opencv库或完全重新安装。 那应该可以解决您的问题。