
YOLOv8現(xiàn)在可以接受輸入很多,如下表所示。包括圖像、URL、PIL圖像、OpenCV、NumPy數(shù)組、Torch張量、CSV文件、視頻、目錄、通配符、YouTube視頻和視頻流。表格?指示了每個(gè)輸入源是否可以在流模式下使用,并給出了每個(gè)輸入源使用流模式的示例參數(shù)
預(yù)測(cè)參數(shù)
Key | Value | Description |
---|---|---|
source |
'ultralytics/assets' |
source directory for images or videos |
conf |
0.25 |
object confidence threshold for detection |
iou |
0.7 |
intersection over union (IoU) threshold for NMS |
half |
False |
use half precision (FP16) |
device |
None |
device to run on, i.e. cuda device=0/1/2/3 or device=cpu |
show |
False |
show results if possible |
save |
False |
save images with results |
save_txt |
False |
save results as .txt file |
save_conf |
False |
save results with confidence scores |
save_crop |
False |
save cropped images with results |
hide_labels |
False |
hide labels |
hide_conf |
False |
hide confidence scores |
max_det |
300 |
maximum number of detections per image |
vid_stride |
False |
video frame-rate stride |
line_width |
None |
The line width of the bounding boxes. If None, it is scaled to the image size. |
visualize |
False |
visualize model features |
augment |
False |
apply image augmentation to prediction sources |
agnostic_nms |
False |
class-agnostic NMS |
retina_masks |
False |
use high-resolution segmentation masks |
classes |
None |
filter results by class, i.e. class=0, or class=[0,2,3] |
boxes |
True |
Show boxes in segmentation predictions |
下面是每個(gè)參數(shù)的解釋?zhuān)?/p>
-
source
:輸入源的目錄,可以是圖像或視頻文件。 -
conf
:目標(biāo)檢測(cè)的對(duì)象置信度閾值。只有置信度高于此閾值的對(duì)象才會(huì)被檢測(cè)出來(lái)。默認(rèn)值為0.25
。 -
iou
:非極大值抑制(NMS)的交并比(IoU)閾值。用于在重疊較大的候選框中選擇最佳的檢測(cè)結(jié)果。默認(rèn)值為0.7
。 -
half
:是否使用半精度(FP16)進(jìn)行推理。半精度可以減少計(jì)算量,但可能會(huì)犧牲一些精度。默認(rèn)值為False
。 -
device
:模型運(yùn)行的設(shè)備,可以是cuda設(shè)備(cuda device=0/1/2/3)或CPU(device=cpu)。 -
show
:是否顯示檢測(cè)結(jié)果。如果設(shè)置為True
,則會(huì)在屏幕上顯示檢測(cè)到的對(duì)象。默認(rèn)值為False
。 -
save
:是否保存帶有檢測(cè)結(jié)果的圖像。如果設(shè)置為True
,則會(huì)將檢測(cè)結(jié)果保存為圖像文件。默認(rèn)值為False
。 -
save_txt
:是否將檢測(cè)結(jié)果保存為文本文件(.txt)。默認(rèn)值為False
。 -
save_conf
:是否將檢測(cè)結(jié)果與置信度分?jǐn)?shù)一起保存。默認(rèn)值為False
。 -
save_crop
:是否保存裁剪后的帶有檢測(cè)結(jié)果的圖像。默認(rèn)值為False
。 -
hide_labels
:是否隱藏標(biāo)簽。如果設(shè)置為True
,則在顯示檢測(cè)結(jié)果時(shí)不顯示對(duì)象標(biāo)簽。默認(rèn)值為False
。 -
hide_conf
:是否隱藏置信度分?jǐn)?shù)。如果設(shè)置為True
,則在顯示檢測(cè)結(jié)果時(shí)不顯示置信度分?jǐn)?shù)。默認(rèn)值為False
。 -
max_det
:每張圖像的最大檢測(cè)數(shù)。如果檢測(cè)到的對(duì)象數(shù)超過(guò)此值,將保留置信度高低來(lái)保留。默認(rèn)值為300
。 -
vid_stride
:視頻幀率步長(zhǎng)。默認(rèn)值為False
,表示使用默認(rèn)的幀率。 -
line_width
:邊界框的線(xiàn)寬。如果設(shè)置為None
,則根據(jù)圖像大小進(jìn)行自動(dòng)縮放。默認(rèn)值為None
。 -
visualize
:是否可視化模型特征。默認(rèn)值為False
。 -
augment
:是否對(duì)預(yù)測(cè)源應(yīng)用圖像增強(qiáng)。默認(rèn)值為False
。 -
agnostic_nms
:是否使用類(lèi)別無(wú)關(guān)的NMS。默認(rèn)值為False
。 -
retina_masks
:是否使用高分辨率的分割掩膜。默認(rèn)值為False
。 -
classes
:按類(lèi)別過(guò)濾結(jié)果??梢灾付▎蝹€(gè)類(lèi)別(例如class=0
)或多個(gè)類(lèi)別(例如class=[0,2,3]
)。默認(rèn)值為None
,表示不進(jìn)行類(lèi)別過(guò)濾。 -
boxes
:在分割預(yù)測(cè)中顯示邊界框。默認(rèn)值為True
。
使用Results對(duì)象
Results
對(duì)象包含以下組件:
Results.boxes
:用于操作邊界框的屬性和方法的對(duì)象
Results.masks
:用于索引掩膜或獲取分段坐標(biāo)的對(duì)象
Results.probs
:包含類(lèi)別概率或邏輯值的張量(tensor)
Results.orig_img
:加載在內(nèi)存中的原始圖像
Results.path
:包含輸入圖像的路徑
result對(duì)象默認(rèn)是torch.Tensor對(duì)象,也可以轉(zhuǎn)為其他對(duì)象
results = results.cuda()
results = results.cpu()
results = results.to('cpu')
results = results.numpy()
Boxes
Boxes對(duì)象可用于索引、操作邊界框,并將其轉(zhuǎn)換為不同的格式。Box格式轉(zhuǎn)換結(jié)果是緩存的,這意味著每個(gè)對(duì)象只計(jì)算一次,并且這些值將在將來(lái)的調(diào)用中重復(fù)使用。
results = model(img)
boxes = results[0].boxes
box = boxes[0] # returns one box
box.xyxy
Boxes 的屬性有
boxes.xyxy # xyxy 形式的目標(biāo)框, (N, 4)
boxes.xywh # xywh 形式的目標(biāo)框, (N, 4)
boxes.xyxyn # xyxy 形式的目標(biāo)框且歸一化, (N, 4)
boxes.xywhn # xywh 形式的目標(biāo)框且歸一化, (N, 4)
boxes.conf # 置信度的分?jǐn)?shù), (N, 1)
boxes.cls # 類(lèi)別, (N, 1)
boxes.data # 原始目標(biāo)框參數(shù)坐標(biāo) (x, y, w, h)、置信度以及類(lèi)別, (N, 6) or boxes.boxes
Masks
Masks可以被索引、操作修改、將Masks轉(zhuǎn)換為分割結(jié)果。也可以緩存段轉(zhuǎn)換操作。
results = model(inputs)
masks = results[0].masks # Masks object
masks.xy # x, y segments (pixels), List[segment] * N
masks.xyn # x, y segments (normalized), List[segment] * N
masks.data # raw masks tensor, (N, H, W) or masks.masks
Probs 置信度
包含了所有類(lèi)別的置信度
results = model(inputs)
results[0].probs # cls prob, (num_class, )
更多文檔可以參考 https://docs.ultralytics.com/reference/yolo/engine/results/
繪制結(jié)果
yolov8提供了plot函數(shù)繪制結(jié)果,可以繪制邊框,分割結(jié)果,分類(lèi)結(jié)果類(lèi)別等等。不在需要像yolov5那樣直接寫(xiě)后處理nms等,很方便。
res = model(img)
res_plotted = res[0].plot()
cv2.imshow("result", res_plotted)
Argument | Description |
---|---|
conf (bool) | 是否繪制檢測(cè)置信度得分。 |
line_width (int, optional) | 邊界框的線(xiàn)寬。如果為None,則根據(jù)圖像大小進(jìn)行縮放。 |
font_size (float, optional) | 文本的字體大小。如果為None,則根據(jù)圖像大小進(jìn)行縮放。 |
font (str) | 用于文本的字體。 |
pil (bool) | 是否使用PIL庫(kù)進(jìn)行圖像繪制。 |
example (str) | 要顯示的示例字符串。用于指示輸出的期望格式。 |
img (numpy.ndarray) | 繪制到另一個(gè)圖像上。如果為None,則繪制到原始圖像上。 |
labels (bool) | 是否繪制邊界框的標(biāo)簽。 |
boxes (bool) | 是否繪制邊界框。 |
masks (bool) | 是否繪制掩膜。 |
probs (bool) | 是否繪制分類(lèi)概率。 |
視頻流數(shù)據(jù)源
以下是使用OpenCV和YOLOv8在視頻幀上運(yùn)行推理的代碼。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-509159.html
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('yolov8n.pt')
# Open the video file
video_path = "path/to/your/video/file.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()
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-509159.html
到了這里,關(guān)于YOLOv8預(yù)測(cè)參數(shù)詳解(全面詳細(xì)、重點(diǎn)突出、大白話(huà)闡述小白也能看懂)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!