Yolov8 target detection

Yolov8 target detection


1. Prepare the dataset

Yolov8 only supports data in yolo format, so the data set format needs to be adjusted to

datasets
     |
      images
            |
            train
            	|
            	000000.jpg
            	000001.jpg
            test
            	|
            	100000.jpg
            	100001.jpg
       labels
            |
             train
            	|
            	000000.txt
            	000001.txt
            test
            	|
            	100000.txt
            	100001.txt

2. Source code download configuration

2.1 Download library

pip install ultralytics

2.2 Modify configuration

Create a new model configuration file yolov8.yaml, add data path and category in it:

train: "/home/dxfcv/workspace/sunsirui/label/dataset/train/images"
val: "/home/dxfcv/workspace/sunsirui/label/dataset/test/images"
nc: 3
names: ["cement","metal","plastics"]

2.3 Training

Specific parameters can be found in the link

yolo task=detect mode=train model=yolov8s.pt data=yolov8.yaml batch=8 epochs=100 imgsz=640 workers=16 device=0

After training, a folder will be generated to store all the results

2.4 Verification

yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=yolov8.yaml device=0

2.5 Testing

yolo task=detect mode=predict model=runs/detect/train/weights/best.pt source=/home/dxfcv/workspace/sunsirui/label/dataset/images device=0

You can see the effect in the generated predict folder (like the marked effect)

2.6 Model export

yolo task=detect mode=export model=runs/detect/train/weights/best.pt format=onnx

2.7 Local testing

Use different methods (such as opencv) to adjust the picture to detect, and the result will appear in the corresponding folder

from ultralytics import YOLO
from PIL import Image
import cv2
 
model = YOLO("/home/dxfcv/workspace/sunsirui/label/dataset/runs/detect/train/weights/best.onnx")
# accepts all formats - image/dir/Path/URL/video/PIL/ndarray. 0 for webcam
# results = model.predict(source="0")
# results = model.predict(source="folder", show=True) # Display preds. Accepts all YOLO predict arguments
 
# # from PIL
# im1 = Image.open("bus.jpg")
# results = model.predict(source=im1, save=True)  # save plotted images
 
# from ndarray
im2 = cv2.imread("/home/dxfcv/workspace/sunsirui/label/dataset/images/camera2_200025.jpg")
results = model.predict(source=im2, save=True, save_txt=True)  # save predictions as labels
 
# from list of PIL/ndarray
# results = model.predict(source=[im1, im2]

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/131975060