筛选数据的工具

 

一、利用 shutil.copy 进行处理

# -*- coding: utf-8 -*-
import os
import glob
import cv2
import json
import shutil

def move_src_accord_filter(filter_path, src_path, dataset_path):
    files = glob.glob(filter_path + '/*.png')
    count = 0
    test_path = "/home/user/lcw/RFBnet_Data/obqFind_wanli/test_dataset"
    for image_path in files:
        image_name = image_path[image_path.rfind("/")+1: ]
        src_image_path = os.path.join(src_path, image_name)
        if not os.path.exists(src_image_path):
            continue

        json_name = image_name[:image_name.rfind(".")] + ".json"
        json_image_path = os.path.join(src_path, json_name)
        print("json_image_path is: ", json_image_path)
        if count < 16:
            shutil.copy(src_image_path, dataset_path)
            shutil.copy(json_image_path, dataset_path)
        else:
            return
            # shutil.copy(src_image_path, test_path)
            # shutil.copy(json_image_path, test_path)
        count = count + 1
        #shutil.move(json_image_path, dataset_path)



if __name__ == "__main__":
    # filter_path = "/media/Data/wangjunjie/active_learning/test_lcw"
    # src_path = "/home/user/lcw/RFBnet_Data/obqFind_wanli/verify_data"
    # dataset_path ="/media/Data/wangjunjie/active_learning/test_lcw_src"
    filter_path = "/media/newData/paperData/opqData/data_28_filter/verify_result"
    src_path = "/media/newData/paperData/opqData/data_28_filter/verify_src"
    dataset_path = "/home/user/lcw/RFBnet_Data/obqFind_wanli/dataset_lcw"
    if not os.path.exists(dataset_path):
        os.mkdir(dataset_path)
    move_src_accord_filter(filter_path, src_path, dataset_path)

二、

import os
import shutil
import sys
def get_MD5(file_path):
    files_md5 = os.popen('md5 %s' %file_path).read().strip()
    file_md5 = files_md5.replace('MD5 (%s) = ' %file_path, ' ')
    return file_md5

def main(path, out):
    for files in os.listdir(path):
        name = os.path.join(path, files)
        back_name = os.path.join(out, files)
        if os.path.isfile(name):
            if os.path.isfile(back_name):
                if get_MD5(name) != get_MD5(back_name):
                    shutil.copy(name, back_name)
            else:
                shutil.copy(name, back_name)
        else:
            if not os.path.isdir(back_name):
                os.makedirs(back_name)
            main(name, back_name)
if __name__ == '__main__':
    A = sys.argv[1]
    B = sys.argv[2]
    print("A is: %s, B is: %s" %(A, B))
    main(A, B)

 

import os
import glob
import cv2
import json
def get_id(img_dir):
    files = glob.glob(img_dir + '/*_1.png')
    #print("files is: ", files)
    id_list = []
    length = len(files)
    for i in range(length):
        file = files[i]
        _, file_name = os.path.split(file)
        id = file_name[: file_name.find("_")]
        #print("id is:", id)
        id_list.append(id)
    return id_list


def save_detect_img(id_list, save_path="/home/user/lcw/project_new/data1/data_failure", base_path = "/home/user/lcw/project_new/data1/data_automodel_error"):
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    for i in range(len(id_list)):
        id = id_list[i]
        img_path = os.path.join(os.path.join(base_path, id), "src")
        img_files = glob.glob(img_path + '/*.png')
        img_files.sort()
        txt_files = glob.glob(img_path + '/*.txt')
        txt_files.sort()
        for j in range(len(txt_files)):
            img_file = img_files[j]
            txt_file = txt_files[j]
            print("img_file is: %s, txt_file is: %s" %(img_file, txt_file))
            _, file_name = os.path.split(img_file)
            name, _ = os.path.splitext(file_name)
            json_name = name + ".json"
            print("json_name is: ", json_name)
            img = cv2.imread(img_file, 0)
            save_path_img = os.path.join(save_path, file_name)
            save_path_json = os.path.join(save_path, json_name)
            cv2.imwrite(save_path_img, img)
            with open(txt_file, "r") as json_f:
                detect_result = json.load(json_f)
            with open(save_path_json, "w") as json_f_write:
                json_f_write.write(json.dumps(detect_result))


def fiter_error_data(id_list, save_path="/home/user/lcw/project_new/data1/data_failure_auto_model", base_path ="/home/user/lcw/project_new/data1/data"):
    for i in range(len(id_list)):
        id = id_list[i]
        save_path_id = os.path.join(save_path, id)
        base_path_id = os.path.join(base_path, id)
        print("save_path_id is: %s, base_path_id is: %s" %(save_path_id, base_path_id))
        cmd = "python cp_custom.py" + " " + base_path_id + " " + save_path_id
        os.system(cmd) 


if __name__ == "__main__":
    id_list = get_id("./failure")
    #save_detect_img(id_list)
    fiter_error_data(id_list)
