Python 與相機入門指南

使用 Python以及 C, C++ 和C# ,您可以完成所有必要的操作:

  • 顯示影像串流

  • 擷取單張影像,包含手動與自動模式

  • 擷取影片檔案

  • 設定所有相機屬性

Windows:

安裝方式:python3 -m pip install imagingcontrol4

驅動程式:適用於您 The Imaging Source 相機的 GenTL Producer,可從以下路徑取得:https://www.theimagingsource.com/en-us/support/download/

說明文件: IC Imaging Control 4 Python Library

範例: https://github.com/TheImagingSource/ic4-examples/tree/master/python

單張影像抓取範例:

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:

安裝方式:請至 https://www.theimagingsource.com/en-us/support/download/ 的「SDKs」章節,下載適用於您 Linux 平台的

驅動程式:無需安裝。

說明文件: tiscamera

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

單張影像抓取範例:

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()

針對 Raspberry Pi 5 的補充資訊(可能也適用於 Raspberry Pi 4): 需安裝的軟體包:

sudo apt install python3-gi python3-gst-1.0

此外,也可以安裝 OpenCV

sudo apt install python3-opencv

這顯示了如何透過 apt 進行安裝。使用 pip3 的操作方式也類似。不過,將 apt 和 pip3 的安裝方式混用並非一個好主意。

如有進一步疑問,請使用我們的 contact form