Sleinomagic
Sleinomagic

Reputation: 21

Using both front cameras of Vive Pro with OpenCV in Python

I seek to use the two cameras in the front of my HTC Vive Pro in order to implement SLAM using Stereo Vision. I would prefer if this was possible using Python, however, I cannot find a good way to open up both cameras (I can only open the right camera with cv2.VideoCapture(1)). The device is currently simply connected by a usb.

What I have so far is

import cv2

cv2.namedWindow("Camera 1")
cv2.namedWindow("Camera 2")

stereo = cv2.VideoCapture(1)

if stereo.isOpened():
    rval, frame = stereo.read()
else:
    rval = False

while rval:
    rval_left, left = stereo.retrieve(0)
    rval_right, right = stereo.retrieve(1)
    cv2.imshow("Camera 1", left)
    cv2.imshow("Camera 2", right)
    key = cv2.waitKey(20)
    if key == 27: 
        # exit on ESC
        break

cv2.destroyAllWindows()

But stereo is not really stereo as expected. cv2.VideoCapture(0) is the laptop webcam and all other cv2.VideoCapture(...) returns None. I hope somebody can help.

Upvotes: 2

Views: 331

Answers (1)

Milind Padalkar
Milind Padalkar

Reputation: 11

It seems by default the camera is set to capture at 640x480 resolution, but it does support 640x960 resolution (see: this and this).

A simple solution is below.

import cv2

cv2.namedWindow("Camera 1")
cv2.namedWindow("Camera 2")

stereo = cv2.VideoCapture(1)
width = stereo.get(cv2.CAP_PROP_FRAME_WIDTH)
height = stereo.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"default size: {width} x {height}")

stereo.set(cv2.CAP_PROP_FRAME_WIDTH, width)
stereo.set(cv2.CAP_PROP_FRAME_HEIGHT, height*2)

while stereo.isOpened():
    rval, frame = stereo.read()
    if not rval:
        break
    left  = frame[int(height):, :, ...]
    right = frame[:int(height), :, ...]
    cv2.imshow("Camera 1", left)
    cv2.imshow("Camera 2", right)
    key = cv2.waitKey(20)
    if key == 27: 
        # exit on ESC
        break

cv2.destroyAllWindows()

Upvotes: 0

Related Questions