import os
import glob
import cv2
import json
def get_id(img_dir):
    files = glob.glob(img_dir + '/*_1.png')
    #print("files is: ", files)
    id_list = []
    length = len(files)
    for i in range(length):
        file = files[i]
        _, file_name = os.path.split(file)
        id = file_name[: file_name.find("_")]
        #print("id is:", id)
        id_list.append(id)
    return id_list


def save_detect_img(id_list, save_path="/home/user/lcw/project_new/data1/data_failure", base_path = "/home/user/lcw/project_new/data1/data"):
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    for i in range(len(id_list)):
        id = id_list[i]
        img_path = os.path.join(os.path.join(base_path, id), "src")
        img_files = glob.glob(img_path + '/*.png')
        img_files.sort()
        txt_files = glob.glob(img_path + '/*.txt')
        txt_files.sort()
        for j in range(len(txt_files)):
            img_file = img_files[j]
            txt_file = txt_files[j]
            print("img_file is: %s, txt_file is: %s" %(img_file, txt_file))
            _, file_name = os.path.split(img_file)
            name, _ = os.path.splitext(file_name)
            json_name = name + ".json"
            print("json_name is: ", json_name)
            img = cv2.imread(img_file, 0)
            save_path_img = os.path.join(save_path, file_name)
            save_path_json = os.path.join(save_path, json_name)
            cv2.imwrite(save_path_img, img)
            with open(txt_file, "r") as json_f:
                detect_result = json.load(json_f)
            with open(save_path_json, "w") as json_f_write:
                json_f_write.write(json.dumps(detect_result))


  


if __name__ == "__main__":
    id_list = get_id("./failure")
    save_detect_img(id_list)

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用说明 : 1、本程序可以作什么? 本程序功能分为两个部分: 一部分是:批量查找指定路径下的文件中的内容中,是否包含要查询的项目。并把查询出的内容分文件存储。 一部分是:将文本文件导入EXCEL中,可以将上一步查找的结果导入,也可以自己选择文件导入(支持多选)。 2、如何使用他? a、批量查找: 首先,在“读入位置”按钮处设置你所要读取的文件的存放位置,此时程序会自动读入此文件夹下所有文件,以供选择;其次,用“>>”或“>”将要读取的文件选入读取队列,当然如果选错了可以用“<<”或“<”删除队列,或者鼠标双击选中项删除;再次,点击“+”按钮,添加查找项目到查找项目列表,一次只可以添加一条,如需添加多条则需要重复添加操作;如果添加错误可以双击选中项删除或选中后点击“-”按钮。最后,点击“开始查找”,程序将会把查找结果输出保存到指定路径下面的output文件夹下面,你可以选择是否打开目录查看。如果需要查询的文件有文件头,可以选择“保留文件首行”。 b、EXCEL导入: 首先,选择导入方式,导入方式分为“查询结果导入”和“新选文件导入”两种;当选择“查询结果导入”时,本程序将把“读入位置”处“output”文件夹下文件批量导入EXCEL。当选择“新选文件导入”时,本程序在点击“开始导入”时将弹出窗口,您可以自己选择需要导入文件(支持多选),导入EXCEL。其次,设置导入文件时的分割符,默认为“|”,本程序只支持按照分隔符导入。最后,点击“开始导入”按钮开始导入。 3、本程序不判断所查找的文件类型 由于本程序在读入文件时,并没有校验文件的内容和文件类型,因此本程序会读取用户所选择的任意文件,即使此文件是二进制格式的。不论是查询或者是导入功能都是这样。本程序将按行读取所选择的文件(或者有换行符的),在读取完文件后,无论是否找到,都会创建和源文件相同类型的文件,即使是.exe或.rar(一般是打不开的),文件名存储为“output”+原文件名。即使没有找到任何相匹配的内容,本程序也会创建文件,这时后文件大小是0字节,可以按照大小排列看到。 4、请使用“清除文件”按钮及时清除查询结果 程序在查询和创建文件的过程中,不会判断是否已经执行过查询操作。如果已经执行过查询操作,“output”文件夹下就会存在查询出的文件,当再次执行查询时,本程序会在已存在的文件后追加查询结果。这样就会出现重复的记录或内容。因此,当需要多次查询时,每次查询前需要点击“清空文件”按钮删除output文件夹,才能保证查找的准确。 5、本程序不会判断运行的环境,因此在运行过程中可能会有些未知的错误 本程序在win7环境,vs2012,Netframe4.0下编译通过。本程序支持winxp及以上操作系统。执行EXCEL导入的时候,需要安装Office。Office的版本在2003以上就可以。但是不同我Office版本对导入性能,有一定的影响。Excel2003,最多256列,即2的8次方,最多65536行,即2放入16次方; Excel2007及以上版本,最多16384列,即2的14次方,最多1048576行,即2的20次方。因此如果需要导入的单个文件的行数或者列数,超过了所安装Office版本的最多行列数,程序将会报错!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值