From f45da3f02c2f1b29ae7ef77ffa4b3849d895a8b4 Mon Sep 17 00:00:00 2001 From: Eelke Visser Date: Sat, 9 Nov 2024 18:19:34 +0000 Subject: [PATCH 1/2] Change to the picamera2 library --- acquire.py | 80 ++++++++++++++++++++++++------------------ configuration.ini-dist | 11 +++--- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/acquire.py b/acquire.py index c787ca7..3a07f7b 100755 --- a/acquire.py +++ b/acquire.py @@ -15,31 +15,32 @@ import configparser import argparse + + # Capture images from pi def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, cfg): - from picamerax.array import PiRGBArray - from picamerax import PiCamera + # Use the Picamera2 lib + from picamera2 import Picamera2 # Intialization first = True slow_CPU = False # Initialize cv2 device - camera = PiCamera(sensor_mode=2) - camera.resolution = (nx, ny) - # Turn off any thing automatic. - camera.exposure_mode = "off" - camera.awb_mode = "off" - # ISO needs to be 0 otherwise analog and digital gain won't work. - camera.iso = 0 - # set the camea settings - camera.framerate = cfg.getfloat(camera_type, "framerate") - camera.awb_gains = (cfg.getfloat(camera_type, "awb_gain_red"), cfg.getfloat(camera_type, "awb_gain_blue")) - camera.analog_gain = cfg.getfloat(camera_type, "analog_gain") - camera.digital_gain = cfg.getfloat(camera_type, "digital_gain") - camera.shutter_speed = cfg.getint(camera_type, "exposure") - - rawCapture = PiRGBArray(camera, size=(nx, ny)) + picam2 = Picamera2() + picam2.configure(picam2.create_preview_configuration(main={"format": 'BGR888', "size": (nx, ny)})) + picam2.start() + + # Turn off anything automatic. + picam2.set_controls({ + "AeEnable": False, + "AwbEnable": False, + "ExposureTime": cfg.getint(camera_type, "exposure"), + "AnalogueGain": cfg.getfloat(camera_type, "analog_gain"), + "ColourGains": (cfg.getfloat(camera_type, "awb_gain_red"), cfg.getfloat(camera_type, "awb_gain_blue")), + "FrameRate": cfg.getfloat(camera_type, "framerate") + }) + # allow the camera to warmup time.sleep(0.1) @@ -59,29 +60,35 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c # Get frames i = 0 - for frameA in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): - + while True: + #reduce logging level, the camera stack shows too much debug. + logger.setLevel(logging.INFO) + # Store start time - t0 = float(time.time()) - # grab the raw NumPy array representing the image, then initialize the timestamp - frame = frameA.array + t0 = float(time.time()) + + # Obtain the image from the camera + im = picam2.capture_array() # Compute mid time t = (float(time.time()) + t0) / 2 - + + # Restore log level + logger.setLevel(logging.DEBUG) + # Skip lost frames - if frame is not None: + if im is not None: # Convert image to grayscale z = np.asarray(cv2.cvtColor( - frame, cv2.COLOR_BGR2GRAY)).astype(np.uint8) - # optionally rotate the frame by 2 * 90 degrees. + im, cv2.COLOR_BGR2GRAY)).astype(np.uint8) + # optionally rotate the frame by 2 * 90 degrees. # z = np.rot90(z, 2) - + # Display Frame - if live is True: - cv2.imshow("Capture", z) + if live is True: + cv2.imshow("Capture", im) cv2.waitKey(1) - + # Store results if first: z1[:, :, i] = z @@ -89,14 +96,17 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c else: z2[:, :, i] = z t2[i] = t - - # clear the stream in preparation for the next frame - rawCapture.truncate(0) + # count up to nz frames, then break out of the for loop. i += 1 if i >= nz: break - + + # Show current camera settings + #metadata = picam2.capture_metadata() + #controls = {c: metadata[c] for c in ["ExposureTime", "AnalogueGain", "ColourGains"]} + #logger.debug(controls) + if first: buf = 1 else: @@ -116,7 +126,7 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c finally: # End capture logger.info("Capture: %s - Exiting" % reason) - camera.close() + picam2.close() diff --git a/configuration.ini-dist b/configuration.ini-dist index 6f8832e..d67f31c 100644 --- a/configuration.ini-dist +++ b/configuration.ini-dist @@ -38,12 +38,11 @@ device_id = 0 # Device ID nx = 800 # Camera horizontal pixel count ny = 600 # Camera vertical pixel count nframes = 100 # Number of frames for each image -framerate = 5 # Take 5 frames per second -exposure = 200000 # Exposure time in us -awb_gain_red = 2 # Gain for red. -awb_gain_blue = 2.3 # Gain for blue -analog_gain = 16 # Analog gain -digital_gain = 64 # Digital gain +framerate = 10 # Take 10 frames per second +exposure = 100000 # Exposure time in us +awb_gain_red = 3.2 # Gain for red. +awb_gain_blue = 1.5 # Gain for blue +analog_gain = 1024 # Combination of analog and digital gain [CV2] device_id = 0 # Device ID From eca658fbbf662de5f834dd695c8e9e3db5107430 Mon Sep 17 00:00:00 2001 From: Eelke Visser Date: Sun, 10 Nov 2024 19:55:47 +0000 Subject: [PATCH 2/2] Swap red and blue setting. Fix gains. Minor improvements. --- README.md | 2 +- acquire.py | 19 +++++++++---------- configuration.ini-dist | 6 +++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6966f28..840c3b6 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ With the dependencies installed, we can install STVID using the following comman ```bash sudo apt install python-is-python3 python3-pip # Install python3 and pip cd $HOME/software # Goto directory -git clone https:/github.com/cbassa/stvid.git # Clone STVID repository +git clone https://github.com/cbassa/stvid.git # Clone STVID repository cd $HOME/software/stvid # Goto directory pip install -r requirements.txt # Install python requirements ``` diff --git a/acquire.py b/acquire.py index 3a07f7b..3a0fa86 100755 --- a/acquire.py +++ b/acquire.py @@ -27,7 +27,7 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c slow_CPU = False # Initialize cv2 device - picam2 = Picamera2() + picam2 = Picamera2(device_id) picam2.configure(picam2.create_preview_configuration(main={"format": 'BGR888', "size": (nx, ny)})) picam2.start() @@ -35,12 +35,16 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c picam2.set_controls({ "AeEnable": False, "AwbEnable": False, + "FrameDurationLimits": (100, 200000), "ExposureTime": cfg.getint(camera_type, "exposure"), "AnalogueGain": cfg.getfloat(camera_type, "analog_gain"), - "ColourGains": (cfg.getfloat(camera_type, "awb_gain_red"), cfg.getfloat(camera_type, "awb_gain_blue")), + "ColourGains": (cfg.getfloat(camera_type, "awb_gain_blue"), cfg.getfloat(camera_type, "awb_gain_red")), "FrameRate": cfg.getfloat(camera_type, "framerate") }) + #reduce logging level, the camera stack shows too much debug. + Picamera2.set_logging(Picamera2.INFO) + # allow the camera to warmup time.sleep(0.1) @@ -61,8 +65,6 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c # Get frames i = 0 while True: - #reduce logging level, the camera stack shows too much debug. - logger.setLevel(logging.INFO) # Store start time t0 = float(time.time()) @@ -73,9 +75,6 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c # Compute mid time t = (float(time.time()) + t0) / 2 - # Restore log level - logger.setLevel(logging.DEBUG) - # Skip lost frames if im is not None: # Convert image to grayscale @@ -103,9 +102,9 @@ def capture_pi(image_queue, z1, t1, z2, t2, nx, ny, nz, tend, device_id, live, c break # Show current camera settings - #metadata = picam2.capture_metadata() - #controls = {c: metadata[c] for c in ["ExposureTime", "AnalogueGain", "ColourGains"]} - #logger.debug(controls) + metadata = picam2.capture_metadata() + controls = {c: metadata[c] for c in ["ExposureTime", "FrameDuration", "AnalogueGain", "DigitalGain", "ColourGains"]} + logger.debug(controls) if first: buf = 1 diff --git a/configuration.ini-dist b/configuration.ini-dist index d67f31c..c89fb8c 100644 --- a/configuration.ini-dist +++ b/configuration.ini-dist @@ -40,9 +40,9 @@ ny = 600 # Camera vertical pixel count nframes = 100 # Number of frames for each image framerate = 10 # Take 10 frames per second exposure = 100000 # Exposure time in us -awb_gain_red = 3.2 # Gain for red. -awb_gain_blue = 1.5 # Gain for blue -analog_gain = 1024 # Combination of analog and digital gain +awb_gain_red = 2.0 # Gain for red. +awb_gain_blue = 2.8 # Gain for blue +analog_gain = 90 # Combination of analog and digital gain. Max 90 for HQ Camera. [CV2] device_id = 0 # Device ID