Skip to content

Commit

Permalink
add image capturing script for picamera (#5)
Browse files Browse the repository at this point in the history
* add image capturing script for picamera

* linter fix

* fix bug and add more details in comment

* update foramtting and documentation

* add picamera test utility script

* Update docstring on venv instructions

* linter fixes, disable invalid-name

* move rc.local instructions from confluence to rc.local.copyme

* Fixup test_picam_settings.py

* Move rc.local commands to script file

* add comments and format picam.sh

* Move log directory creation into main function
  • Loading branch information
maxlou05 authored Nov 30, 2024
1 parent 2c66cb6 commit 2aa5d81
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
File renamed without changes.
34 changes: 34 additions & 0 deletions picam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Taking images with a Raspberry pi camera module 3 on a Rpi on the drone.
"""

import pathlib
import time

import picamera2


LOG_DIRECTORY_PATH = pathlib.Path("logs")
SAVE_PREFIX = str(pathlib.Path(LOG_DIRECTORY_PATH, "image_" + str(int(time.time())) + "_"))
DELAY = 1.0 # seconds


def main() -> None:
"""
Main loop for capturing and saving images.
"""
LOG_DIRECTORY_PATH.mkdir(parents=True, exist_ok=True)

picam2 = picamera2.Picamera2()
picam2.start(show_preview=False)

name_counter = 0
while True:
picam2.capture_file(f"{SAVE_PREFIX}{name_counter}.png")
name_counter += 1

time.sleep(DELAY)


if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions picam.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Get current Unix time to avoid collision
# TODO: Something more sophisticated (e.g. counter)
TIME="${date +%s}"
echo $TIME

# Picamera Python
cd /home/warg/image-collection # Update to actual path
touch logs/record_$TIME.log # To confirm script has run
source venv/bin/activate
python picam.py &
6 changes: 5 additions & 1 deletion rc.local.copyme
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

# OpenCV Python (preferred)
chmod +x /home/warg/image-collection/run.sh # Update to actual path
/home/warg/image-collection/run.sh # Update to actual path
/home/warg/image-collection/opencv.sh # Update to actual path

# FFMPEG video (backup)
chmod +x /home/warg/image-collection/video.sh
/home/warg/image-collection/video.sh

# Picamera2 (for use with RPi 5 and Picamera)
chmod +x /home/warg/image-collection/picam.sh
/home/warg/image-collection/picam.sh
35 changes: 35 additions & 0 deletions test_picam_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Utility script to test different picamera settings (for use on a Rpi5).
For available settings, see appendix C: https://datasheets.raspberrypi.com/camera/picamera2-manual.pdf
"""

import picamera2


if __name__ == "__main__":
picam2 = picamera2.Picamera2()

picam2.configure(picam2.create_preview_configuration())

# Example default settings for use with IR camera + IR filter. (only sees IR hotspot)
# Don't change Brightness (artificially makes everything whiter) or Contrast (doesn't look too good)
# picam2.set_controls({"ExposureTime": 250, "AnalogueGain": 64.0, "Brightness": 0.0, "Contrast": 1.0})
print(picam2.controls)

picam2.start(show_preview=True)

while True:
# Since preview already started, it does not block preview
command = input("Enter a config and value (e.g. 'ExposureTime 250'): ")

input_tokens = command.split()
if len(input_tokens != 2):
print("Invalid input")
continue

config, raw_value = input_tokens
value = int(raw_value) if config == "ExposureTime" else float(raw_value)

picam2.set_controls({command: value})
print("New controls set")
print(picam2.controls)

0 comments on commit 2aa5d81

Please sign in to comment.