WinClip非官方复现代码学习笔记9

本文详细介绍了eval_utils.py文件中的函数,涉及图像分辨率调整、得分归一化以及分类和分割结果的保存,展示了如何使用OpenCV进行图像处理和文件操作。
摘要由CSDN通过智能技术生成

一、代码结构展示

昨天的笔记中讲解了csv_utils.py文件的内容,今天的笔记主要讲解接下来的eval_utils.py文件。

二、代码功能介绍

这个文件的代码包含了几个函数,用于图像处理和结果保存,其中包括调整分辨率、归一化得分以及保存分类和分割结果。

  1. specify_resolution(image_list, score_list, mask_list, resolution=(400,400))

    • 这个函数用于调整输入的图像、得分和掩码的分辨率。它接受四个参数:图像列表、得分列表、掩码列表和分辨率(可选参数,默认为(400, 400))。
    • 在函数内部,使用 zip() 函数将图像、得分和掩码逐个取出,并使用 OpenCV 的 cv2.resize() 函数将它们调整到指定的分辨率。插值方法为 cv2.INTER_CUBIC(用于图像和得分)和 cv2.INTER_NEAREST(用于掩码)。
    • 调整后的图像、得分和掩码分别存储在 resize_imageresize_scoreresize_mask 列表中,并最终返回这三个列表。
  2. normalize(scores)

    • 这个函数用于对输入的得分进行归一化处理。它接受一个得分列表作为参数。
    • 在函数内部,首先计算得分列表中的最大值和最小值,然后使用这两个值将得分列表进行归一化处理,计算公式为 (scores - min_value) / (max_value - min_value)
    • 归一化后的得分列表被返回。
  3. save_single_result(classification_score, segmentation_score, root_dir, shot_name, experiment_indx, subset_name, defect_type, name, use_defect_type)

    • 这个函数用于保存单个结果,包括分类得分和分割得分。
    • 它接受了分类得分、分割得分、保存根目录、抽样名称、实验索引、子集名称、缺陷类型、结果名称和是否使用缺陷类型作为参数。
    • 在函数内部,根据是否使用缺陷类型,构建保存结果的文件路径,并创建相应的目录。然后,将分类得分保存为 .txt 文件,将分割得分保存为 .npz 文件。
  4. save_results(classification_score_list, segmentation_score_list, root_dir, shot_name, experiment_indx, name_list, use_defect_type)

    • 这个函数用于保存多个结果,循环遍历分类得分、分割得分和结果名称列表,并调用 save_single_result 函数保存每个结果。

三、代码逐行注释

# 这段代码包含了几个函数,用于图像处理和结果保存。
# 这些函数主要用于图像处理和结果保存,其中包括调整分辨率、归一化得分以及保存分类和分割结果
import cv2
import os
import numpy as np

def specify_resolution(image_list, score_list, mask_list, resolution: tuple=(400,400)):# 这个函数接受三个参数:图像列表、得分列表、掩码列表、分辨率(作为可选参数,默认值为(400,400))
    resize_image = []
    resize_score = []
    resize_mask = []
    # print(resolution)
    for image, score, mask in zip(image_list, score_list, mask_list):  # 使用 zip() 函数将图像、得分和掩码逐个取出
        image = cv2.resize(image, (resolution[0], resolution[1]), interpolation=cv2.INTER_CUBIC)  # 使用 OpenCV 的 cv2.resize() 函数将它们调整到指定的分辨率
        score = cv2.resize(score, (resolution[0], resolution[1]), interpolation=cv2.INTER_CUBIC)  # 插值方法为 cv2.INTER_CUBIC(用于图像和得分)
        mask = cv2.resize(mask, (resolution[0], resolution[1]), interpolation=cv2.INTER_NEAREST)  # cv2.INTER_NEAREST(用于掩码)
        resize_image += [image]  # 调整后的图像、得分和掩码分别存储在 resize_image、resize_score 和 resize_mask 列表中,并最终返回这三个列表
        resize_score += [score]
        resize_mask += [mask]

    return resize_image, resize_score, resize_mask

def normalize(scores):  # 这个函数用于对输入的得分进行归一化处理。它接受一个得分列表作为参数

    max_value = np.max(scores)  # 首先计算得分列表中的最大值和最小值
    min_value = np.min(scores)

    norml_scores = (scores - min_value) / (max_value - min_value)  # 然后使用这两个值将得分列表进行归一化处理
    return norml_scores  # 归一化后的得分列表被返回

def save_single_result(classification_score, segmentation_score, root_dir, shot_name, experiment_indx, subset_name, defect_type, name, use_defect_type):
    # 这个函数用于保存单个结果,包括分类得分和分割得分。它接受了分类得分、分割得分、保存根目录、抽样名称、实验索引、子集名称、缺陷类型、结果名称和是否使用缺陷类型作为参数。
    if use_defect_type:  # 根据是否使用缺陷类型,构建保存结果的文件路径,并创建相应的目录。
        # mvtec2d mvtec3d
        save_dir = os.path.join(root_dir, shot_name, experiment_indx, subset_name, defect_type)
    else:
        # visa
        save_dir = os.path.join(root_dir, shot_name, experiment_indx, subset_name)

    os.makedirs(save_dir, exist_ok=True)

    classification_dir = os.path.join(save_dir, 'classification')
    segmentation_dir = os.path.join(save_dir, 'segmentation')
    os.makedirs(classification_dir, exist_ok=True)  # 构建保存结果的文件路径
    os.makedirs(segmentation_dir, exist_ok=True)
    # 将分类得分保存为 .txt 文件,将分割得分保存为 .npz 文件。
    classification_path = os.path.join(classification_dir, f'{name}.txt')
    segmentation_path = os.path.join(segmentation_dir, f'{name}.npz')

    with open(classification_path, "w") as f:
        f.write(f'{classification_score:.5f}')

    segmentation_score = np.round(segmentation_score * 255).astype(np.uint8)
    np.savez_compressed(segmentation_path, img=segmentation_score)

def save_results(classification_score_list, segmentation_score_list, root_dir, shot_name, experiment_indx, name_list, use_defect_type):
    # 这个函数用于保存多个结果,循环遍历分类得分、分割得分和结果名称列表,并调用 save_single_result 函数保存每个结果
    for classification_score, segmentation_score, full_name in zip(classification_score_list,
                                                                           segmentation_score_list,
                                                                           name_list):
        subset_name, defect_type, name = full_name.split('-')
        save_single_result(classification_score, segmentation_score, root_dir, shot_name, experiment_indx, subset_name, defect_type, name, use_defect_type)

  • 22
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值