Opencv3-Python3

环境:windows
opencv 3.4.3
python 3.6.5
问题:
使用opencv3.4.3来开启摄像头,10s之后关闭摄像头,但是cv2.release()之后摄像头依然开启,并未释放(通过观察电脑上面摄像头旁边的指示灯可以知道),然后再次启动摄像头的话会有警告,警告提示如下:

[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072873821                                        
[ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072873821

代码如下:

#encoding=utf-8
'''
Created on 2018年10月18日
'''
import cv2
import threading
import time
flag_video = True
def videoCapture():
    print(cv2.__version__)
    print('cv2.VideoCapture......')
    global cap
    cap = cv2.VideoCapture( 0)
    print('cap.read......')
    ret, frame = cap.read()
    
    global flag_video
    print('Open camera......')
    global flag_video
    while (flag_video):
        cv2.namedWindow('Video')
        cv2.imshow('Video', frame)
        cv2.waitKey(10)
        ret, frame = cap.read()
    print('Close camera......')
    cap.release()
if __name__ == '__main__':
    t = threading.Thread(target=videoCapture, args=())
    t.start()
    time.sleep(10)
    flag_video 
    flag_video = False    
    time.sleep(10)

这确实是个问题呀,查看资料,一篇stackoverflow映入眼帘,差不多一样的警告,然后详细解读了一下,有一位好友给出了方法:

Thanks to brianpeiris for the solution, I’m just documenting it here in a more formal way
The cv2.CAP_DSHOW is a flag passed as part of the open call, there are
many others you can pass, and this CAP_DSHOW is Microsoft specific.

大概意思就是说:cv2.CAP_DSHOW是作为open调用的一部分传递标志,还有许多其它的参数,而这个CAP_DSHOW是微软特有的。

import cv2
camera_number = 0 
c = cv2.VideoCapture( camera_number + cv2.CAP_DSHOW)
# this picks the LARGEST image possible
c.set( cv2.CAP_PROP_FRAME_HEIGHT, 10000 )
c.set( cv2.CAP_PROP_FRAME_WIDTH, 10000 )

while True:
    a,f = c.read()
    if not a:
         continue
    cv2.imshow( "it-works", f )
    k=cv2.waitKey(10)
    # press q to quit.
    if k == ord('q'):
       break

这样,问题就解决了。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值