Skip to content

gp2

plantimager.gp2 Link

Implementation of a camera module based on gPhoto2.

Requires system library libgphoto2-dev & python package gphoto2.

sudo apt-get install libgphoto2-dev
pip install gphoto2

Camera Link

Camera()

Bases: AbstractCamera

gPhoto2 Camera object.

See Also

plantimager.hal.AbstractCamera

Examples:

>>> from plantimager.gp2 import Camera
>>> cam = Camera()
>>> # Grab a picture as an hal.DataItem:
>>> img = cam.grab(0)
>>> # Get the numpy array with RGB data:
>>> arr = img.channel("rgb").data
>>> arr.shape
>>> # Save the picture to a local file:
>>> img_file = cam.grab_write('gp2_img.jpg')
Source code in plantimager/gp2.py
65
66
67
68
def __init__(self):
    self.camera = None
    self.start()
    atexit.register(self.stop)

grab Link

grab(idx, metadata=None)

Grab a picture with gPhoto2.

Source code in plantimager/gp2.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def grab(self, idx: int, metadata: dict = None):
    """Grab a picture with gPhoto2. """
    # with tempfile.TemporaryDirectory as tmp:
    #     fname = os.path.join(tmp, "frame.jpg")
    #     self.grab_write(fname)
    #     data_item = DataItem(idx, metadata)
    #     data = imageio.imread(fname)
    #     data_item.add_channel("rgb", data)
    #     return data_item
    # Initialize an hal.DataItem object to return:
    data_item = DataItem(idx, metadata)
    # Capture
    file_path = self.camera.capture(0)
    camera_file = self.camera.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL)
    # Read data using 'get_data_and_size' which allocates its own buffer:
    file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file))
    # Open with ImageIO:
    data = imageio.imread(BytesIO(file_data))
    # Add data as RGB channel to hal.DataItem object:
    data_item.add_channel(self.channels()[0], data)
    return data_item

grab_write Link

grab_write(target)

Grab & save a picture with gPhoto2.

Source code in plantimager/gp2.py
107
108
109
110
111
112
113
def grab_write(self, target: str):
    """Grab & save a picture with gPhoto2. """
    file_path = self.camera.capture(0)
    camera_file = self.camera.file_get(file_path.folder, file_path.name,
                                       gp.GP_FILE_TYPE_NORMAL)
    gp.gp_file_save(camera_file, target)
    return target