forked from HunterCQu/AITools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darknetTxt_1img1line.py
126 lines (101 loc) · 3.73 KB
/
darknetTxt_1img1line.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
# -*- coding: utf-8 -*-
'''
@Time : 2020/11/09 11:06
@Author : Andy
@File : darknetTxt_1img1line.py
@Noice :
@Modificattion :
@Author :
@Time :
@Detail :
'''
import os
import sys
import argparse
import shutil
from tqdm import tqdm
TRAIN_RATE = 0.8
TXT_PATH = "./new.txt"
ERROR_LOG = "./errorLog.txt"
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('label_dir', help='label directory', type=str)
parser.add_argument('img_dir', help='image directory', type=str)
args = parser.parse_args()
return args
def convert_annotation(label_dir, img_dir):
'''
image_path1 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
image_path2 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
@image_path : Image absolute path
@x1,y1 : Coordinates of upper left corner
@x2,y2 : Coordinates of the lower right corner
@id : Object category
'''
label_dir = os.path.abspath(label_dir)
if label_dir[-1] == "/":
label_dir = label_dir[:-1]
img_dir = os.path.abspath(img_dir)
if img_dir[-1] == "/":
img_dir = img_dir[:-1]
labelList = os.listdir(label_dir)
imgList = os.listdir(img_dir)
imgNum = len(imgList)
# print("images num = ", imgNum)
# trainnum = int(labelList*TRAIN_RATE)
# trainset = random.sample(labelList, trainnum)
# txt_train_path = os.path.join(os.path.dirname(label_dir), "train.txt")
# txt_val_path = os.path.join(os.path.dirname(label_dir), "val.txt")
txt = open(TXT_PATH, 'w')
log_file = open(ERROR_LOG, 'w')
for f in labelList:
print(f + "-->start!")
labelInfor = f.split(".")
labelPath = os.path.join(label_dir, f)
if not os.path.exists(labelPath):
print(labelPath, " is not exists!")
log_file.write(labelPath + "\n")
continue
if labelInfor[-1] !="txt":
print(labelPath, " is not txt file!")
log_file.write(labelPath + "\n")
continue
imgPath = os.path.join(img_dir, f.replace("txt", "jpg"))
if not os.path.exists(imgPath):
imgPath = imgPath.replace("jpg", "png")
if not os.path.exists(imgPath):
print(imgPath, " is not exists!")
log_file.write(labelPath + "\n")
continue
# txt.write(imgPath + " ")
box_info = imgPath + " "
with open(labelPath) as t:
lines = t.readlines()
for line in lines:
box = line.strip().split(" ")
box_info += box[1] + "," + box[2] + "," + box[3] + "," + box[4] + "," + box[0] + " "
# txt.write(box[1] + "," + box[2] + "," + box[3] + "," + box[4] + "," + box[0] + " ")
# txt.write("\n")
txt.write(box_info.strip() + "\n")
log_file.close()
txt.close()
print("\n")
print("-------------------------------------- OUTPUT --------------------------------------")
print("Path of txt file = ", os.path.abspath(TXT_PATH))
print("Path of error log file = ", os.path.abspath(ERROR_LOG))
print("------------------------------------------------------------------------------------")
print("\n")
if __name__ == '__main__':
# args = parse_args()
# label_dir = args.label_dir
# if not os.path.exists(label_dir):
# print("Error !!! %s is not exists, please check the parameter"%label_dir)
# sys.exit(0)
# img_dir = args.img_dir
# if not os.path.exists(img_dir):
# print("Error !!! %s is not exists, please check the parameter"%img_dir)
# sys.exit(0)
label_dir = "./picked_txt"
img_dir = "./picked_img"
convert_annotation(label_dir, img_dir)
print("Done!")