• 【YOLOv8分割】在 预测阶段 如何预测整个目录并输出二值mask图像


    YOLOv8的原来的分割样式如图:
    在原图的基础上进行分割
    这里实现上述预测整个目录的代码:

    import glob
    from PIL import Image
    from ultralytics import YOLO
    import csv
    import os
    from os.path import join , basename
    import torchvision.transforms as transforms
    import matplotlib.pyplot as plt
    import numpy as np
    import torch
    import torch.nn.functional as F
    import cv2
    
    # 模型路径
    model = YOLO(r'F:\Deep_Learning\Model\YOLOv8_Seg\runs\segment\train\weights\best.pt')
    # 图片路径
    source = 'F:/CRACK500/val/images'
    # 预测图片的保存目录
    pred_dir = r'F:\Deep_Learning\Model\YOLOv8_Seg\Pre_Dir'
    
    
    # 如果保存的话:
    results = model(source=source,save=True, name='./Pre_Dir',show_labels=False,show_conf=False,boxes=False)
    
    # 如果不保存的话:
    # results = model(source=source,show_labels=False,show_conf=False,boxes=False)
    
    for result in results:
        image_name = basename(result.path)  # 提取图片名称
        mask_name = f"{os.path.splitext(image_name)[0]}.png"  # 根据图片名称生成保存结果的名称
        pred_image_path = join(r'F:\Deep_Learning\Model\YOLOv8_Seg\Dataset\mask', mask_name)# 图片保存路径
        # 检测到裂缝时:
        if result.masks is not None and len(result.masks) > 0:
            masks_data = result.masks.data
            for index, mask in enumerate(masks_data):
                mask = mask.cpu().numpy() * 255
    
                # cv2.imwrite(f'./output_{index}.png', mask)
    
    
                cv2.imwrite(pred_image_path , mask)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    其代码目录安排如下:
    在这里插入图片描述
    --------------------------------------------------------------------------------------------------------------------------------
    下面是转化后样式:
    二值mask图像
    转换保存的目录:
    在这里插入图片描述

    转换代码:

    import glob
    from PIL import Image
    from ultralytics import YOLO
    import csv
    import os
    from os.path import join , basename
    import torchvision.transforms as transforms
    import matplotlib.pyplot as plt
    import numpy as np
    import torch
    import torch.nn.functional as F
    import cv2
    
    # 模型路径
    model = YOLO(r'F:\Deep_Learning\Model\YOLOv8_Seg\runs\segment\train\weights\best.pt')
    # 图片路径
    source = 'F:/CRACK500/val/images'
    
    # 如果保存的话:
    #results = model(source=source,save=True, name='./Pre_Dir',show_labels=False,show_conf=False,boxes=False)
    
    # 如果不保存的话:
    results = model(source=source,show_labels=False,show_conf=False,boxes=False)
    
    for result in results:
        image_name = basename(result.path)  # 提取图片名称
    
        mask_name = f"{os.path.splitext(image_name)[0]}.png"  # 根据图片名称生成保存结果的名称
    
        pred_image_path = join(r'F:\Deep_Learning\Model\YOLOv8_Seg\Dataset\mask', mask_name)
        # 检测到裂缝时:
        if result.masks is not None and len(result.masks) > 0:
            masks_data = result.masks.data
            for index, mask in enumerate(masks_data):
                mask = mask.cpu().numpy() * 255
                cv2.imwrite(pred_image_path , mask)
        # 检测不到裂缝时:
        else:
            width , height = 640 , 360
            black_image = np.zeros((height , width , 3) , dtype=np.uint8)
            # 保存全黑的图像为PNG文件
            cv2.imwrite(pred_image_path , black_image)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    C++ [](){}匿名函数
    android Compose Text文本
    IDEA控制台取消悬浮&全局配置&SpringBoot配置https
    【开源三方库】Fuse.js:强大、轻巧、零依赖的模糊搜索库
    SettingsProvider
    Discuz!X 3.4任意文件删除漏洞
    Springboot 结合PDF上传到OSS
    Lock和synchronized的区别
    GPT引领前沿与应用突破之GPT4科研实践技术与AI绘图
    编译原理基本概念
  • 原文地址:https://blog.csdn.net/qq_45897436/article/details/132639033