ターミナルでの推論結果の表示

Sixel example of image in Terminal

libsixelのWebサイトからの画像。

動機

When connecting to a remote machine, normally visualizing image results is not possible or requires moving data to a local device with a GUI. The VSCode integrated terminal allows for directly rendering images. This is a short demonstration on how to use this in conjunction with ultralytics with prediction results.

警告

Only compatible with Linux and MacOS. Check the VSCode repository, check Issue status, or documentation for updates about Windows support to view images in terminal with sixel.

統合ターミナルを使用して画像を表示するためのVSCode互換プロトコルは、sixeliTermです。このガイドではsixelプロトコルの使用方法を説明します。

プロセス

  1. まず、VSCodeで設定terminal.integrated.enableImagesterminal.integrated.gpuAccelerationを有効にする必要があります。

    "terminal.integrated.gpuAcceleration": "auto" # "auto" is default, can also use "on"
    "terminal.integrated.enableImages": true

    VSCode enable terminal images setting

  2. Install the python-sixel library in your virtual environment. This is a fork of the PySixel library, which is no longer maintained.

    pip install sixel
  3. モデルをロードして推論を実行し、結果をプロットして変数に格納します。推論の引数や結果の操作に関する詳細は、予測モードのページを参照してください。

    from ultralytics import YOLO
    
    # Load a model
    model = YOLO("yolo26n.pt")
    
    # Run inference on an image
    results = model.predict(source="ultralytics/assets/bus.jpg")
    
    # Plot inference results
    plot = results[0].plot()  # (1)!
    1. 使用可能な引数については、プロットメソッドのパラメータを参照してください。
  4. 次に、OpenCVを使用してnp.ndarraybytesデータに変換します。その後、io.BytesIOを使用して「ファイルのような」オブジェクトを作成します。

    import io
    
    import cv2
    
    # Results image as bytes
    im_bytes = cv2.imencode(
        ".png",  # (1)!
        plot,
    )[1].tobytes()  # (2)!
    
    # Image bytes as a file-like object
    mem_file = io.BytesIO(im_bytes)
    1. 他の画像拡張子を使用することも可能です。
    2. 返されるインデックス1のオブジェクトのみが必要です。
  5. SixelWriterインスタンスを作成し、.draw()メソッドを使用してターミナルに画像を描画します。

    from sixel import SixelWriter
    
    # Create sixel writer object
    w = SixelWriter()
    
    # Draw the sixel image in the terminal
    w.draw(mem_file)

推論結果の例

YOLO inference results displayed in terminal

危険

ビデオやアニメーションGIFフレームでこの例を使用することはテストされていません。ご自身の責任でお試しください。

コード例全体

import io

import cv2
from sixel import SixelWriter

from ultralytics import YOLO

# Load a model
model = YOLO("yolo26n.pt")

# Run inference on an image
results = model.predict(source="ultralytics/assets/bus.jpg")

# Plot inference results
plot = results[0].plot()  # (3)!

# Results image as bytes
im_bytes = cv2.imencode(
    ".png",  # (1)!
    plot,
)[1].tobytes()  # (2)!

mem_file = io.BytesIO(im_bytes)
w = SixelWriter()
w.draw(mem_file)
  1. 他の画像拡張子を使用することも可能です。
  2. 返されるインデックス1のオブジェクトのみが必要です。
  3. 使用可能な引数については、プロットメソッドのパラメータを参照してください。

ヒント

ターミナル上の画像表示を「消去」するには、clearを使用する必要がある場合があります。

FAQ

macOSまたはLinuxのVSCodeターミナルでYOLOの推論結果を表示するにはどうすればよいですか?

macOSまたはLinuxのVSCodeターミナルでYOLOの推論結果を表示するには、以下の手順に従ってください。

  1. 必要なVSCode設定を有効にします:

    "terminal.integrated.enableImages": true
    "terminal.integrated.gpuAcceleration": "auto"
  2. sixelライブラリをインストールします:

    pip install sixel
  3. YOLOモデルをロードして推論を実行します:

    from ultralytics import YOLO
    
    model = YOLO("yolo26n.pt")
    results = model.predict(source="path_to_image")
    plot = results[0].plot()
  4. 推論結果の画像をバイトデータに変換し、ターミナルに表示します:

    import io
    
    import cv2
    from sixel import SixelWriter
    
    im_bytes = cv2.imencode(".png", plot)[1].tobytes()
    mem_file = io.BytesIO(im_bytes)
    SixelWriter().draw(mem_file)

詳細については、予測モードページをご覧ください。

なぜsixelプロトコルはLinuxとmacOSでしか動作しないのですか?

sixelプロトコルが現在LinuxとmacOSでのみサポートされているのは、これらのプラットフォームがsixelグラフィックスと互換性のあるネイティブのターミナル機能を備えているためです。sixelを使用したWindowsのターミナルグラフィックスサポートは現在開発中です。Windowsの互換性に関する最新情報については、VSCodeのIssueステータスドキュメントを確認してください。

VSCodeターミナルで画像を表示する際に問題が発生した場合はどうすればよいですか?

sixelを使用してVSCodeターミナルで画像を表示する際に問題が発生した場合は、以下を確認してください:

  1. 必要なVSCodeの設定が有効になっていることを確認します:

    "terminal.integrated.enableImages": true
    "terminal.integrated.gpuAcceleration": "auto"
  2. sixelライブラリがインストールされているか確認します:

    pip install sixel
  3. 画像データの変換とプロットのコードにエラーがないか確認してください。例:

    import io
    
    import cv2
    from sixel import SixelWriter
    
    im_bytes = cv2.imencode(".png", plot)[1].tobytes()
    mem_file = io.BytesIO(im_bytes)
    SixelWriter().draw(mem_file)

問題が解決しない場合は、VSCodeリポジトリを参照し、追加のガイダンスについてはプロットメソッドのパラメータセクションをご覧ください。

YOLOはsixelを使用してターミナルでビデオの推論結果を表示できますか?

sixelを使用してターミナルでビデオの推論結果やアニメーションGIFフレームを表示することは現在テストされておらず、サポートされていない可能性があります。まずは静止画像から開始して互換性を確認することをお勧めします。パフォーマンスの制約を考慮し、ビデオ結果の表示はご自身の責任でお試しください。推論結果のプロットに関する詳細については、予測モードページをご覧ください。

python-sixelライブラリの問題をトラブルシューティングするにはどうすればよいですか?

python-sixelライブラリの問題をトラブルシューティングするには、以下を行ってください:

  1. 仮想環境にライブラリが正しくインストールされていることを確認します:

    pip install sixel
  2. 必要なPythonおよびシステムの依存関係があるか確認します。

  3. 追加のドキュメントやコミュニティサポートについては、python-sixel GitHubリポジトリを参照してください。

  4. SixelWriterの使用方法や画像データの変換手順など、コードに潜在的なエラーがないか再確認してください。

YOLOモデルとsixelの統合に関する詳細なサポートについては、エクスポートおよび予測モードのドキュメントページを参照してください。

コメント