Python 双目摄像机控制(windows + linux)

一、Windows

下载 Download libusb-win32-devel-filter-1.2.6.0.exe (libusb-win32)

安装,在弹出框中选择摄像机usb设备

pip install pyusb

pip install libusb

代码如下,注意如果报错要以管理员权限运行: 

import cv2
import usb.core

cam = cv2.VideoCapture(1)

dev = usb.core.find(idVendor=0x18e3, idProduct=0x5031)

dev.set_configuration()

# simulate the SET_CUR sequence
dev.ctrl_transfer(0x21, 0x01, 0x0800, 0x0600, [0x50, 0xff])
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')):
    ret, frame = cam.read()
    cv2.imshow("cam_test", frame)

    k = cv2.waitKey(18) & 0xFF
    kv = k - ord('0')
    #  print(kv)
    #  if press 1,2,3 or 4, change the 3d camera mode
    # 1右2左3红蓝4双目
    if kv in [1, 2, 3, 4]:
        dev.ctrl_transfer(0x21, 0x01, 0x0a00, 0x0600, [kv, 0x00])

 参考文章链接:

hack一个3D摄像头来解闷 - 简书

20220305CHUSEI双目摄像头windows驱动+python+openCV - 简书

二、Linux

需要安装libwebcam库,debian 系可以通过apt安装,centos的安装方法如下

Centos安装libwebcam库运行 uvcdynctrl 命令_Gaoithe的博客-CSDN博客

代码如下:

import time

import cv2
import numpy as np
import socket
import struct
import subprocess



# 打开摄像头,注意,执行命令之前摄像头必须read()一次,否则命令不生效
cap = cv2.VideoCapture(1)
ret, frame = cap.read()

camera_device = "/dev/video1"

# 1右2左3红蓝4双目
mode = "3"

commands = [
    f'uvcdynctrl -d {camera_device} -S 6:8  "(LE)0x50ff"',
    f'uvcdynctrl -d {camera_device} -S 6:15 "(LE)0x00f6"',
    f'uvcdynctrl -d {camera_device} -S 6:8  "(LE)0x2500"',
    f'uvcdynctrl -d {camera_device} -S 6:8  "(LE)0x5ffe"',
    f'uvcdynctrl -d {camera_device} -S 6:15 "(LE)0x0003"',
    f'uvcdynctrl -d {camera_device} -S 6:15 "(LE)0x0002"',
    f'uvcdynctrl -d {camera_device} -S 6:15 "(LE)0x0012"',
    f'uvcdynctrl -d {camera_device} -S 6:15 "(LE)0x0004"',
    f'uvcdynctrl -d {camera_device} -S 6:8  "(LE)0x76c3"',
    f'uvcdynctrl -d {camera_device} -S 6:10 "(LE)0x0{mode}00"',
]

# 执行每个命令
for command in commands:
    subprocess.run(command, shell=True)

while True:
    # 读取摄像头画面

    ret, frame = cap.read()
    
    .....#省略

参考文章:ros学习最便宜的双目Chusei 3d webcam在linux下用 uvcdynctrl命令控制 结贴_qq_278667286的博客-CSDN博客

你可能感兴趣的:(linux,运维,服务器)