使用Python 與 The Imaging Source 相機入門

出自The Imaging Source Knowledgebase Taiwanese
跳至導覽 跳至搜尋

使用 Python 以及 C、C++ 和 C#,您可以完成以下操作:

  • 顯示視訊串流
  • 手動和自動捕捉單張影像
  • 擷取視訊
  • 設定所有相機屬性

Windows:

  • 安裝 Imagingcontrol4 輸入指令安裝Imagingcontrol4
    python3 -m pip install imagingcontrol4
    
    或是在Visual Studio Python環境裡選擇安裝imagingcontrol4套件,如下圖:

擷取影像的範例:

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:

程式範例:

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