玩一玩 yolo v11

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


记录体验 yolo v11 的过程:

1.下载镜像

docker pull ultralytics/ultralytics

这个镜像有 11 GB

2.进入镜像的 bash

docker run -it --name=yolov11_test -v ~/Pictures:/Pictures --ipc=host --cpus=2 -m=512m ultralytics/ultralytics:latest bash

3. 执行 yolo 命令行

cd /ultralytics
yolo detect predict model=yolo11n.pt source='/Pictures/bus.jpg'

对一张叫 bus.jpg 的图片进行目标检测。
输出:

image 1/1 /macos/Pictures/bus.jpg: 640x480 4 persons, 1 bus, 548.8ms

图中有 4 个人,一辆 bus.

4.使用python代码来对图片进行物体识别

# yolo_v11.py
import sys
import cv2
import numpy as np
from ultralytics import YOLO

def main():
    imgPath = sys.argv[1]
    img = cv2.imread(imgPath)
    image_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    model = YOLO("/ultralytics/yolo11n.pt")
    results = model(image_bgr)
    if results is not None and len(results) > 0:
        print(results[0].to_json())

if __name__ == '__main__':
    main()

执行:

python yolov11.py ./bus.jpg

返回如下的 json:

[
  {
    "name": "bus",
    "class": 5,
    "confidence": 0.93556,
    "box": {
      "x1": 21.09849,
      "y1": 230.98537,
      "x2": 806.54425,
      "y2": 734.20367
    }
  },
  {
    "name": "person",
    "class": 0,
    "confidence": 0.88089,
    "box": {
      "x1": 671.24689,
      "y1": 394.42633,
      "x2": 809.86993,
      "y2": 878.40442
    }
  },
  {
    "name": "person",
    "class": 0,
    "confidence": 0.86937,
    "box": {
      "x1": 47.11706,
      "y1": 400.43146,
      "x2": 236.89668,
      "y2": 904.49927
    }
  },
  {
    "name": "person",
    "class": 0,
    "confidence": 0.86492,
    "box": {
      "x1": 223.44214,
      "y1": 409.60806,
      "x2": 344.12372,
      "y2": 859.69666
    }
  },
  {
    "name": "person",
    "class": 0,
    "confidence": 0.5732,
    "box": {
      "x1": 0.0,
      "y1": 559.20795,
      "x2": 65.6593,
      "y2": 877.65045
    }
  }
]

Have Fun. 😃

posted on 2024-12-31 15:46  ahfuzhang  阅读(258)  评论(0)    收藏  举报