1,环境:
win7 64位,
python3.8(anaconda),
opencv-python(pip install opencv-python)
pyusb(pip install pyusb)
libusb(pip install libusb)
另外在windows下,必须安装 libusb-win32-devel-filter-1.2.6.0.exe。仅 解压缩 libusb-win32-bin-1.2.6.0.zip,并安装压缩包里的2个软件,install-filter-win.exe, inf-wizard.exe 是不可以的。
2,硬件,
插入双目摄像头,到lenovo x220笔记本usb口
3,程序
程序使用方法:默认是右摄像头画面,按1是左面图,按2是右面图,按3是红蓝滤光片眼镜立体图,按4是双目立体图。下图是双目立体图。
# for硬件--CHUSEI 双目摄像头
# from https://www.jianshu.com/p/2ce302d588a0, 原程序端口不对cv2.VideoCapture(0)
# edit for linux and windows
import cv2
import usb.core
cam=cv2.VideoCapture(700) # 不一定是700,需要枚举法,暴力测试端口
dev = usb.core.find(idVendor=0x18e3, idProduct=0x5031)
dev.set_configuration() #### 去除这个语句也可以使用??
dev.ctrl_transfer(0x21,0x01,0x0800,0x0600,[0x50,0xff]) # simulate the SET_CUR sequence
dev.ctrl_transfer(0x21,0x01,0x0f00,0x0600,[0x00,0xf6])
dev.ctrl_transfer(0x21,0x01,0x0800,0x0600,[0x25,0x00])
dev.ctrl_transfer(0x21,0x01,0x0800,0x0600,[0x5f,0xfe])
dev.ctrl_transfer(0x21,0x01,0x0f00,0x0600,[0x00,0x03])
dev.ctrl_transfer(0x21,0x01,0x0f00,0x0600,[0x00,0x02])
dev.ctrl_transfer(0x21,0x01,0x0f00,0x0600,[0x00,0x12])
dev.ctrl_transfer(0x21,0x01,0x0f00,0x0600,[0x00,0x04])
dev.ctrl_transfer(0x21,0x01,0x0800,0x0600,[0x76,0xc3])
k=0
while (k!=ord('q')): # 按下q键后退出
ret,frame=cam.read()
cv2.imshow("1-left / 2-right(default) / 3-RB / 4-stero",frame)
if k == ord('q'):
break
k=cv2.waitKey(18)&0xFF # 等待18毫秒,并非仅仅是等待键盘输入,是窗口显示的必要语句
kv=k-ord('0')
# print(kv)
# if press 1,2,3 or 4, change the 3d camera mode
if kv in [1,2,3,4]:
dev.ctrl_transfer(0x21,0x01,0x0a00,0x0600,[kv,0x00])
cam.release() # 如果放在wihle循环中,程序强行从 循环中退出后,不能释放资源。再次打开将发生各种错误。
cv2.destroyAllWindows()
如果上面的程序不能正常运行,可以枚举CHUSEI双目摄像头 USB 端口
## xxx 暴力枚举CHUSEI双目摄像头 USB 端口
##原文链接:https://blog.csdn.net/hushaoqiqimingxing/article/details/96888775
import cv2
ID = 0
while(1):
cam = cv2.VideoCapture(ID)
# get a frame
ret, frame = cam.read()
if ret == False:
ID += 1
# print(ID)
# print("X")
else:
print(ID)
print("V")
break
各种debug报错
error: ......libusb_claim_interface_ ...... usb设备资源被占用,上次运行后未能释放
error: ......could not claim interface 0 usb......设备资源被占用,上次运行后未能释放
error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' usb 设备资源被占用,上次运行后未能释放
error: ....._src.empty() in function 'cv::cvtColor' ...... 设备资源被占用,上次运行后未能释放
参考资料:usb 细节和定义
https://www.keil.com/pack/doc/mw/USB/html/_u_s_b__endpoints.html
网友评论