Keras-Yolov4代码整理

Page.py规格化图片、xml的序号

import os

import sys


def page(path, type):
    names = os.listdir(path)  # 读取原文件名
    lenth = len(names)  # 获取文件个数
    num = [0] * lenth
    for i in range(int(lenth)):
        num[i] = "%02d" % i  # 三位数字编码  从000开始
        temp = names[i]
        new_name =  num[i] + '.' + type
        os.rename(path + '/' + temp, path + '/' + new_name)


if __name__ == '__main__':
    path = "VOC2007/JPEGImages"
    type = "jpg"
    page(path, type)

bmp2jpg.py

# coding:utf-8
import os
from PIL import Image


# bmp 转换为jpg
def bmpToJpg(file_path):
    for fileName in os.listdir(file_path):
        print(fileName)

        newFileName = fileName.split('.')[0] + ".jpg"
        print(newFileName)

        img = Image.open(file_path + "\\" + fileName)

        img = img.convert('RGB')

        save_path = file_path + "\\output"

        if not os.path.exists(save_path):
            os.makedirs(save_path)
            print("目录创建成功!")

        img.save(save_path + "\\" + newFileName)


# 删除原来的位图
def deleteImages(file_path, imageFormat):
    command = "del " + file_path + "\\*." + imageFormat
    os.system(command)


def main():
    file_path = r"D:\Study\yolov4-spore\VOCdevkit\VOC2007\JPEGImages"
    bmpToJpg(file_path)
    # deleteImages(file_path, "bmp")


if __name__ == '__main__':
    main()

predict_dir.py

from yolo import YOLO
from PIL import Image

import os


def get_file(path, save_path):
    yolo = YOLO()
    dir = os.listdir(path)

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    for file in dir:
        print(file)
        image = Image.open(path + "\\" + file)
        r_image = yolo.detect_image(image)
        r_image.save(save_path + "\\" + str(file))


if __name__ == '__main__':
    path = r"D:\Study\yolov4-spore\VOCdevkit\VOC2007\JPEGImages"
    save_path = path + "\\output"
    get_file(path, save_path)

猜你喜欢

转载自blog.csdn.net/qq_43656233/article/details/108937787