Hide Ultralytics' Yolov8 model.predict() output from terminal
Asked Answered
W

2

6

I have this output that was generated by 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)

...

How do I hide the output from the terminal?

I can't find out the information in this official link: https://docs.ultralytics.com/modes/predict/#arguments

Wellfound answered 9/5, 2023 at 21:23 Comment(0)
I
15

The Ultralytics docs are sadly not up to date, as of today. The right way of doing this is:

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)

Notice the verbose=False argument. This won't print the default output

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

In this case only:

...
torch.Size([4, 480, 640])
...
Interdental answered 11/5, 2023 at 8:11 Comment(2)
Are there any sources for this and other extra argumentsNozzle
docs.ultralytics.com/modes/predict/#inference-argumentsInterdental
R
0

One way to achieve this is to redirect the standered output to dev/null (wich actually discards all outputs and you will not even see errors) with the help of os module and contextlib module as follow:

import os
from contextlib import redirect_stdout

with open(os.devnull, 'w') as devnull:
  with redirect_stdout(devnull):
    result = modle.predict
Regatta answered 9/5, 2023 at 21:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.