爆了!爆了!YOLOv9和SAM合体!

下载yolov9

数据集准备好后,克隆 YOLOv9 存储库,然后切换到 YOLOv9 目录并安装所需的依赖项,为对象检测和分割任务做好准备。

git clone https://github.com/SkalskiP/yolov9.git

or
直接去githhub下载zip,快捷又方便。

下载权值

https://github.com/WongKinYiu/yolov9/releases/download/v0.1/gelan-c.pt
https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth

下载到yolov9的目录下即可,开个UU加速器的学术资源,就能飞速下载。

在这里插入图片描述

打开,在yolov9的项目下创建两个文件夹:
yolov9SAM.py 红色下划线安装包即可。

import os
import subprocess

import cv2
from segment_anything import sam_model_registry, SamPredictor

from matplotlib import pyplot as plt
import numpy as np
import yaml
import time


def show_mask(mask, ax, color):
    h, w = mask.shape[-2:]
    mask_image = mask.reshape(h, w, 1) * np.array(color).reshape(1, 1, -1)
    ax.imshow(mask_image)


def show_box(box, label, conf_score, color, ax):
    x0, y0 = box[0], box[1]
    w, h = box[2] - box[0], box[3] - box[1]
    rect = plt.Rectangle((x0, y0), w, h, edgecolor=color, facecolor='none', lw=2)
    ax.add_patch(rect)

    label_offset = 10

    # Construct the label with the class name and confidence score
    label_text = f'{label} {conf_score:.2f}'

    ax.text(x0, y0 - label_offset, label_text, color='black', fontsize=10, va='top', ha='left',
            bbox=dict(facecolor=color, alpha=0.7, edgecolor='none', boxstyle='square,pad=0.4'))


weight = 'gelan-c.pt'


def detect(frame, image_name):
    print(frame)
    # 构建调用命令
    image = cv2.imread(frame)
    height, width = image.shape[0], image.shape[1]
    line_thickness = 5
    if width > 640 and height > 640:
        command = f"python detect.py --weight {weight} --source {frame}"
        command = command + f" --save-txt --save-conf --line-thickness {line_thickness} --name {image_name}"
    else:
        command = f"python detect.py --weight {weight} --source {frame} --name {image_name}"

    # 使用subprocess执行命令
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()

    # 输出结果
    print(stdout.decode("UTF-8"), stderr.decode("UTF-8"))


def run(image_path):
    image_name = image_path.split('/')[-1].split('.')[0]
    # Specify the path to your image
    detect(image_path, image_name)
    # Read the image to get its dimensions
    image = cv2.imread(image_path)
    image_height, image_width, _ = image.shape

    detections_path = f'runs/detect/{image_name}/labels/{image_name}.txt'

    bboxes = []
    class_ids = []
    conf_scores = []

    with open(detections_path, 'r') as file:
        for line in file:
            components = line.split()
            class_id = int(components[0])
            confidence = float(components[5])
            cx, cy, w, h = [float(x) for x in components[1:5]]

            # Convert from normalized [0, 1] to image scale
            cx *= image_width
            cy *= image_height
            w *= image_width
            h *= image_height

            # Convert the center x, y, width, and height to xmin, ymin, xmax, ymax
            xmin = cx - w / 2
            ymin = cy - h / 2
            xmax = cx + w / 2
            ymax = cy + h / 2

            class_ids.append(class_id)
            bboxes.append((int(xmin), int(ymin), int(xmax), int(ymax)))
            conf_scores.append(confidence)

    # 为了可视化检测和分割结果,我们必须使用 SAM 将边界框转换为分割掩模。我们随机为类 ID 分配唯一的颜色,然后定义用于显示掩码、
    # 置信度分数和边界框的辅助函数。coco.yaml 文件用于将 class_ids 映射到类名。
    with open('data/coco.yaml', 'r') as file:
        coco_data = yaml.safe_load(file)
        class_names = coco_data['names']
        # Display the results
    for class_id, bbox, conf in zip(class_ids, bboxes, conf_scores):
        class_name = class_names[class_id]
        print(f'Class ID: {class_id}, Class Name: {class_name}, Confidence: {conf:.2f}, BBox coordinates: {bbox}')
    time1 = time.time()
    sam_checkpoint = "sam_vit_h_4b8939.pth"
    model_type = "vit_h"
    sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
    predictor = SamPredictor(sam)

    imageSAM = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    predictor.set_image(image)
    time2 = time.time()
    print(f'SAM time:{time2 - time1} s')

    color_map = {}
    for class_id in class_ids:
        color_map[class_id] = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)

    plt.figure(figsize=(10, 10))
    ax = plt.gca()
    plt.imshow(image)

    # Display and process each bounding box with the corresponding mask
    for class_id, bbox in zip(class_ids, bboxes):
        class_name = class_names[class_id]
        # print(f'Class ID: {class_id}, Class Name: {class_name}, BBox coordinates: {bbox}')

        color = color_map[class_id]
        input_box = np.array(bbox)

        # Generate the mask for the current bounding box
        masks, _, _ = predictor.predict(
            point_coords=None,
            point_labels=None,
            box=input_box,
            multimask_output=False,
        )

        show_mask(masks[0], ax, color=color)
        show_box(bbox, class_name, conf, color, ax)

    # Show the final plot
    plt.axis('off')
    result_save = f'YOLOV9SAM/{image_name}'
    os.makedirs(result_save, exist_ok=True)
    plt.savefig(f"{result_save}/SAM Result.jpg")
    plt.show()
    return f"{result_save}/SAM Result.jpg"


