欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python/yolo 筛选特定标签的图片复制到另一文件夹中

程序员文章站 2024-02-07 12:33:23
提取特定标签的txt# encoding: utf-8import os.pathimport shutilimport re# 遍历指定目录,显示目录下的所有文件名def eachFile(filepath): pathDir = os.listdir(filepath) for allDir in pathDir: child = os.path.join('%s\%s' % (filepath, allDir)) if os.path.i...

提取特定标签的txt

# encoding: utf-8

import os.path
import shutil
import re

# 遍历指定目录,显示目录下的所有文件名
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        child = os.path.join('%s\%s' % (filepath, allDir))
        if os.path.isfile(child):
            readFile(child) 
            continue
        eachFile(child)

# 遍历出结果 返回文件的名字
def readFile(filenames):
    f = open(filenames, 'r')  # r 代表read
    line = f.readline()  # 以行的形式进行读取文件
    list1 = []
    while line:
        a = line.split()
        str = a[0]   # 读取该行的第一个数据
        if str == '0':   #可修改标签
            list1.append(filenames)
            shutil.copy(filenames, '../labels0')
            print("匹配到:" + filenames)
        line = f.readline()
    f.close()

if __name__ == "__main__":
    filenames = '../labels'  #标签根目录
    arr = []
    eachFile(filenames)

含特定标签的图片

# -*- coding: utf-8 -*-

import os
import shutil

root_dir = "../一级目录/"  #根目录
txt_dir = root_dir + "labels/"  #标签
image_dir = root_dir + "images/"  #结果存放地址
origin_image_dir = "../images"  #原始图片地址
count = 0
# train_data_list = os.listdir(images_dir)
for file in os.listdir(txt_dir):
    # count += 1
    # if file not in train_data_list:
    print(count, " ", file)
    shutil.copy(origin_image_dir + '/' + os.path.splitext(file)[0] + '.jpg',
                image_dir + '/' + os.path.splitext(file)[0] + '.jpg')

本文地址:https://blog.csdn.net/mndlgzzd/article/details/107368640