import cv2

#Gstreamer Pipeline for using Arducam
gst_pipeline = (
    "libcamerasrc !"
    "video/x-raw,format=NV12,width=4624,height=3472 ! " #tried various resoltions but the zoom level and focus was not happening
    "videoconvert !"
    "videoscale !"
    "video/x-raw,format=NV12,width=640,height=480 !"
    "appsink"
)

print(gst_pipeline)
cap = cv2.VideoCapture(gst_pipeline)
#even tried using default OpenCV backend pipeline
#cap = cv2.VideoCapture('/dev/media0')
if not cap.isOpened():
    print("Failed to open video file with GStreamer pipeline")
    exit()

#both of these settings are not working
#cap.set(cv2.CAP_PROP_AUTOFOCUS, 1)
#cap.set(cv2.CAP_PROP_FOCUS, 1)

while True:
    ret,frame = cap.read()

    if not ret:
        print("Error!")
        break

    h, w = frame.shape[:2]
        # Create a BGR image
    bgr_image = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_NV12)
    #rs_frame = cv2.resize()

    cv2.imshow("Frame",bgr_image)
    #cv2.imshow("Frame",frame)
    
    cv2.waitKey(1)