-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop_txt.py
68 lines (56 loc) · 1.43 KB
/
crop_txt.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
import argparse
import os
from tqdm import tqdm
def build_parser():
parser = argparse.ArgumentParser("Add polygons according to sign class")
parser.add_argument(
"--input-folder",
type=str
)
parser.add_argument(
"--output-folder",
type=str
)
parser.add_argument(
"--min-height",
type=str
)
parser.add_argument(
"--min-width",
type=str
)
return parser
def check(line):
line = line.split()
xtl, ytl, xbr, ybr = line[1:]
if int(xbr) - int(xtl) < int(args.min_width):
return 0
elif int(ybr) - int(ytl) < int(args.min_height):
return 0
else:
return 1
def crop_file(file):
lines = file.readlines()
str = ""
for line in lines:
if check(line) == 0:
lines.remove(line)
for line in lines:
str += line
return str
def main(args):
for file in os.listdir(args.output_folder):
file_path = os.path.join(args.output_folder, file)
if os.path.isfile(file_path):
os.unlink(file_path)
for file in tqdm(os.listdir(args.input_folder)):
f_in = open(args.input_folder+"/" + file, 'r')
f_out = open(args.output_folder+"/" + file, 'w')
str = crop_file(f_in)
f_out.write(str)
f_in.close()
f_out.close()
if __name__ == "__main__":
parser = build_parser()
args = parser.parse_args()
main(args)