if __name__ == '__main__':
    image_path = '/Users/。。。。'
    run(image_path)

yolov9SAM-UI.py,运行此文件!!!!!

import sys
import threading

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QPushButton, QWidget, QFileDialog
from PyQt5.QtCore import Qt, QThread, QTimer, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap
import yolov9SAM


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.image_path = ''
        self.initUI()

    def initUI(self):
        self.setWindowTitle('YOLOV9-SAM')
        self.setGeometry(100, 100, 800, 800)
        self.layout = QVBoxLayout()

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.central_widget.setLayout(self.layout)

        self.label2 = QLabel(self)
        self.layout.addWidget(self.label2, alignment=Qt.AlignCenter)
        self.label2.setText('SEGMENT RESULT:')
        self.label2.setStyleSheet("QLabel{font:40px}")

        self.label3 = QLabel(self)
        self.layout.addWidget(self.label3, alignment=Qt.AlignCenter)
        self.label3.setStyleSheet("QLabel{font:20px}")

        self.scbutton = QPushButton('上传图片', self)
        self.scbutton.clicked.connect(self.shuangchuan)
        self.layout.addWidget(self.scbutton)
        self.scbutton.setFixedSize(100, 25)

        self.detect = QPushButton('识别图片', self)
        self.layout.addWidget(self.detect)
        self.detect.clicked.connect(self.detect32)
        self.detect.setFixedSize(100, 25)

        self.label = QLabel(self)

        self.layout.addWidget(self.label, alignment=Qt.AlignCenter)
        pixmap = QPixmap.fromImage(QImage('SAM Result.jpg'))
        pixmap = QPixmap.scaled(pixmap, 640, 640)
        self.label.setPixmap(pixmap)

    def shuangchuan(self):
        image_path, _ = QFileDialog.getOpenFileName(self, '选择图片', '/Users/。。。。', '图片 (*.jpg *.png *.jpeg)')
        if image_path:
            self.image_path = image_path

    def detect32(self):
        thread = threading.Thread(target=self.detect1)
        thread.start()

    def detect1(self):
        SAMresult = yolov9SAM.run(self.image_path)
        self.label3.setText(f'Result saved in:{SAMresult}')
        pixmap = QPixmap.fromImage(QImage(SAMresult))
        pixmap = QPixmap.scaled(pixmap, 640, 640)
        self.label.setPixmap(pixmap)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

修改自己的路径即可。如果有报错,可能是qt的原因。
image_path, _ = QFileDialog.getOpenFileName(self, ‘选择图片’, ‘/Users/。。。。’, ‘图片 (*.jpg *.png *.jpeg)’)

尽量不要用相同名字的图片检测,路径可能会出错

