隐藏终端的 ultralytics model.predict() 输出

问题描述 投票:0回答:2

我有这个由

model.predict()

生成的输出
0: 480x640 1 Hole, 234.1ms
Speed: 3.0ms preprocess, 234.1ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)

0: 480x640 1 Hole, 193.6ms
Speed: 3.0ms preprocess, 193.6ms inference, 3.5ms postprocess per image at shape (1, 3, 640, 640)

...

如何隐藏终端的输出?

我在这个官方链接里查不到信息 https://docs.ultralytics.com/modes/predict/#arguments

python output stdout yolo yolov8
2个回答
0
投票

实现此目的的一种方法是在 os 模块和 contextlib 模块的帮助下将标准输出重定向到 dev/null (实际上会丢弃所有输出,您甚至看不到错误)

import os
from contextlib import redirect_stdout

with open(os.devnull, 'w') as devnull:
  with redirect_stdout(devnull):
    result = modle.predict

0
投票

Ultralytics 文档很遗憾不是最新的。正确的做法是:

from ultralytics import YOLO

model = YOLO('yolov8m-seg.pt')
results = model.predict(source='0', verbose=False)
for result in results:
    masks = result.masks.masks
    print(masks.shape)

注意

verbose=False
论点。这不会打印默认输出

...
0: 480x640 1 Hole, 193.6ms
Speed: 3.0ms preprocess, 193.6ms inference, 3.5ms postprocess per image at shape (1, 3, 640, 640)
...

仅在这种情况下:

...
torch.Size([4, 480, 640])
...
© www.soinside.com 2019 - 2024. All rights reserved.