ターミナルでの推論結果の表示
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互換プロトコルは、sixelとiTermです。このガイドではsixelプロトコルの使用方法を説明します。
プロセス
-
まず、VSCodeで設定
terminal.integrated.enableImagesとterminal.integrated.gpuAccelerationを有効にする必要があります。"terminal.integrated.gpuAcceleration": "auto" # "auto" is default, can also use "on" "terminal.integrated.enableImages": true
-
Install the
python-sixellibrary in your virtual environment. This is a fork of thePySixellibrary, which is no longer maintained.pip install sixel -
モデルをロードして推論を実行し、結果をプロットして変数に格納します。推論の引数や結果の操作に関する詳細は、予測モードのページを参照してください。
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)!- 使用可能な引数については、プロットメソッドのパラメータを参照してください。
-
次に、OpenCVを使用して
np.ndarrayをbytesデータに変換します。その後、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のオブジェクトのみが必要です。
-
SixelWriterインスタンスを作成し、.draw()メソッドを使用してターミナルに画像を描画します。from sixel import SixelWriter # Create sixel writer object w = SixelWriter() # Draw the sixel image in the terminal w.draw(mem_file)
推論結果の例
ビデオやアニメーション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のオブジェクトのみが必要です。 - 使用可能な引数については、プロットメソッドのパラメータを参照してください。
ターミナル上の画像表示を「消去」するには、clearを使用する必要がある場合があります。
FAQ
macOSまたはLinuxのVSCodeターミナルでYOLOの推論結果を表示するにはどうすればよいですか?
macOSまたはLinuxのVSCodeターミナルでYOLOの推論結果を表示するには、以下の手順に従ってください。
-
必要なVSCode設定を有効にします:
"terminal.integrated.enableImages": true "terminal.integrated.gpuAcceleration": "auto" -
sixelライブラリをインストールします:
pip install sixel -
YOLOモデルをロードして推論を実行します:
from ultralytics import YOLO model = YOLO("yolo26n.pt") results = model.predict(source="path_to_image") plot = results[0].plot() -
推論結果の画像をバイトデータに変換し、ターミナルに表示します:
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ターミナルで画像を表示する際に問題が発生した場合は、以下を確認してください:
-
必要なVSCodeの設定が有効になっていることを確認します:
"terminal.integrated.enableImages": true "terminal.integrated.gpuAcceleration": "auto" -
sixelライブラリがインストールされているか確認します:
pip install sixel -
画像データの変換とプロットのコードにエラーがないか確認してください。例:
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ライブラリの問題をトラブルシューティングするには、以下を行ってください:
-
仮想環境にライブラリが正しくインストールされていることを確認します:
pip install sixel -
必要なPythonおよびシステムの依存関係があるか確認します。
-
追加のドキュメントやコミュニティサポートについては、python-sixel GitHubリポジトリを参照してください。
-
SixelWriterの使用方法や画像データの変換手順など、コードに潜在的なエラーがないか再確認してください。
YOLOモデルとsixelの統合に関する詳細なサポートについては、エクスポートおよび予測モードのドキュメントページを参照してください。