From 05dc3170954385a4eb8da630501b31863baf223b Mon Sep 17 00:00:00 2001 From: Romain Bazile Date: Tue, 24 Nov 2020 17:25:57 +0100 Subject: [PATCH] imager: add support for white balance --- scripts/planktoscope/imager.py | 29 ++++++++++++++++++++++++++++- scripts/planktoscope/light.py | 23 ++++++++++++++--------- scripts/planktoscope/raspimjpeg.py | 27 +++++++++++++++++++++++++++ scripts/raspimjpeg/bin/raspimjpeg | Bin 84520 -> 84520 bytes scripts/raspimjpeg/raspimjpeg.conf | 2 +- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/scripts/planktoscope/imager.py b/scripts/planktoscope/imager.py index 81e4628..580a8dd 100644 --- a/scripts/planktoscope/imager.py +++ b/scripts/planktoscope/imager.py @@ -126,7 +126,7 @@ class ImagerProcess(multiprocessing.Process): """This class contains the main definitions for the imager of the PlanktoScope""" @logger.catch - def __init__(self, stop_event, iso=100, shutter_speed=500): + def __init__(self, stop_event, iso=200, shutter_speed=20): """Initialize the Imager class Args: @@ -188,6 +188,12 @@ class ImagerProcess(multiprocessing.Process): self.__iso = iso self.__shutter_speed = shutter_speed self.__exposure_mode = "fixedfps" + self.__white_balance = "off" + self.__white_balance_gain = ( + 200, + 140, + ) # Those values were tested on a HQ camera to give a whitish background + self.__base_path = "/home/pi/data/img" # Let's make sure the base path exists if not os.path.exists(self.__base_path): @@ -231,6 +237,24 @@ class ImagerProcess(multiprocessing.Process): "A timeout has occured when setting the exposure mode, trying again" ) self.__camera.exposure_mode = self.__exposure_mode + time.sleep(0.1) + + try: + self.__camera.white_balance = self.__white_balance + except TimeoutError as e: + logger.error( + "A timeout has occured when setting the white balance mode, trying again" + ) + self.__camera.white_balance = self.__white_balance + time.sleep(0.1) + + try: + self.__camera.white_balance_gain = self.__white_balance_gain + except TimeoutError as e: + logger.error( + "A timeout has occured when setting the white balance gain, trying again" + ) + self.__camera.white_balance_gain = self.__white_balance_gain logger.success("planktoscope.imager is initialised and ready to go!") @@ -324,6 +348,9 @@ class ImagerProcess(multiprocessing.Process): "acq_camera_iso": self.__iso, "acq_camera_shutter_speed": self.__shutter_speed, } + # TODO add here the field size metadata + # For cam HQ 4,15mm x 3,14mm, résolution 1µm/px + # For cam 2.1 2.31mm x 1,74mm, résolution 0.7µm/px # Concat the local metadata and the metadata from Node-RED self.__global_metadata = {**local_metadata, **nodered_metadata} diff --git a/scripts/planktoscope/light.py b/scripts/planktoscope/light.py index d4852e0..8ae0af4 100644 --- a/scripts/planktoscope/light.py +++ b/scripts/planktoscope/light.py @@ -8,7 +8,7 @@ # Library to send command over I2C for the light module on the fan import smbus import RPi.GPIO -import subprocess +import subprocess # nosec # define the bus used to actuate the light module on the fan bus = smbus.SMBus(1) @@ -22,25 +22,26 @@ rgb_off_reg = 0x07 ################################################################################ # LEDs functions ################################################################################ + + +def i2c_update(): + # Update the I2C Bus in order to really update the LEDs new values + subprocess.Popen("i2cdetect -y 1".split(), stdout=subprocess.PIPE) # nosec + + def setRGB(R, G, B): """Update all LED at the same time""" bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x01, R & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x02, G & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x03, B & 0xFF) - - # Update the I2C Bus in order to really update the LEDs new values - cmd = "i2cdetect -y 1" - subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) + i2c_update() def setRGBOff(): """Turn off the RGB LED""" bus.write_byte_data(DEVICE_ADDRESS, 0x07, 0x00) - - # Update the I2C Bus in order to really update the LEDs new values - cmd = "i2cdetect -y 1" - subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) + i2c_update() def setRGBEffect(effect): @@ -88,6 +89,10 @@ def light(state): RPi.GPIO.output(21, RPi.GPIO.LOW) +## Wait message: Green +## Actuate message: White +## Pumping message: Blue + # This is called if this script is launched directly if __name__ == "__main__": # TODO This should be a test suite for this library diff --git a/scripts/planktoscope/raspimjpeg.py b/scripts/planktoscope/raspimjpeg.py index 1095a3b..06b3c38 100644 --- a/scripts/planktoscope/raspimjpeg.py +++ b/scripts/planktoscope/raspimjpeg.py @@ -25,6 +25,7 @@ class raspimjpeg(object): self.__binary = "/home/pi/PlanktonScope/scripts/raspimjpeg/bin/raspimjpeg" self.__statusfile = "/dev/shm/mjpeg/status_mjpeg.txt" self.__pipe = "/home/pi/PlanktonScope/scripts/raspimjpeg/FIFO" + self.__sensor_name = "" # make sure the status file exists and is empty if not os.path.exists(self.__statusfile): @@ -374,6 +375,32 @@ class raspimjpeg(object): ) raise ValueError + @property + def white_balance_gain(self): + return self.__white_balance_gain + + @white_balance_gain.setter + def white_balance_gain(self, gain): + """Change the camera white balance gain + + The gain value should be a int between 0 and 300. By default the camera + is set to use 150 both for the red and the blue gain. + + Args: + gain (tuple of int): Red gain and blue gain to use + """ + logger.debug(f"Setting the white balance mode to {gain}") + if (0 < gain[0] < 300) and (0 < gain[1] < 300): + self.__white_balance_gain = gain + self.__send_command( + f"ag {self.__white_balance_gain[0]} {self.__white_balance_gain[1]}" + ) + else: + logger.error( + f"The camera white balance gain specified ({gain}) is not valid" + ) + raise ValueError + @property def image_quality(self): return self.__image_quality diff --git a/scripts/raspimjpeg/bin/raspimjpeg b/scripts/raspimjpeg/bin/raspimjpeg index 24d3f8cf0fffe30860353ce29b64a96a0cd06172..9c197a93c129d27029eccec79f80ac185689b5e8 100755 GIT binary patch delta 43 zcmV+`0M!4elm)1i1+aht6v`auV$ngxX#*rj^IyK~5VIl3zY /dev/null 2>&1;rm "%s";) & # image_width 3280 image_height 2464 -image_quality 25 +image_quality 80 #time lapse interval 0.1 sec units tl_interval 30