将图片路径与标签路径写入到txt文档中

 

from pathlib import Path


if __name__ == '__main__':

    img_files = [
                 Path(r"Clothes"),
                 Path(r"train_bn")]

    txt_files = [
                 Path(r"Clothes"),
                 Path(r"train_bn")]
    result_png_list = r"val.txt"

    write_png = open(result_png_list, "w", encoding="utf-8")
    for img_file, txt_file in zip(img_files, txt_files):
        for img_pth, txt_pth in list(zip(img_file.glob("*.jpg"), txt_file.glob("*.txt"))):
            txt_pth = str(txt_file / img_pth.stem) + ".txt"
            if img_pth.is_file() and Path(txt_pth).is_file():
                print(f"{str(img_pth)}")
                write_png.write(str(img_pth) + "\n")
            else:
                print(f"文件:{str(img_pth.stem)} 有一个不存在!")

    write_png.close()

你可能感兴趣的:(深度学习,其他,深度学习,人工智能)