2
0

OMRON SENTECカメラ Python Buildup (1) grab.py

Last updated at Posted at 2024-05-24

こちらの資料を読んでください。

基本的なカメラ操作と画像取得の方法 - StApiを使って

この記事では、StApiを使用してカメラに接続し、画像データを取得する基本的な方法を紹介します。以下のコードは、カメラから画像データを取得し、情報を表示する基本的な操作を示しています。

コードの概要

このサンプルコードでは、次のポイントを示します:

  • StApiの初期化
  • カメラへの接続
  • メインスレッドでの画像データの取得
import stapipy as st

# Number of images to grab
number_of_images_to_grab = 100

try:
    # Initialize StApi before using.
    st.initialize()

    # Create a system object for device scan and connection.
    st_system = st.create_system()

    # Connect to first detected device.
    st_device = st_system.create_first_device()

    # Display DisplayName of the device.
    print('Device=', st_device.info.display_name)

    # Create a datastream object for handling image stream data.
    st_datastream = st_device.create_datastream()

    # Start the image acquisition of the host (local machine) side.
    st_datastream.start_acquisition(number_of_images_to_grab)

    # Start the image acquisition of the camera side.
    st_device.acquisition_start()

    # A while loop for acquiring data and checking status
    while st_datastream.is_grabbing:
        # Create a localized variable st_buffer using 'with'
        # Warning: if st_buffer is in a global scope, st_buffer must be
        #          assign to None to allow Garbage Collector release the buffer
        #          properly.
        with st_datastream.retrieve_buffer() as st_buffer:
            # Check if the acquired data contains image data.
            if st_buffer.info.is_image_present:
                # Create an image object.
                st_image = st_buffer.get_image()
                # Display the information of the acquired image data.
                print("BlockID={0} Size={1} x {2} First Byte={3}".format(
                      st_buffer.info.frame_id,
                      st_image.width, st_image.height,
                      st_image.get_image_data()[0]))
            else:
                # If the acquired data contains no image data.
                print("Image data does not exist.")

    # Stop the image acquisition of the camera side
    st_device.acquisition_stop()

    # Stop the image acquisition of the host side
    st_datastream.stop_acquisition()

except Exception as exception:
    print(exception)

コードの詳細解説

1. 必要なモジュールのインポート
import stapipy as st

stapipyモジュールをインポートして、StApiの機能を利用できるようにします。

2. 取得する画像の数を設定
number_of_images_to_grab = 100

取得する画像の数を指定します。この例では100枚の画像を取得します。

3. StApiの初期化
st.initialize()

StApiを初期化します。このステップはStApiのすべての機能を使用する前に必須です。

4. システムオブジェクトの作成
st_system = st.create_system()

システムオブジェクトを作成します。これはデバイススキャンと接続に使用されます。

5. 最初に検出されたデバイスに接続
st_device = st_system.create_first_device()

最初に検出されたデバイスに接続します。

6. デバイス名の表示
print('Device=', st_device.info.display_name)

接続されたデバイスの名前を表示します。

7. データストリームオブジェクトの作成
st_datastream = st_device.create_datastream()

画像ストリームデータを処理するためのデータストリームオブジェクトを作成します。

8. 画像取得の開始(ローカル側)
st_datastream.start_acquisition(number_of_images_to_grab)

ホスト(ローカルマシン)側での画像取得を開始します。

9. 画像取得の開始(カメラ側)
st_device.acquisition_start()

カメラ側での画像取得を開始します。

10. データ取得と表示
while st_datastream.is_grabbing:
    with st_datastream.retrieve_buffer() as st_buffer:
        if st_buffer.info.is_image_present:
            st_image = st_buffer.get_image()
            print("BlockID={0} Size={1} x {2} First Byte={3}".format(
                  st_buffer.info.frame_id,
                  st_image.width, st_image.height,
                  st_image.get_image_data()[0]))
        else:
            print("Image data does not exist.")

ループ内でバッファからデータを取得し、画像データの情報を表示します。データが存在しない場合は、その旨を表示します。

11. 画像取得の停止
st_device.acquisition_stop()
st_datastream.stop_acquisition()

カメラ側とホスト側の画像取得を停止します。

12. エラーハンドリング
except Exception as exception:
    print(exception)

例外が発生した場合、その詳細を表示します。

まとめ

このサンプルコードは、StApiを使用してカメラに接続し、画像データを取得するための基本的な手順を示しています。これにより、カメラの制御と画像データの取得方法を理解し、さらに高度な画像処理やリアルタイム処理に応用する基礎を学ぶことができます。

このコードを基にして、さらに高度な画像処理やリアルタイムアプリケーションを開発するための第一歩として活用してください。

関連資料

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0