【超详细】【YOLOV8使用说明】一套框架解决CV的5大任务:目标检测、分割、姿势估计、跟踪和分类任务【含源码】(1)https://developer.aliyun.com/article/1536304
3.3 追踪任务
代码如下:【与目标检测不同的是,每个物体有一个ID。】
from ultralytics import YOLO # Load a model model = YOLO('yolov8n.pt',task='detect') # model = YOLO('yolov8n-seg.pt') # Track with the model results = model.track(source="1.mp4", show=True)
3.4 姿态检测任务
姿态检测(图片)代码
from ultralytics import YOLO import cv2 # Load a model model = YOLO('yolov8n-pose.pt') # load a pretrained model (recommended for training) results = model('./ultralytics/assets/bus.jpg') res = results[0].plot() cv2.imshow("YOLOv8 Inference", res) cv2.waitKey(0)
姿态检测(视频)代码
import cv2 from ultralytics import YOLO # Load the YOLOv8 model model = YOLO('yolov8n-pose.pt', task='pose') # Open the video file video_path = "1.mp4" cap = cv2.VideoCapture(video_path) # Loop through the video frames while cap.isOpened(): # Read a frame from the video success, frame = cap.read() if success: # Run YOLOv8 inference on the frame results = model(frame) # Visualize the results on the frame annotated_frame = results[0].plot() # Display the annotated frame cv2.imshow("YOLOv8 Inference", annotated_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord("q"): break else: # Break the loop if the end of the video is reached break # Release the video capture object and close the display window cap.release() cv2.destroyAllWindows()
4. 模型中相关参数及结果说明
4.1模型预测可以设置的参数
results = model(source= ‘./ultralytics/assets/bus.jpg’)
此处可以设置许多不同的参数,参数说明如下:
4.2 results[0].plot()图形展示可以设置的参数说明
results= model(img) res_plotted = results[0].plot() cv2.imshow("result", res_plotted)
4.3 模型支持的图片与视频格式
图片格式:
视频格式:
4.4 各任务检测结果results信息说明
上述各任务中的检测结果results均为一个列表,每一个元素为result对象,包含以下属性,不同任务中使用的属性不相同。详细说明如下:
Results.boxes:表示Boxs对象,具有属性和操作边界框的方法
Results.masks:用于获取分割相关信息
Results.probs:表示预测各类别的概率
Results.orig_img:表示内存中加载的原始图像
Results.path:表示输入图像路径的路径
results = model("./ultralytics/assets/bus.jpg") for result in results: **# Detection 目标检测** result.boxes.xyxy # box with xyxy format, (N, 4) result.boxes.xywh # box with xywh format, (N, 4) result.boxes.xyxyn # box with xyxy format but normalized, (N, 4) result.boxes.xywhn # box with xywh format but normalized, (N, 4) result.boxes.conf # confidence score, (N, 1) result.boxes.cls # cls, (N, 1) **# Segmentation 分割** result.masks.data # masks, (N, H, W) result.masks.xy # x,y segments (pixels), List[segment] * N result.masks.xyn # x,y segments (normalized), List[segment] * N **# Classification 分类** result.probs # cls prob, (num_class, )
总结
由于篇幅原因,本文只是介绍了如何使用预训练模型进行相关的任务检测,关于模型的训练及其他相关内容,后续有时间再进行更新,感兴趣的小伙伴,可以点赞关注我~谢谢
。