forked from aimuch/AITools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocesspublicdata.py
195 lines (174 loc) · 6.99 KB
/
processpublicdata.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
# Author : Andy Liu
# Data: 2018-11-06
# This tool is used to process public data
# input: python processpublicdata.py /home/andy/data/txt /home/andy/data/img /drawout /ROIs
# output:
# ./drawout
# ./ROIs
import os
import sys
import random
import shutil
import argparse
import cv2
from tqdm import tqdm
classes = {}
colors = [(0,10,255),(0,255,0),(255,0,0),(0,255,255)]
img_error = []
def parse_args():
'''Parsing command line arguments'''
parse = argparse.ArgumentParser()
parse.add_argument('txt_dir', help='Txt directory', type=str)
parse.add_argument('img_dir', help='Image directory', type=str)
parse.add_argument('drawout', help='Draw directory', type=str)
parse.add_argument('ROIs' ,help='ROI directory', type=str)
args = parse.parse_args()
return args
def draw_img(img, box, clr, message):
'''Draw bounding box on image'''
x1, y1, x2, y2 = map(int, box) # map appley the first parameter function to the second parameter
cv2.rectangle(img, (x1,y1), (x2,y2), clr, 1)
cv2.putText(img, message, (x1-10,y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, clr, 1)
return img
def parse(txt_path):
f = open(txt_path, "r")
lines = f.read().split('\n') # ['0 0.6 0.7 0.04 0.06', '3 0.5 0.6 0.01 0.01', '']
lines = lines[:-1] # ['0 0.6 0.7 0.04 0.06', '3 0.5 0.6 0.01 0.01']
f.close()
return lines
# lines = parse(txt_dir)
# for line in lines:
# line = line.split(';') # split by ';'
# img_name = line[0] # with extend
# box = (line[1], line[2], line[3], line[4])
# label = line[-1]
# if label not in classes:
# classes[label] = 1
# else:
# classes[label] += 1
# imgname = img_name.split('.')[0]
# img = cv.imread(img_dir + '/' + img_name)
# roi = img[box[0]:box[2], box[1]:box[3]]
# cv2.imwrite(ROIs + '/' + imgname + '_' + random.randint(0,99)//random.randint(1,9) + '.png', roi)
# img_draw = draw_img(img, box, colors[0], label)
# cv2.imwrite(drawout + '/' + img_name, img_draw)
# return
def process(txt_dir, img_dir, drawout, ROIs):
'''The main process images function'''
if txt_dir[-1] == "/":
txt_dir = txt_dir[:-1]
if img_dir[-1] == "/":
img_dir = img_dir[:-1]
if drawout[-1] == "/":
drawout = drawout[:-1]
if ROIs[-1] == "/":
ROIs = ROIs[:-1]
if os.path.isfile(txt_dir):
lines = parse(txt_dir)
for line in tqdm(lines):
line = line.split(';') # split by ';'
if line[-1] == '':
line.pop()
elif line[-1] == '\r':
line.pop()
if len(line) == 6:
img_name = line[0] # with extend
box = (int(float(line[1])), int(float(line[2])), int(float(line[3])), int(float(line[4]))) # col_top, row_top, col_bottom, row_bottom
# print(box)
label = line[5]
elif len(line) == 8:
img_name = line[0] # with extend
box = (int(float(line[3])), int(float(line[4])), int(float(line[5])), int(float(line[6]))) # col_top, row_top, col_bottom, row_bottom
# print(box)
label = line[7]
else:
continue
if label not in classes:
classes[label] = 1
else:
classes[label] += 1
imgname = img_name.split('.')[0]
# print(img_name)
imgpath = img_dir + '/' + img_name
if not os.path.exists(imgpath):
continue
img = cv2.imread(imgpath)
if img is None:
# print("%s can't read!"%(img_dir + '/' + img_name))
img_error.append(imgpath)
continue
roi = img[box[1]:box[3], box[0]:box[2]]
cv2.imwrite(ROIs + '/' + imgname + '_' + str(random.randint(0,99)//random.randint(1,9)) + '.png', roi)
img_draw = draw_img(img, box, colors[0], label)
cv2.imwrite(drawout + '/' + img_name, img_draw)
return
elif os.path.isdir(txt_dir):
txt_list = os.listdir(txt_dir)
for txt in txt_list:
lines = parse(txt_dir)
for line in tqd,(lines):
line = line.split(';') # split by ';'
if line[-1] == '':
line.pop()
elif line[-1] == '\r':
line.pop()
if len(line) == 6:
img_name = line[0] # with extend
box = (int(float(line[1])), int(float(line[2])), int(float(line[3])), int(float(line[4]))) # col_top, row_top, col_bottom, row_bottom
# print(box)
label = line[5]
elif len(line) == 8:
img_name = line[0] # with extend
box = (int(float(line[3])), int(float(line[4])), int(float(line[5])), int(float(line[6]))) # col_top, row_top, col_bottom, row_bottom
# print(box)
label = line[7]
else:
continue
if label not in classes:
classes[label] = 1
else:
classes[label] += 1
imgname = img_name.split('.')[0]
imgpath = img_dir + '/' + img_name
if not os.path.exists(imgpath):
continue
img = cv.imread(imgpath)
if img is None:
# print("%s can't read!"%(img_dir + '/' + img_name))
img_error.append(imgpath)
continue
roi = img[box[1]:box[3], box[0]:box[2]]
cv2.imwrite(ROIs + '/' + imgname + '_' + random.randint(0,99)//random.randint(1,9) + '.png', roi)
img_draw = draw_img(img, box, colors[0], label)
cv2.imwrite(drawout + '/' + img_name, img_draw)
return
if __name__ == '__main__':
args = parse_args()
txt_dir = args.txt_dir
img_dir = args.img_dir
drawout = args.drawout
ROIs = args.ROIs
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)
if not os.path.exists(drawout):
os.makedirs(drawout)
print("Output folder = ", os.path.abspath(drawout))
else:
shutil.rmtree(drawout)
os.makedirs(drawout)
print("Output folder = ", os.path.abspath(drawout))
if not os.path.exists(ROIs):
os.makedirs(ROIs)
print("Output folder = ", os.path.abspath(ROIs))
else:
shutil.rmtree(ROIs)
os.makedirs(ROIs)
print("Output folder = ", os.path.abspath(ROIs))
process(txt_dir, img_dir, drawout, ROIs)
print("The damaged images : %d"%len(img_error))
print(img_error)