forked from aimuch/AITools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pick_txt_img_by_label.py
89 lines (72 loc) · 2.55 KB
/
pick_txt_img_by_label.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
# Author : Andy Liu
# Last modified: 2018-8-14
# This tool is used to pick .txt and image by label
# input : python pick_txt_img_by_label.py "/home/andy/data/label_dir/" "/home/andy/data/img_dir/"
# output :
# ./pickedLabel
# ./pickedImg
from xml.dom.minidom import Document
import os
import os.path
import shutil
import sys
import argparse
from PIL import Image
labelset = ["6"]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('txt_dir', help='Annotations directory', type=str)
parser.add_argument('img_dir', help='Images directory', type=str)
args = parser.parse_args()
return args
def pick(txt_dir, img_dir):
dst_label = "./pickedLabel"
dst_img = "./pickedImg"
txt_dir = os.path.abspath(txt_dir)
img_dir = os.path.abspath(img_dir)
if txt_dir[-1] == "/":
txt_dir = txt_dir[:-1]
if img_dir[-1] == "/":
img_dir = img_dir[:-1]
if not os.path.exists(dst_label):
os.makedirs(dst_label)
if not os.path.exists(dst_img):
os.makedirs(dst_img)
filelist = os.listdir(txt_dir)
for file in filelist:
print(file + "-->start!")
fileInfor = file.split(".")
txtpath = txt_dir + "/" + file
txtfile = open(txtpath, "r")
lines = txtfile.read().split('\n')
for line in lines:
if line is '' or line is None:
continue
obj = line.split(" ")[0]
if not obj in labelset:
continue
dst_label_file = dst_label + "/" + file
srcimg = img_dir + "/" + fileInfor[0] + ".jpg"
dst_img_file = dst_img + "/" + fileInfor[0] + ".jpg"
if not os.path.exists(srcimg):
srcimg = img_dir + "/" + fileInfor[0] + ".png"
dst_img_file = dst_img + "/" + fileInfor[0] + ".png"
if not os.path.exists(srcimg):
break
shutil.copyfile(txtpath, dst_label_file)
shutil.copyfile(srcimg, dst_img_file)
print("Path of picked labels = ",os.path.abspath(dst_label))
print("Path of picked images = ",os.path.abspath(dst_img))
if __name__ == '__main__':
args = parse_args()
txt_dir = args.txt_dir
img_dir = args.img_dir
if not os.path.exists(txt_dir):
print("Error !!! %s is not exists, please check the parameter"%txt_dir)
sys.exit(0)
if not os.path.exists(img_dir):
print("Error !!! %s is not exists, please check the parameter"%img_dir)
sys.exit(0)
pick(txt_dir,img_dir)
print("Done!")