Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8042

Beginners • Re: RaspberryPi camera code

$
0
0
The provided code looks mostly correct, but there are a few points to consider and some improvements to make. Here are the inline comments and necessary adjustments. The code below is generated and has been reviewed but not been tested. I will bring you up to speed in a short time.

Code:

from gpiozero import Buttonimport timeimport osfrom picamera2 import Picamera2from libcamera import controls# Initialize the cameracamera = Picamera2()# Create configurations for preview and still capturepreview_config = camera.create_preview_configuration()capture_config = camera.create_still_configuration()# Configure the camera with the preview configurationcamera.configure(preview_config)# Start the camera previewcamera.start(show_preview=True)# Define the output folder for captured imagesoutput_folder = "/home/raspberrypi/captured_image"os.makedirs(output_folder, exist_ok=True)# Set exposure time (example: 5000 microseconds = 0.005 seconds)camera.set_controls({'ExposureTime': 5000})# Set autofocus mode to manualcamera.set_controls({'AfMode': controls.AfModeEnum.Manual})# Set lens position for focus distance (example: 0.5 for 2 meters)camera.set_controls({'LensPosition': 0.5})# Define the path for the captured imageimage_path = os.path.join(output_folder, "image_test.jpg")# Capture the image with the still configurationcamera.switch_mode_and_capture_file(capture_config, image_path)# Close the cameracamera.close()
  • GPIO Button**: The `Button` import from `gpiozero` is not used in the code. If you intend to use a button to trigger the capture, you need to add the button logic.
  • Exposure Time**: Ensure that the exposure time value is appropriate for your lighting conditions.
  • Focus Distance**: The `LensPosition` value should be adjusted based on your specific requirements.

Example with Button Logic:
If you want to capture an image when a button is pressed, you can add the button logic as follows:

Code:

from gpiozero import Buttonimport timeimport osfrom picamera2 import Picamera2from libcamera import controls# Initialize the cameracamera = Picamera2()# Create configurations for preview and still capturepreview_config = camera.create_preview_configuration()capture_config = camera.create_still_configuration()# Configure the camera with the preview configurationcamera.configure(preview_config)# Start the camera previewcamera.start(show_preview=True)# Define the output folder for captured imagesoutput_folder = "/home/raspberrypi/captured_image"os.makedirs(output_folder, exist_ok=True)# Set exposure time (example: 5000 microseconds = 0.005 seconds)camera.set_controls({'ExposureTime': 5000})# Set autofocus mode to manualcamera.set_controls({'AfMode': controls.AfModeEnum.Manual})# Set lens position for focus distance (example: 0.5 for 2 meters)camera.set_controls({'LensPosition': 0.5})# Define the path for the captured imageimage_path = os.path.join(output_folder, "image_test.jpg")# Initialize the button (assuming it's connected to GPIO pin 17)button = Button(17)# Function to capture image when button is presseddef capture_image():    camera.switch_mode_and_capture_file(capture_config, image_path)    print(f"Image captured and saved to {image_path}")# Assign the capture function to the button press eventbutton.when_pressed = capture_image# Keep the script running to listen for button pressestry:    while True:        time.sleep(1)except KeyboardInterrupt:    pass# Close the cameracamera.close()

Statistics: Posted by gftrobots — Mon Jul 22, 2024 4:42 pm



Viewing all articles
Browse latest Browse all 8042

Trending Articles