woff 發表於 2023-10-5 23:56:30

Yolov8 照片偵測後如何抓物體座標位置的方法

1. 法一
import os
import numpy as np
from ultralytics import YOLO
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

if __name__ == '__main__':
    model = YOLO("best.pt")

    results = model("bus.jpg")
    boxes = results.boxes
    arrxy = results.boxes.xyxy.cpu() #numpy只能用CPU運算
    coordinates = np.array(arrxy)
    x_coords = (coordinates[:, 0] + coordinates[:, 2]) / 2
    y_coords = (coordinates[:, 1] + coordinates[:, 3]) / 2
    midpoints = np.column_stack((x_coords, y_coords))
    rounded_n_sorted_arr = np.round(midpoints.argsort()]).astype(int)
    print(rounded_n_sorted_arr[:]) #列出圖片預測物體所有座標

    for box in boxes: #一項一項列出圖片預測物體座標
      print(box.cls.item()) # class id
      print(box.xyxy)# box coordinates (tensor)
      print("準確率",box.conf.item()) # confidence value)
      coordinates = np.array(box.xyxy.cpu()) #numpy只能用CPU運算
      x_coords = (coordinates[:, 0] + coordinates[:, 2]) / 2
      y_coords = (coordinates[:, 1] + coordinates[:, 3]) / 2
      midpoints = np.column_stack((x_coords, y_coords))
      rounded_n_sorted_arr = np.round(midpoints.argsort()]).astype(int)
      print("x,y:",rounded_n_sorted_arr[:])

結果圖:


座標:
[[ 16 290]




[ 32 710]]
5.0
image 1/1 D:\yolo\bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 83.9ms
Speed: 3.0ms preprocess, 83.9ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 480)
tensor([[ 22.3412, 228.0822, 802.0841, 754.3939]], device='cuda:0')
準確率 0.879738450050354
x,y: []
0.0
tensor([[ 47.5999, 398.8344, 244.2552, 903.1386]], device='cuda:0')
準確率 0.873720109462738
x,y: []
0.0
tensor([], device='cuda:0')
準確率 0.8693700432777405
x,y: []
0.0
tensor([], device='cuda:0')
準確率 0.819391667842865
x,y: []
11.0
tensor([], device='cuda:0')
準確率 0.44594067335128784
x,y: [[ 16 290]]
0.0
tensor([], device='cuda:0')
準確率 0.29976797103881836
x,y: [[ 32 710]]


法二、程式比法一簡單(速度慢數幾ms)程式碼
from ultralytics import YOLO
model = YOLO("best.pt")
result = model.predict(
    source="bus.jpg",
    mode="predict",
    save=True
)

print('模型可偵測項目的名稱與編號')
print(model.names)
print('模型偵測結果')
boxes = result.boxes.cpu().numpy()
for box in boxes:
      print("分類編號:", box.cls)
      print("座標位置:", box.xyxy)
      print("機率:", box.conf)
image 1/1 D:\yolo\bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 86.8ms
Speed: 3.0ms preprocess, 86.8ms inference, 5.0ms postprocess per image at shape (1, 3, 640, 480)
Results saved to runs\detect\predict21
模型可偵測項目的名稱與編號
{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
模型偵測結果
分類編號: [          5]
座標位置: [[   22.341      228.08      802.08      754.39]]
機率: [    0.87974]
分類編號: [          0]
座標位置: [[       47.6      398.83      244.26      903.14]]
機率: [    0.87372]





文章出處: NetYea 網頁設計

參考文章
https://medium.com/@batuhansener ... yolov8-682dcdc0f451
https://stackoverflow.com/questi ... ect-detection-model


頁: [1]
查看完整版本: Yolov8 照片偵測後如何抓物體座標位置的方法