• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
本地使用pycharm进行yolo推理

继上一篇在docker中使用yolo之后,开始本地通过代码跑yolo

参考:目标检测:YOLOv11(Ultralytics)环境配置,适合0基础纯小白,超详细

现在是2025.01.07,安装之后和原文中的版本已经有一些变化。

比如:

conda安装之后python已经默认使用3.12版本

pytorch已经变成了2.5.1

我使用conda安装pytorch始终安装不上,一怒之下直接官网安装,还好兼容。

一切就绪之后,在ultrylitics-8.3.58中准备跑第一个示例

在tests下面新建目录

新建CatTest1.py

from ultralytics import YOLO
# 加载预训练的 YOLOv11n 模型
model = YOLO('yolo11s.pt')
source = 'D:\\Bob\\yolo\\test\\images\\1.jpg'
# 运行推理,并附加参数
model.predict(source, save=True)

右键,run

运行结果如下:

image 1/1 D:\Bob\yolo\test\images\1.jpg: 640x480 1 cat, 294.4ms
Speed: 6.0ms preprocess, 294.4ms inference, 3.0ms postprocess per image at shape (1, 3, 640, 480)
Results saved to runs\detect\predict6

原图(网图侵删)

结果如下:

 

yes.

 

但是我个人不喜欢仅仅只做物体的框选,我还希望做边缘覆盖或者边缘检测。
一番搜索,最有效的算法是canny算法,就在想这个怎么和yolo集成,又一番搜索,发现还有一个东西叫轮廓提取,额,这个我也想要。

言归正传,结果发现yolo本身就带了边缘覆盖的模型,叫segment,全称:Instance Segmentation。

那么,来测试一下吧:

from ultralytics import YOLO

# Load a model
model = YOLO("yolo11n-seg.pt")  # load an official model
# model = YOLO("path/to/best.pt")  # load a custom model
source = 'D:\\Bob\\yolo\\test\\images\\1.jpg'
model.predict(source, save=True)

还是这只猫,测试效果如下

image 1/1 D:\Bob\yolo\test\images\1.jpg: 640x480 1 cat, 251.0ms
Speed: 6.0ms preprocess, 251.0ms inference, 15.0ms postprocess per image at shape (1, 3, 640, 480)
Results saved to runs\segment\predict3

 

yes

官方提供了五中任务:

Detect(推理)、Segment(分割)、Classify(分类)、OBB(旋转目标检测)、Pose(姿态)

 

 

但是光有区域覆盖还不行啊,我要把检测结果的文本提供给其他业务做分析啊,光有图片是不够的

不要慌,官方也提供了方案

先上代码:

from ultralytics import YOLO

# Load a model
model = YOLO("yolo11n-seg.pt")  # load an official model
# model = YOLO("path/to/best.pt")  # load a custom model
source = 'D:\\Bob\\yolo\\test\\images\\1.jpg'

results = model(source)
print(results)

结果如下:

image 1/1 D:\Bob\yolo\test\images\1.jpg: 640x480 1 cat, 277.3ms
Speed: 8.0ms preprocess, 277.3ms inference, 12.4ms postprocess per image at shape (1, 3, 640, 480)
[ultralytics.engine.results.Results object with attributes:

boxes: ultralytics.engine.results.Boxes object
keypoints: None
masks: ultralytics.engine.results.Masks object
names: {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'}
obb: None
orig_img: array([[[190, 195, 194],
        [188, 193, 192],
        [184, 189, 188],
        ...,
        [185, 185, 185],
        [176, 176, 176],
        [169, 169, 169]],

       [[190, 195, 194],
        [189, 194, 193],
        [189, 194, 193],
        ...,
        [202, 202, 202],
        [193, 193, 193],
        [184, 184, 184]],

       [[196, 201, 200],
        [195, 200, 199],
        [195, 200, 199],
        ...,
        [206, 206, 206],
        [201, 201, 201],
        [195, 195, 195]],

       ...,

       [[228, 231, 229],
        [233, 236, 234],
        [237, 240, 238],
        ...,
        [245, 248, 239],
        [245, 247, 241],
        [246, 249, 240]],

       [[231, 234, 232],
        [233, 236, 234],
        [236, 239, 237],
        ...,
        [247, 251, 240],
        [241, 244, 235],
        [238, 242, 231]],

       [[231, 234, 232],
        [229, 232, 230],
        [227, 230, 228],
        ...,
        [239, 243, 232],
        [236, 240, 229],
        [237, 241, 230]]], dtype=uint8)
orig_shape: (1440, 1078)
path: 'D:\\Bob\\yolo\\test\\images\\1.jpg'
probs: None
save_dir: 'runs\\segment\\predict4'
speed: {'preprocess': 7.999897003173828, 'inference': 277.3125171661377, 'postprocess': 12.35508918762207}]

Process finished with exit code 0

 

见https://blog.csdn.net/weixin_44878336/article/details/138316279,写的非常好。 

posted on 2025-01-08 15:08  红无酒伤  阅读(536)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3