A Step-by-Step Guide to Running YOLOv8 on Windows

Part II: Configuring Confidence Values and Advanced Features

New Terra
5 min readSep 21, 2023

In the first part of this series, we learned how to set up YOLOv8 on Windows and perform object detection on images. Now, let’s dive deeper into YOLOv8’s capabilities and explore advanced features. In this guide, we’ll cover configuring confidence values, saving bounding box information, hiding labels and confidence values, segmentation, and exporting models in ONNX format.

Setting the Confidence Value

without setting a confidence

YOLOv8 allows you to set a confidence threshold for object detection. This threshold determines the minimum confidence required for an object to be detected and labeled in the output.

To set the confidence value, navigate to the folder containing YOLOv8-related files using the command prompt:

cd path_to_your_YOLOv8_folder

Next, run YOLOv8 to detect objects in an image without setting a confidence value:

yolo task=detect mode=predict model=yolov8n.pt source=image.jpg

By default, YOLOv8 may detect objects with varying confidence levels.

To set a specific confidence threshold, such as 0.8, use the following command:

yolo task=detect mode=predict model=yolov8n.pt source=image.jpg conf=0.8

With this command, YOLOv8 will only label and identify objects with a confidence value greater than or equal to 0.8.

With a confidence = 0. 8
YOLOv8n summary: 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs
image 1/1 D:\GitHub\YOLOv8\Implementation\image.jpg: 448x640 4 persons, 104.6ms
Speed: 0.0ms pre-process, 104.6ms inference, 2.6ms postprocess per image at shape (1, 3, 640, 640)
Results saved to runs\detect\predict3

Saving Bounding Box Information

YOLOv8 allows you to save the bounding box information for detected objects. This information is useful for further analysis and processing.

To save the bounding box information, run the following command:

yolo task=detect mode=predict model=yolov8n.pt source=image.jpg save_txt=True
The Labels folder is where the coordinates are saved. The text gives you the class and the 4 coordinates for the bounding box.

The labels and coordinates for the bounding boxes will be saved in the “Labels” folder within the YOLOv8 directory. Each text file contains the class and the four coordinates defining the bounding box.

Cropping from detection

yolo task=detect mode=predict model=yolov8n.pt source=image.jpg save_txt=True save_crop=True
crops folder→ persons folder→ result images

Removing or Hiding Labels and Confidence Values

Sometimes, you may want to hide the labels and confidence values in the detection results. To do this, use the following command:

yolo task=detect mode=predict model=yolov8n.pt source=image.jpg hide_labels=True hide_conf=True

This command will generate output without labels or confidence values, making it suitable for specific applications.

The result of hiding labels and confidence | When run without training the model it will have the result on the right. This will be addressed in a later article and fixing the result.

Segmentation

YOLOv8 also supports image segmentation, which involves creating masks and bounding boxes for objects detected in an image.

To perform segmentation, change the task and model and run the following command:

yolo task=segment mode=predict model=yolov8n-seg.pt source=image.jpg
The results can be found by going to runs → segment → predict

The segmentation results can be found in the “runs” directory under the “segment” and “predict” subfolders.

Displaying Your Output

To view the segmentation output, use the following command:

yolo task=segment mode=predict model=yolov8n-seg.pt source=image.jpg hide_labels=True hide_conf=True

This command generates segmentation results without labels or confidence values.

Results

Note that we are not doing anything with our camera just yet, if you want to see what happens go ahead and run the following code:

# Run YOLOv8 to detect objects in a web cam don't do this yet,
# we still need to do a bit more.
yolo task=segment mode=predict model=yolov8n-seg.pt source=0 show=True

ONNX Format

ONNX stands for Open Neural Network Exchange. It is an open-source file format and ecosystem developed by Microsoft and several other major tech companies, including Facebook and AWS. ONNX is designed to facilitate interoperability and portability of machine learning models across different frameworks and hardware platforms.

YOLOv8 allows you to export models in the ONNX format, which is useful for integration with other applications or frameworks.

To export a YOLOv8 model in ONNX format, use the following command:

yolo task=detect mode=export model=yolov8n.pt format=onnx

The exported ONNX model will be created in your YOLOv8 folder.

Created after this is run.

One-Line Command

For convenience, you can create a Python script named ‘prediction.py’ with the following code:

from ultralytics import YOLO
# Initialize YOLO with the Model Name
model = YOLO("yolov8n.pt")
# Predict Method Takes all the parameters of the Command Line Interface
model.predict(source='image.jpg', save=True, conf=0.5, save_txt=True)

Save this script in the folder containing the image you want to predict. Then, execute the script using the Anaconda command prompt:

python prediction.py

This one-line command simplifies the process of running predictions using YOLOv8.

Success
prediction result

ONNX format for One-Line

We may want this since ONNX serves as a bridge between different deep learning frameworks and hardware platforms, making it easier for developers and researchers to collaborate, share models, and deploy them across a wide range of devices. This flexibility and interoperability have made ONNX a valuable tool in the machine-learning ecosystem.

from ultralytics import YOLO

#Initialize YOLO with the Model Name
model = YOLO("yolov8n.pt")

##Predict Method Takes all the parameters of the Command Line Interface
# model.predict(source='image.jpg', save=True, conf=0.5, save_txt=True)

model.export(format="onnx")
Created after this is run.

Conclusion

In this guide, we explored advanced features and configurations of YOLOv8 on Windows, including setting confidence values, saving bounding box information, hiding labels and confidence values, segmentation, and exporting models in ONNX format. These capabilities make YOLOv8 a versatile tool for a wide range of object detection and segmentation tasks. In future articles, we will address training your own dataset and fine-tuning YOLOv8 for specific applications. Stay tuned for more YOLOv8 tutorials, as I continue to build my training dataset.

If you want to know more about me, feel free to connect with me on LinkedIn

--

--

New Terra

I'm Pat Guillen, founder of New Terra. Exploring new frontiers in gaming and real-world challenges