import cv2
import numpy as np

def main():
    # Open a connection to the camera (0 is usually the default camera)
    cap = cv2.VideoCapture(0)

    # Check if the camera opened successfully
    if not cap.isOpened():
        print("Error: Could not open video device.")
        return

    # Set video frame width and height (optional)
    #cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    #cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

    for i in range(0,10): # it gives time to camera to adjust, necessary to avoid that the image is very dark
        # Capture frame-by-frame

        #cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        #cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
        ret, frame = cap.read()

    # If frame is read correctly, ret is True
    if not ret:
        print("Error: Failed to capture image.")

    # Display the resulting frame
    #cv2.imshow('Video Feed', frame)

    # Save the current frame as a JPEG filesudo apt install -y python3-opencv
    filename = 'captured_image.jpg'
    cv2.imwrite(filename, frame)

    # Release the capture when done
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
