How-to by @Jesse
This How To made use of this Harvesters documentation.
Harvesters is a library that you can use to get an image from a GenICam compatible camera (such as the Genie nano). For more information, check out their official GitHub
I’m assuming you are using Jupyter Notebook from anaconda.
Here is a step by step walkthrough:
Create a specific anaconda environment
It can be usefull to create a specific anaconda environment, just in case you somehow corrupt your environment. In that case you can just delete it and start over.
In this How to, I use python version 3.8 for which I know Harvesters work. It should also totally work with more recent versions, but if it doesn’t version 3.8 should at least work.
- Open anaconda prompt.
- Type in the following commands. The prompt should start with (base):
conda create -n {environment name} python=3.8
conda activate {environment name}
Download the necessary libraries
Now that we have a specific genicam environment to work in, we can start downloading the necessary libraries. The prompt should now start with (genicam) instead of (base)
- Type the following commands.
conda install ipython
python -m pip install harvesters
It should say somethin like this:
You’ll probably also want to download matplotlib to show your taken images and opencv (the contrib library is used for whitebalance later in the How To).
python -m pip install matplotlib opencv-python opencv-contrib-python
- To be able to select this environment as a kernel in Jupyter Notebook, you’ll need to make a new kernel referring to this environment. To do this, enter the following commands.
conda install ipykernel
python -m ipykernel install --user --name {environment name} --display-name "{
The name that will be displayed when selecting the kernel}"
You should now be able to select this environment as a kernel in Jupyter Notebook.
- You should be done with installing libraries. Enter the following command to exit the environment.
conda deactivate
Downloading a GenTL Producer
For the Harvesters to be able to acquire images, we need a GenTL Producer. This producer is a good option.
- Download and install the latest version of ImpactAcquire for your machine.
- To find your GenTL Producer: go to
C:\Program Files\Balluff\ImpactAcquire
and search for*.cti
. It should be called something likemvGenTLProducer.cti
. Note down this exact path, you’ll need it later!
Actually acquiring an image
You should have everything installed to start getting an image. With the following code, you should be able to take a image and display it.
This code block you only need to run once:
from harvesters.core import Harvester
import matplotlib.pyplot as plt
import numpy as np
import cv2
from genicam.gentl import TimeoutException
# Global declaration of h and buffer
h = None
buffer = None
# Custom exception
class deviceListException(Exception):
pass
# Initialize the harvester
def initHarvester():
global h
h = Harvester()
h.add_file(r"{path/to/.cti/file}")
h.update()
if len(h.device_info_list) == 0:
raise deviceListException("Device list is empty")
#Initialize the ImageAcquirer
def initIA():
global ia
ia = h.create()
ia.start()
return ia
# Make a photo
def foto(ia):
global buffer # Make the buffer global to access it
buffer = ia.fetch(timeout=3000)
# Check if you received something in the buffer
if buffer is None:
raise ValueError("No buffer received")
component = buffer.payload.components[0]
width = component.width
height = component.height
img = component.data.reshape((height, width)) # Shape the buffer into an image
# Check if the image is empty
if img is None:
raise ValueError("Image is empty")
return img
try:
initHarvester() # Initialize Harvester
except deviceListException as e:
print(e)
This second block, you’ll need to run every single time you want an image:
try:
ia = initIA() # Initialize ImageAcquirer
img = foto(ia) # Take a photo
# Genie Nano uses the Bayer-RG color format
rgb_img = cv2.cvtColor(img, cv2.COLOR_BAYER_RG2RGB)
# Whitebalance the image
wb = cv2.xphoto.createSimpleWB()
corrected_img = wb.balanceWhite(rgb_img)
# Display the whitebalanced image
plt.figure(figsize=(8, 6))
plt.imshow(cv2.cvtColor(corrected_img, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB, because whitebalanced image is in the BGR format
plt.title('Whitebalanced image')
plt.axis('off')
plt.show()
except TimeoutException as e:
print(e)
except ValueError as e:
print(e)
except Exception as e:
print(f"Something else went wrong: {e}")
finally:
# Queue the buffer for the next image
if buffer is not None:
buffer.queue()
# Close the connection with the camera
if ia is not None:
ia.stop()
ia.destroy()