Getting Started with Python and Camera

From The Imaging Source Knowledgebase
Jump to navigation Jump to search

With Python, as well as with C, C++ and C# you can do everything needed:

  • Display a video stream
  • Capture single images, manual and automatically
  • Capture video files
  • Set all camera properties

Windows:

Installation: python3 -m pip install imagingcontrol4

Driver: The GenTL Producer for your The Imaging Source Camera from https://www.theimagingsource.com/en-us/support/download/

Documentation: IC Imaging Control 4 Python Library

Samples: [https://github.com/TheImagingSource/ic4-examples/tree/master/python

Sample script for grabbing an image:

import imagingcontrol4 as ic4
ic4.Library.init()

# Create a Grabber object
grabber = ic4.Grabber()

# Open the first available video capture device
first_device_info = ic4.DeviceEnum.devices()[0]
grabber.device_open(first_device_info)

# Set the resolution to 640x480
grabber.device_property_map.set_value(ic4.PropId.WIDTH, 640)
grabber.device_property_map.set_value(ic4.PropId.HEIGHT, 480)

# Create a SnapSink. A SnapSink allows grabbing single images (or image sequences) out of a data stream.
sink = ic4.SnapSink()
# Setup data stream from the video capture device to the sink and start image acquisition.
grabber.stream_setup(sink, setup_option=ic4.StreamSetupOption.ACQUISITION_START)

try:
    # Grab a single image out of the data stream.
    image = sink.snap_single(1000)

    # Print image information.
    print(f"Received an image. ImageType: {image.image_type}")

    # Save the image.
    image.save_as_bmp("test.bmp")

except ic4.IC4Exception as ex:
    print(ex.message)
	
# Stop the data stream.
grabber.stream_stop()

Linux:

Installation: At https://www.theimagingsource.com/en-us/support/download/ in chapter “SDKs”. Download “tiscamera” for your Linux platform.

Driver: not needed.

Documentation: tiscamera

Samples: https://github.com/TheImagingSource/tiscamera/tree/master/examples/python and https://github.com/TheImagingSource/Linux-tiscamera-Programming-Samples/tree/master/python

Sample script for live stream:

import time
import sys
import gi

gi.require_version("Gst", "1.0")

from gi.repository import Gst

def main():

    Gst.init(sys.argv)  # init gstreamer

    serial = None

    pipeline = Gst.parse_launch("tcambin name=bin "
                                " ! videoconvert"
                                " ! ximagesink sync=false")

    # retrieve the bin element from the pipeline
    camera = pipeline.get_by_name("bin")

    # serial is defined, thus make the source open that device
    if serial is not None:
        camera.set_property("serial", serial)

    pipeline.set_state(Gst.State.PLAYING)

    print("Press Ctrl-C to stop.")

    # We wait with this thread until a
    # KeyboardInterrupt in the form of a Ctrl-C
    # arrives. This will cause the pipline
    # to be set to state NULL
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        pass
    finally:
        pipeline.set_state(Gst.State.NULL)

if __name__ == "__main__":
    main()

Additional information for PI5, which may also is necessary for PI4:

Packets to be installed:

sudo apt install python3-gi python3-gst-1.0

Additionally OpenCV can be installed

sudo apt install python3-opencv

This shows, how to do that with apt. It should work similar with pip3. It is not a good idea to mix apt and pip3 installations.

For further questions, please use our contact form