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[0].boxes
- arrxy = results[0].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[midpoints[:, 1].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[midpoints[:, 1].argsort()]).astype(int)
- print("x,y:",rounded_n_sorted_arr[:])
複製代碼
結果圖:
座標:
- [[ 16 290]
- [412 491]
- [740 626]
- [283 631]
- [146 651]
- [ 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: [[412 491]]
- 0.0
- tensor([[ 47.5999, 398.8344, 244.2552, 903.1386]], device='cuda:0')
- 準確率 0.873720109462738
- x,y: [[146 651]]
- 0.0
- tensor([[670.3670, 376.9174, 810.0000, 875.0829]], device='cuda:0')
- 準確率 0.8693700432777405
- x,y: [[740 626]]
- 0.0
- tensor([[220.5713, 405.7240, 344.5589, 857.2506]], device='cuda:0')
- 準確率 0.819391667842865
- x,y: [[283 631]]
- 11.0
- tensor([[7.7698e-02, 2.5441e+02, 3.2119e+01, 3.2465e+02]], device='cuda:0')
- 準確率 0.44594067335128784
- x,y: [[ 16 290]]
- 0.0
- tensor([[3.2650e-02, 5.4988e+02, 6.4001e+01, 8.6930e+02]], 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[0].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
|