train代码
if __name__ == '__main__':
from ultralytics import YOLO
# Create a new YOLO model from scratch
model = YOLO(r'D:\pythonxm\ultralytics-main\ultralytics\cfg\models\v8\sandboxs.yaml')
# Load a pretrained YOLO model (recommended for training)
model = YOLO(r'D:\pythonxm\ultralytics-main\ultralytics\models\yolo\detect\yolov8n.pt')
# Train the model using the 'coco128.yaml' dataset for 3 epochs
results = model.train(data=r'D:\pythonxm\ultralytics-main\ultralytics\cfg\datasets\sandbox.yaml', epochs=100)
# Evaluate the model's performance on the validation set
results = model.val()
# # Perform object detection on an image using the model
# results = model('https://ultralytics.com/images/bus.jpg')
#
# # Export the model to ONNX format
# success = model.export(format='onnx')
detect代码
if __name__ == '__main__':
from ultralytics import YOLO
from PIL import Image
import cv2
model = YOLO(r"D:\pythonxm\ultralytics-main\runs\detect\train6\weights\best.pt")
# 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("bus.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])