import cv2
import numpy as np

def capture_and_split_camera(device=0, width=1856, height=800, fps=30):
    cap = cv2.VideoCapture(0,cv2.CAP_V4L2)
    # # cap.open('/dev/video0')
    # cap.open(0)
    # cap.open(0,apiPreference=cv2.CAP_DSHOW)
    # cap.set(6, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap.set(cv2.CAP_PROP_FPS, fps)

    if not cap.isOpened():
        print("Error: Could not open camera")
        return
    else:
        print("camera detected !")
        try:
            while True:
                ret, frame = cap.read()

                if not ret:
                    print("Error: Failed to capture frame")
                    break

                height, width = frame.shape[:2]
                left_half = frame[:, :width//2]
                right_half = frame[:, width//2:]

                split_frame = np.hstack((left_half ,right_half))

                cv2.imshow('Camera Frame', split_frame)

                key = cv2.waitKey(10)
                if key == 27:    # "q" 
                    break

        finally:
            cap.release()
            cv2.destroyAllWindows()

def main():
    capture_and_split_camera(
        device=0,      # Camera device node
        width=1856,    # Frame width
        height=800,    # Frame height
        fps=30         # Frames per second
    )
    # capture_and_split_camera(
    #     device=0,      # Camera device node
    #     width=1856/2,    # Frame width
    #     height=800/2,    # Frame height
    #     fps=30*2         # Frames per second
    # )
if __name__ == "__main__":
    main()