Meet YOLO26: next-gen vision AI.

Link to this sectionVSCodeターミナルでYOLOの推論結果を表示する方法#

Sixel example of image in Terminal

libsixelウェブサイトの画像。

Link to this section目的#

SSHでリモートマシンに接続している場合、画像結果を通常通りに可視化することは不可能か、GUIを持つローカルデバイスにデータを転送する必要があります。VSCodeの統合ターミナルは画像を直接レンダリングできるため、ファイルをラップトップにコピーすることなく、推論を実行したその場所で推論結果を確認できます。このガイドでは、セットアップの有効化、コードの組み込み手順を説明し、よくある質問に回答します。

LinuxおよびmacOSのみ

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 プロトコルの使用方法を説明します。

Link to this sectionプロセス#

  1. まず、VSCodeで terminal.integrated.enableImages および terminal.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)

Link to this section推論結果の例#

YOLO inference results displayed in terminal

動画とGIFは未検証です

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

Link to this section完全なコード例#

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 コマンドを使用する必要がある場合があります。

Link to this sectionよくある質問 (FAQ)#

Link to this sectionmacOSまたは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="ultralytics/assets/bus.jpg")
    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)

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

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

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

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

何も表示されない場合は、順番に以下のチェックを行ってください:

  1. Confirm both terminal.integrated.enableImages and terminal.integrated.gpuAcceleration are enabled, as shown in the Process section, then restart the integrated terminal so the settings take effect.
  2. スクリプトが実行されている仮想環境に sixel がインストールされていることを確認します (pip install sixel)。
  3. Make sure plot is a valid np.ndarray before encoding it, since cv2.imencode raises an error if the image is empty or not a valid array. See the plot method parameters for the values results[0].plot() accepts.

問題が解決しない場合は、ターミナルの画像サポート状況についてVSCodeリポジトリに相談してください。

Link to this sectionYOLOはsixelを使用してターミナルに動画の推論結果を表示できますか?#

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

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

The sixel package is a fork of the unmaintained PySixel library and is a thin Python wrapper over Pillow (PIL). If import sixel fails or SixelWriter().draw() raises an error, confirm the package installed into your active virtual environment, ensure Pillow is available, and check the python-sixel GitHub repository for platform-specific notes. For more on generating the image you pass to draw(), see the predict mode documentation.

コメント