157 lines
6.4 KiB
Python
157 lines
6.4 KiB
Python
# Creation Date: 02.03.2022
|
|
# Author: Kenan Gömek
|
|
# This script takes pictures with Picameras VideoPort like it will be used to work with OpenCV and saves it with OpenCV to have the real use case pictures.
|
|
# This script is designed for shooting manually images with 'i'
|
|
# Press 'q' to exit
|
|
# Change camera parameters with v,b,n,m,x,c,o. See code for more information.
|
|
|
|
|
|
import cv2 as cv
|
|
import picamera
|
|
from picamera.array import PiRGBArray
|
|
from fractions import Fraction
|
|
|
|
import time
|
|
from datetime import datetime
|
|
import os
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
# Define camera settings
|
|
|
|
|
|
SENSOR_MODE = 4 # corresponding sensor mode to resolution 1640x1232
|
|
OUTPUT_RESOLUTION = (416, 320) # (1640x1232)/4=(410,308) --> needs to be divisible by 16 --> 416x320
|
|
|
|
|
|
AWB_MODE = 'off' # Auto white balance mode
|
|
AWB_GAINS = (1.395, 1.15) # White Balance Gains to have colours read correctly: (red, blue). Int, floar or fraction are valid.
|
|
BRIGHTNESS = 25 # sets the brightness setting of the camera. default is 50. [0-100]
|
|
#the brighter, the brighter the LEDs and the higher the RGB values and vice versa!
|
|
CONTRAST = 100 # sets the contrast setting of the camera. The default value is 0. [-100 ... 100]
|
|
|
|
SHUTTER_SPEED = 30000 # [µs]
|
|
|
|
ISO = 320 # ISO value
|
|
EXPOSURE_MODE = 'off'
|
|
FRAMERATE = 25 # frames per second. 40 fps is max for sensor mode 4
|
|
|
|
SLEEP_TIME = 2 # Time for sleep-mode for the camera in seconds. My default: 2 s
|
|
|
|
# Define Funcions
|
|
def get_frames_from_camera():
|
|
# Initialise Camera
|
|
print('Initalise Camera...')
|
|
with picamera.PiCamera() as camera:
|
|
with PiRGBArray(camera) as output:
|
|
# Set camera settings
|
|
camera.sensor_mode = SENSOR_MODE # force camera into desired sensor mode
|
|
camera.resolution = OUTPUT_RESOLUTION # frame will be resized from GPU to this resolution. No CPU usage!
|
|
camera.framerate = FRAMERATE
|
|
|
|
camera.awb_mode = AWB_MODE
|
|
camera.awb_gains = AWB_GAINS
|
|
|
|
camera.iso = ISO
|
|
camera.shutter_speed = SHUTTER_SPEED
|
|
# it was found that, you have to set the right shutter speed at the first initalisation of the current runtime of the program.
|
|
# The gains (analog, digital) will adjust to this set up.
|
|
# After the gains are fixed, they will never change! even if you change the shutter speed during the runtime.
|
|
# To get consistent brightness values, set the right shutter speed at initalisation once.
|
|
|
|
time.sleep(SLEEP_TIME) # wait for iso gains and digital_gain and analog_gain to settle before fixing the gains with exposure_mode = off
|
|
camera.exposure_mode = EXPOSURE_MODE
|
|
|
|
time.sleep(1) # wait before applying brightness and contrast
|
|
camera.brightness = BRIGHTNESS
|
|
camera.contrast = CONTRAST
|
|
time.sleep(SLEEP_TIME) # Camera warm-up time to apply settings
|
|
|
|
# camera.start_preview() # show camera preview through PiCamera interface
|
|
# camera.annotate_frame_num=True # Controls whether the current frame number is drawn as an annotation.
|
|
|
|
print('Start caputure...')
|
|
|
|
for frameidx, frame in enumerate(camera.capture_continuous(output, format='bgr', use_video_port=True)):
|
|
start_processing = time.perf_counter()
|
|
|
|
framenumber = frameidx+1 # frameidx starts with 0, framenumber with 1
|
|
image = frame.array # raw NumPy array without JPEG encoding
|
|
|
|
#cv.imshow("Current Frame", image) # display the image without text
|
|
output.truncate(0) # clear the stream for next frame
|
|
|
|
# processing
|
|
|
|
|
|
|
|
# Only uncomment following code if you display the image. No errors if not commented, but higher fps if commented.
|
|
# if q is pressed, break from loop.
|
|
pressed_key = cv.waitKey(2) & 0xff
|
|
if pressed_key == ord('q'):
|
|
break
|
|
|
|
elif pressed_key == ord('v'): # increase shutterspeed by 5
|
|
shutter_speed = round(shutter_speed+5)
|
|
camera.shutter_speed = shutter_speed
|
|
time.sleep(2) # wait to shutter speed is applied before querying exposure speed
|
|
exposure_speed = camera.exposure_speed
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
print(f"retrieved shutter speed: {exposure_speed}")
|
|
|
|
elif pressed_key == ord('b'): # increase shutterspeed by 50
|
|
shutter_speed = round(shutter_speed+50)
|
|
camera.shutter_speed = shutter_speed
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
elif pressed_key == ord('n'): # increase shutterspeed by 500
|
|
shutter_speed = round(shutter_speed+500)
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
elif pressed_key == ord('m'): # max shutterspeed
|
|
shutter_speed = round(1/FRAMERATE*1e6)
|
|
camera.shutter_speed = shutter_speed
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
elif pressed_key == ord('x'): # decrease shutterspeed by 500
|
|
shutter_speed = round(shutter_speed-500)
|
|
camera.shutter_speed = shutter_speed
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
elif pressed_key == ord('c'): # decrease shutterspeed by 50
|
|
shutter_speed = round(shutter_speed-50)
|
|
camera.shutter_speed = shutter_speed
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
elif pressed_key == ord('o'): # set shutterspeed to 0
|
|
shutter_speed = 0
|
|
camera.shutter_speed = shutter_speed
|
|
print(f"shutter speed set to: {shutter_speed}")
|
|
|
|
end_processing = time.perf_counter()
|
|
time_processing = round(end_processing-start_processing, 2)
|
|
time_processing = time_processing*1000
|
|
print(f'processing time: {time_processing} ms')
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# main
|
|
def main():
|
|
# start capturing
|
|
get_frames_from_camera() # start capture
|
|
|
|
cv.destroyAllWindows()
|
|
print('Program finished')
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|