在终端中查看推理结果
图片来自 libsixel 网站。
动机
连接到远程机器时,通常无法可视化图像结果,或者需要将数据传输到带有图形用户界面(GUI)的本地设备。VSCode 集成终端允许直接渲染图像。这是一个关于如何将其与 ultralytics 及 预测结果 结合使用的简短演示。
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 图形兼容的本地终端能力。Windows 对使用 sixel 的终端图形支持仍在开发中。有关 Windows 兼容性的更新,请查看 VSCode 问题状态 和 文档。
如果在 VSCode 终端中显示图像时遇到问题怎么办?
如果在 VSCode 终端中使用 sixel 显示图像时遇到问题:
-
确保已启用 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的使用和图像数据转换步骤。