-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenLabel.py
185 lines (148 loc) · 5.36 KB
/
genLabel.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
#python3 steven
#10/04/2020 test dataset
import sys,os
import cv2
import numpy as np
def deleteFile(file_path):
if os.path.exists(file_path):
os.remove(file_path)
def pathsFiles(dir,filter=''): #"cpp h txt jpg"
def getExtFile(file):
return file[file.rfind('.')+1:]
def getFmtFile(path):
#/home/User/Desktop/file.txt /home/User/Desktop/file .txt
root_ext = os.path.splitext(path)
return root_ext[1]
fmts = filter.split()
if fmts:
for dirpath, dirnames, filenames in os.walk(dir):
for filename in filenames:
if getExtFile(getFmtFile(filename)) in fmts:
yield dirpath+'\\'+filename
else:
for dirpath, dirnames, filenames in os.walk(dir):
for filename in filenames:
yield dirpath+'\\'+filename
def createPath(dirs):
def deleteFolder(file_path):
if os.path.exists(file_path):
#shutil.rmtree(file_path)
for lists in os.listdir(file_path):
f = os.path.join(file_path, lists)
if os.path.isfile(f):
os.remove(f)
deleteFolder(dirs)
if not os.path.exists(dirs):
os.makedirs(dirs)
def getFileName(path): #full fileName
return os.path.basename(path)
def getLabelFileLabels(fileLabel):
numbers = {'0','1','2','3','4','5','6','7','8','9'}
labels = []
with open(fileLabel,'r') as srcF:
for i in srcF.readlines():
if i[0] in numbers:
#print(i)
pts = i.split(' ')
#print(pts)
labels.append([float(pts[0]),float(pts[1])])
return labels
def writeToDst(file,content):
with open(file,'a',newline='\n') as dstF: #linux--'\n'
dstF.write(content)
def writeAnotationFile(file,pts):
deleteFile(file)
for i in pts:
a,b = i[0],i[1]
#print(i,a,b, str(a)+ ' ' + str(b))
writeToDst(file,str(a) + ' ' + str(b) + '\n')
def sourceDatasetOper():
base = r'.\db\FGNET\\'
imgPath = base + 'images\\'
LabelPath = base + r'points\\'
dstRecImgPath = base + r'test'
dstPath = r'.\db\FGNET\labels\\'
fList = base + 'trainList.list'
deleteFile(fList)
createPath(dstPath)
for i in pathsFiles(imgPath,'JPG'):
print(i)
img = loadImg(i)
fileName = getFileName(i)
fileName = fileName[:fileName.rfind('.')]
#writeToDst(fList,i+'\n')
writeToDst(fList,'.\images\\' + fileName + '.jpg' + '\n')
label = LabelPath + '\\' + fileName + '.pts'
pts = getLabelFileLabels(label)
dstFile = dstPath + fileName + '.pts'
print('dstFile=', dstFile)
writeAnotationFile(dstFile,pts)
def loadImg(file,mode=cv2.IMREAD_COLOR):
#mode = cv2.IMREAD_COLOR cv2.IMREAD_GRAYSCALE cv2.IMREAD_UNCHANGED
return cv2.imread(file,mode)
def getImgHW(img):
return img.shape[0],img.shape[1]
def writeImg(img,filePath):
cv2.imwrite(filePath,img)
def resizeImg(img,NewW,NewH,pts):
h,w = getImgHW(img)
#return cv2.resize(img, (int(h*ratio), int(w*ratio)), interpolation=cv2.INTER_CUBIC) #INTER_LANCZOS4
rimg = cv2.resize(img, (NewW,NewH), interpolation=cv2.INTER_CUBIC) #INTER_CUBIC INTER_NEAREST INTER_LINEAR INTER_AREA
newPts=[]
for i in pts:
x,y = i[0],i[1]
#newPts.append((round(x*NewW/w,4),round(y*NewH/h,4)))
if 0:
newPts.append((x*NewW/w, y*NewH/h)) #use location coordinates
else:
newPts.append((x/w, y/h)) #use location/size ratio
return rimg,newPts
newW=364
newH=440
def newImageScale():
base = r'.\db\FGNET\\'
base = os.path.abspath(base)
imgPath = base + r'\images'
LabelPath = base + r'\points'
newBase = r'.\db\train\\'
newBase = os.path.abspath(newBase)
newimgPath = newBase + r'\images'
newLabelPath = newBase + r'\labels'
newfList = newBase + r'\trainList.list'
deleteFile(newfList)
createPath(newimgPath)
createPath(newLabelPath)
print('base=',base)
print('newBase=',newBase)
print('imgPath=',imgPath)
print('LabelPath=',LabelPath)
print('newimgPath=',newimgPath)
print('newLabelPath=',newLabelPath)
print('newfList=',newfList)
print(imgPath)
for i in pathsFiles(imgPath,'JPG'):
print(i)
img = loadImg(i)
H,W = getImgHW(img)
fileName = getFileName(i)
fileName = fileName[:fileName.rfind('.')]
label = LabelPath + '\\' + fileName + '.pts'
print('label=', label)
pts = getLabelFileLabels(label)
#print('pts=',len(pts),pts)
newImg,newPts = resizeImg(img,newW,newH,pts)
#print(fileName,H,W)
a = newimgPath + '\\' + fileName + '.jpg'
b = newLabelPath + '\\' + fileName + '.pts'
writeToDst(newfList, a + ',' + b + '\n')
dstFile = newimgPath + '\\' + fileName + '.jpg'
print('dstFile=', dstFile)
writeImg(newImg,dstFile)
dstLabelFile = newLabelPath + '\\' + fileName + '.pts'
print('dstLabelFile=', dstLabelFile)
writeAnotationFile(dstLabelFile,newPts)
def main():
#sourceDatasetOper()
newImageScale()
if __name__ == '__main__':
main()