M1芯片分割得要40s左右,估计gpu可能只要1s。
效果:在这里插入图片描述

不要用yolov9-c.pt的权值,会报错!! 运行detect.py文件,产生一个报错“AttributeError: ‘list’ object has no attribute ‘device’”,这个报错是因为yolov9-c模型结构会产生两组检测输出,其中一组用于辅助训练,预测时并不使用,所以将报错的位置utils/general.py这里将代码改为“prediction = prediction[0][0]”,即可。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
需要学习Windows系统YOLOv4的同学请前往《Windows版YOLOv4目标检测实战:原理与源码解析》,课程链接 https://edu.csdn.net/course/detail/29865【为什么要学习这门课】 Linux创始人Linus Torvalds有一句名言:Talk is cheap. Show me the code. 冗谈不够,放码过来!  代码阅读是从基础到提高的必由之路。尤其对深度学习,许多框架隐藏了神经网络底层的实现,只能在上层调包使用,对其内部原理很难认识清晰,不利于进一步优化和创新。YOLOv4是最近推出的基于深度学习的端到端实时目标检测方法。YOLOv4的实现darknet是使用C语言开发的轻型开源深度学习框架,依赖少,可移植性好,可以作为很好的代码阅读案例,让我们深入探究其实现原理。【课程内容与收获】 本课程将解析YOLOv4的实现原理和源码,具体内容包括:- YOLOv4目标检测原理- 神经网络及darknet的C语言实现,尤其是反向传播的梯度求解和误差计算- 代码阅读工具及方法- 深度学习计算的利器:BLAS和GEMM- GPU的CUDA编程方法及在darknet的应用- YOLOv4的程序流程- YOLOv4各层及关键技术的源码解析本课程将提供注释后的darknet的源码程序文件。【相关课程】 除本课程《YOLOv4目标检测:原理与源码解析》外,本人推出了有关YOLOv4目标检测的系列课程,包括:《YOLOv4目标检测实战:训练自己的数据集》《YOLOv4-tiny目标检测实战:训练自己的数据集》《YOLOv4目标检测实战:人脸口罩佩戴检测》《YOLOv4目标检测实战:中国交通标志识别》建议先学习一门YOLOv4实战课程,对YOLOv4的使用方法了解以后再学习本课程。【YOLOv4网络模型架构图】 下图由白勇老师绘制  
需要学习ubuntu系统上YOLOv4的同学请前往:《YOLOv4目标检测实战:原理与源码解析》 【为什么要学习这门课】 Linux创始人Linus Torvalds有一句名言:Talk is cheap. Show me the code. 冗谈不够,放码过来! 代码阅读是从基础到提高的必由之路。尤其对深度学习,许多框架隐藏了神经网络底层的实现,只能在上层调包使用,对其内部原理很难认识清晰,不利于进一步优化和创新。YOLOv4是最近推出的基于深度学习的端到端实时目标检测方法。YOLOv4的实现darknet是使用C语言开发的轻型开源深度学习框架,依赖少,可移植性好,可以作为很好的代码阅读案例,让我们深入探究其实现原理。【课程内容与收获】 本课程将解析YOLOv4的实现原理和源码,具体内容包括:- YOLOv4目标检测原理- 神经网络及darknet的C语言实现,尤其是反向传播的梯度求解和误差计算- 代码阅读工具及方法- 深度学习计算的利器:BLAS和GEMM- GPU的CUDA编程方法及在darknet的应用- YOLOv4的程序流程- YOLOv4各层及关键技术的源码解析本课程将提供注释后的darknet的源码程序文件。【相关课程】 除本课程《Windows版YOLOv4目标检测:原理与源码解析》外,本人推出了有关YOLOv4目标检测的系列课程,包括:《Windows版YOLOv4目标检测实战:训练自己的数据集》《Windows版YOLOv4-Tiny目标检测实战:训练自己的数据集》《Windows版YOLOv4目标检测实战:人脸口罩佩戴检测》《Windows版YOLOv4目标检测实战:中国交通标志识别》建议先学习一门YOLOv4实战课程,对YOLOv4的使用方法了解以后再学习本课程。【YOLOv4网络模型架构图】 下图由白勇老师绘制  

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值