class MyCamera(object):
    def __init__(self):
        pass

    def open_camera(self, device=0, width=-1, height=-1):
        self.cap = cv2.VideoCapture(device, cv2.CAP_V4L2)
        self.arducam_utils = ArducamUtils(device)
        # turn off RGB conversion
        if self.arducam_utils.convert2rgb == 0:
            self.cap.set(cv2.CAP_PROP_CONVERT_RGB,
                         self.arducam_utils.convert2rgb)
        # set width
        if width != -1:
            self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        # set height
        if height != -1:
            self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

        self.fix_orientation()

    def fix_orientation(self):
        _, sensor_id = self.arducam_utils.read_dev(
            ArducamUtils.FIRMWARE_SENSOR_ID_REG)
        print("0x{:X}".format(sensor_id))
        if sensor_id == 0x5647:
            self.arducam_utils.cvt_code = cv2.COLOR_BAYER_GB2BGR
            self.cap.grab()
            self.arducam_utils.write_sensor(0x3820, 0x42)
            self.arducam_utils.write_sensor(0x3821, 0x00)
        elif sensor_id == 0x0219:
            self.arducam_utils.cvt_code = cv2.COLOR_BAYER_RG2BGR
            self.cap.grab()
            self.arducam_utils.write_sensor(0x0172, 0x03)

    def get_framesize(self):
        width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        return (width, height)

    def close_camera(self):
        self.cap.release()
        self.cap = None

    def get_frame(self):
        ret, frame = self.cap.read()
        if not ret:
            return None
        return self.arducam_utils.convert(frame)
def resize(frame, dst_width):
    width = frame.shape[1]
    height = frame.shape[0]
    scale = dst_width * 1.0 / width
    return cv2.resize(frame, (int(scale * width), int(scale * height)))


cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
arducam_utils = ArducamUtils(0)
cap.set(cv2.CAP_PROP_CONVERT_RGB, arducam_utils.convert2rgb)
cv2.namedWindow("window")

while cap.isOpened():
    ret, frame = cap.read()
    frame = arducam_utils.convert(frame)
    resizedImg = resize(frame, 1280.0)
    #dimensions = resizedImg.shape

    image_height = resizedImg.shape[0]
    image_width = resizedImg.shape[1]
    left_img = frame[0:image_height, 0:(image_width // 2)]
    right_img = frame[0:image_height, (image_width // 2):image_width]

    stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
    #disparity = stereo.compute(left_img, right_img)

    #cv2.imshow('gray', disparity)

    cv2.imshow("left", left_img)
    cv2.imshow("right", right_img)