TShopping

 找回密碼
 註冊
搜索
查看: 539|回復: 0

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

[複製鏈接]
發表於 2023-10-5 23:56:30 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
1. 法一
  1. import os
  2. import numpy as np
  3. from ultralytics import YOLO
  4. os.environ["KMP_DUPLICATE_LIB_OK"]  =  "TRUE"

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

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

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


結果圖:

Yolov8 照片偵測 物體座標 python

Yolov8 照片偵測 物體座標  python


座標:
  1. [[ 16 290]
  2. [412 491]
  3. [740 626]
  4. [283 631]
  5. [146 651]
  6. [ 32 710]]
  7. 5.0
  8. image 1/1 D:\yolo\bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 83.9ms
  9. Speed: 3.0ms preprocess, 83.9ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 480)
  10. tensor([[ 22.3412, 228.0822, 802.0841, 754.3939]], device='cuda:0')
  11. 準確率 0.879738450050354
  12. x,y: [[412 491]]
  13. 0.0
  14. tensor([[ 47.5999, 398.8344, 244.2552, 903.1386]], device='cuda:0')
  15. 準確率 0.873720109462738
  16. x,y: [[146 651]]
  17. 0.0
  18. tensor([[670.3670, 376.9174, 810.0000, 875.0829]], device='cuda:0')
  19. 準確率 0.8693700432777405
  20. x,y: [[740 626]]
  21. 0.0
  22. tensor([[220.5713, 405.7240, 344.5589, 857.2506]], device='cuda:0')
  23. 準確率 0.819391667842865
  24. x,y: [[283 631]]
  25. 11.0
  26. tensor([[7.7698e-02, 2.5441e+02, 3.2119e+01, 3.2465e+02]], device='cuda:0')
  27. 準確率 0.44594067335128784
  28. x,y: [[ 16 290]]
  29. 0.0
  30. tensor([[3.2650e-02, 5.4988e+02, 6.4001e+01, 8.6930e+02]], device='cuda:0')
  31. 準確率 0.29976797103881836
  32. x,y: [[ 32 710]]
複製代碼

Yolov8 照片偵測 物體座標 python

Yolov8 照片偵測 物體座標  python


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

  8. print('模型可偵測項目的名稱與編號')
  9. print(model.names)
  10. print('模型偵測結果')
  11. boxes = result[0].boxes.cpu().numpy()
  12. for box in boxes:
  13.         print("分類編號:", box.cls)
  14.         print("座標位置:", box.xyxy)
  15.         print("機率:", box.conf)
複製代碼
  1. image 1/1 D:\yolo\bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 86.8ms
  2. Speed: 3.0ms preprocess, 86.8ms inference, 5.0ms postprocess per image at shape (1, 3, 640, 480)
  3. Results saved to runs\detect\predict21
  4. 模型可偵測項目的名稱與編號
  5. {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'}
  6. 模型偵測結果
  7. 分類編號: [          5]
  8. 座標位置: [[     22.341      228.08      802.08      754.39]]
  9. 機率: [    0.87974]
  10. 分類編號: [          0]
  11. 座標位置: [[       47.6      398.83      244.26      903.14]]
  12. 機率: [    0.87372]
複製代碼



bus-003.png


文章出處: NetYea 網頁設計

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



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-7-16 04:27 , Processed in 0.076064 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表