yolov8s yolov8x yolov8n等的区别
时间: 2023-11-16 16:04:45 浏览: 188
YOLOv8是一种目标检测算法,而YOLOv8s、YOLOv8x、YOLOv8n等则是不同版本的预训练权重文件。它们的区别在于模型的大小和精度。其中,YOLOv8s是最小的版本,速度最快,但精度相对较低;YOLOv8x是最大的版本,精度最高,但速度相对较慢;而YOLOv8n则是介于两者之间的版本,速度和精度都比较平衡。因此,在选择使用哪个版本的预训练权重文件时,需要根据具体的应用场景和需求来进行选择。
下面是一个使用YOLOv8s预训练权重文件进行目标检测的Python代码示例:
```python
import cv2
import numpy as np
# 加载模型和权重文件
net = cv2.dnn.readNetFromDarknet('yolov8s.cfg', 'yolov8s.weights')
# 加载类别标签
classes = []
with open('coco.names', 'r') as f:
classes = [line.strip() for line in f.readlines()]
# 加载图像
img = cv2.imread('test.jpg')
# 对图像进行预处理
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)
# 将预处理后的图像输入到模型中进行推理
net.setInput(blob)
outs = net.forward(net.getUnconnectedOutLayersNames())
# 解析输出结果
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * img.shape[1])
center_y = int(detection[1] * img.shape[0])
w = int(detection[2] * img.shape[1])
h = int(detection[3] * img.shape[0])
x = center_x - w // 2
y = center_y - h // 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
# 非极大值抑制
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 绘制检测结果
for i in indices:
i = i[0]
box = boxes[i]
x = box[0]
y = box[1]
w = box[2]
h = box[3]
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(img, classes[class_ids[i]], (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 显示检测结果
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文