python根据txt文件中的路径把相应图片移到新文件夹中(YOLO)

python根据txt文件中的路径把相应图片移到新文件夹中(YOLO)_第1张图片

 

# -*- coding: utf-8 -*-
import shutil
import os


def objFileName():
    local_file_name_list = "/export/liuzhe/program/detection/yolov5_distill/VOCdevkit/VOC2007/val.txt"
    obj_name_list = []
    for i in open(local_file_name_list, 'r'):
        obj_name_list.append(i.replace('\n', ''))
    return obj_name_list


def copy_img():
    local_img_name = r'/export/liuzhe/program/detection/yolov5_distill/VOCdevkit/VOC2007/JPEGImages'
    # 指定要复制的图片路径
    path = r'/export/liuzhe/program/detection/yolov5_distill/VOCdevkit/VOC2007/images/val'
    # 指定存放图片的目录
    for i in objFileName():
        new_obj_name = i
        dir, file = os.path.split(new_obj_name)
        shutil.copy(new_obj_name, path + '/' + file)


if __name__ == '__main__':
    copy_img()

你可能感兴趣的:(code,python)