Summary - YOLOV5 trains its own data set, some data sets are png solutions

When I was working on the project this time, there were some png images in the data set, and the following information appeared during training

Although this does not affect the final result, it is uncomfortable to watch

 The image channel in png format cannot be directly changed to jpg format, the channel needs to be changed

from PIL import Image
import os

# 设置需要转换的图片目录
img_dir = r""

# 遍历目录下所有文件
for filename in os.listdir(img_dir):
    # 判断文件是否为png格式
    if filename.endswith(".png"):
        # 构造新的文件名,将后缀改为jpg
        new_filename = os.path.splitext(filename)[0] + ".jpg"
        # 打开图片
        img = Image.open(os.path.join(img_dir, filename))
        # 如果图片是RGBA模式,转换为RGB模式
        if img.mode == "RGBA":
            img = img.convert("RGB")
        # 如果图片是P模式,转换为RGB模式
        if img.mode == "P":
            img = img.convert("RGB")
        # 保存为新的jpg格式图片
        img.save(os.path.join(img_dir, new_filename))
        # 关闭图片
        img.close()
        # 删除原来的png格式图片
        os.remove(os.path.join(img_dir, filename))

Change only the suffix, not the prefix

Guess you like

Origin blog.csdn.net/weixin_47037450/article/details/131852305