背景
原始内容 :若干图片数据、两个对应的txt文件(rec_gt.txt和Label.txt) 详细内容如下: 图片数据 :  rec_gt.tx : 
Label.txt : 
需求
任务 :
- 从图片文件夹内挑选一部分图片,形成一个新的文件夹
- 根据挑选的文件名在对应的两个txt文件内找到对应的行保留下来
- 将保留下来的内容形成新的txt文件和处理日志
- 可以根据挑选的图片,反向将剩余的图片挑出形成文件夹并生成对应的txt文件和处理日志
要求输出 :
- 包含对应图片名的Label_res.txt
- 包含对应图片名的rec_gt_res.txt
- 日志(包含总图片量、在lable内无对应的图片名列表、在rec内无对应的图片名列表)
- 在有反向需求的情况时:剩余图片(形成文件夹crop_img_require_reverse)、剩余图片对应的Label_res.txt、剩余图片对应的rec_gt_res.txt、日志
注 :图片文件在两个txt内有一定出入,举例:CB.1251.20211102062443_crop_0.jpg 在lable.txt内的图片文件名为CB.1251.20211102062443_crop_0.jpg 在rec_gt.txt内的图片文件名为CB.1251.20211102062443.jpg,没有”_crop_0”部分
实现
import os
import argparse
import shutil
def get_all_file(path):
all_file=[]
for i in os.listdir(path):
file_name=os.path.join(path,i)
all_file.append(file_name)
return all_file
def get_file_cotent(path):
all_cotent=[]
with open(path, encoding='utf-8') as f:
while True:
line = f.readline()
if line:
all_cotent.append(line)
else:
break
return all_cotent
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument("--input_path", type=str, default="20211208")
parser.add_argument("--reverse", action='store_true')
args = parser.parse_args()
input_path=args.input_path
reverse=args.reverse
resoure_file_path=os.path.join(os.path.abspath('.'),input_path)
target_file_path=os.path.join(os.path.abspath('.'),input_path+"_res")
rec_gt=get_file_cotent(os.path.join(resoure_file_path,'rec_gt.txt'))
all_rec_gt_name=[i for i in map(lambda x:x.split()[0].split('/')[1],rec_gt)]
label=get_file_cotent(os.path.join(resoure_file_path,"Label.txt"))
all_label_name=[i for i in map(lambda x:x.split()[0].split('/')[1],label)]
all_img=get_all_file(os.path.join(resoure_file_path,"crop_img"))
all_img=[i for i in map(lambda x:x.split("\\")[-1],all_img)]
all_img_require=get_all_file(os.path.join(target_file_path,"crop_img_require"))
all_img_require=[i for i in map(lambda x:x.split("\\")[-1],all_img_require)]
if reverse:
all_img_require=[i for i in all_img if i not in all_img_require]
if not os.path.exists(os.path.join(target_file_path,'crop_img_require_reverse')):
os.mkdir(os.path.join(target_file_path,'crop_img_require_reverse'))
for i in all_img_require:
src=os.path.join(os.path.join(resoure_file_path,'crop_img'),i)
dst=os.path.join(os.path.join(target_file_path,'crop_img_require_reverse'),i)
shutil.move(src, dst)
error1=0
error2=0
with open(os.path.join(target_file_path,'rec_gt_res.txt'),"w") as f1, open(os.path.join(target_file_path,'Label_res.txt'),"w") as f2,open(os.path.join(target_file_path,'log.txt'),"w") as f3:
for img in all_img_require:
try:
rec_idx = all_rec_gt_name.index(img)
f1.write(rec_gt[rec_idx])
except:
f3.write(img+"没有对应rec\n")
error1+=1
try:
label_idx = all_label_name.index(img.split('_')[0] + ".jpg")
f2.write(label[label_idx])
except:
f3.write(img + "没有对应label\n")
error2 += 1
f3.write("图片总数是:"+str(len(all_img_require))+"\t没问题图片总数是:"+str(len(all_img_require)-error1-error2)+"\t没有对应rec的图片总数是:"+str(error1)+"\t没有对应label的图片总数是:"+str(error2))
结果展示
使用场景 当前目录下有三个文件,切图文件,结果文件和work.py文件 
切图文件包含图片和两个txt文件  结果文件夹包含需要处理的图片文件夹  使用1 :运行python work.py --input_path=20211208之后生成需要处理文件对应的两个txt文件和一个日志文件。 
使用2 :运行python work.py --input_path=20211208 --reverse之后生成需要处理文件反向的一个文件夹,以及其对应的两个txt文件和一个日志文件。 
|