-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannotation.py
160 lines (127 loc) · 3.73 KB
/
annotation.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
#Generate annotation data for yolov3
from scipy import io as spio
from datetime import datetime
import re
import numpy as np
import os
import shutil
import sys
import glob
import cv2
if len(sys.argv)!=3:
print("python annotation.py [fddb/medical-mask-dataset/mixed] [dataset folder path]")
sys.exit(1)
MODE=sys.argv[1]
DATASET_ROOT_PATH=sys.argv[2]
NO_MASK=0
MASK=1
if MODE!="fddb" and MODE!="medical-mask-dataset" and MODE!="mixed":
print("Unknown mode "+MODE)
sys.exit(1)
if(not os.path.exists(DATASET_ROOT_PATH)):
print("folder not found "+DATASET_ROOT_PATH)
sys.exit(1)
annotation_path="./train_"+MODE+".txt"
f_annotation=open(annotation_path,mode="w")
def fddb(f_annotation,root_src_dir):
if(not os.path.exists(root_src_dir)):
print("folder not found "+root_src_dir)
sys.exit(1)
for list in range(1,11):
list2=str(list)
if list<10:
list2="0"+str(list)
path=root_src_dir+"FDDB-folds/FDDB-fold-"+str(list2)+"-ellipseList.txt"
lines=open(path).readlines()
line_no=0
while True:
if line_no>=len(lines):
break
line=lines[line_no]
line_no=line_no+1
file_path=line.replace("\n","")
image_path=root_src_dir+"originalPics/"+file_path+".jpg"
image=cv2.imread(image_path)
imagew=image.shape[1]
imageh=image.shape[0]
f_annotation.write(image_path+" ")
line_n=int(lines[line_no])
line_no=line_no+1
for i in range(line_n):
line=lines[line_no]
line_no=line_no+1
data=line.split(" ")
major_axis_radius=float(data[0])
minor_axis_radius=float(data[1])
angle=float(data[2])
center_x=float(data[3])
center_y=float(data[4])
x=center_x
y=center_y
w=minor_axis_radius*2
h=major_axis_radius*2
xmin=int(x-w/2)
ymin=int(y-h/2)
xmax=int(x+w/2)
ymax=int(y+h/2)
x=1.0*x/imagew
y=1.0*y/imageh
w=1.0*w/imagew
h=1.0*h/imageh
if w>0 and h>0 and x-w/2>=0 and y-h/2>=0 and x+w/2<=1 and y+h/2<=1:
f_annotation.write(""+str(xmin)+","+str(ymin)+","+str(xmax)+","+str(ymax)+","+str(NO_MASK)+" ")
else:
print("Invalid position removed "+str(x)+" "+str(y)+" "+str(w)+" "+str(h))
f_annotation.write("\n")
def medical_mask_dataset(f_annotation,root_src_dir):
if(not os.path.exists(root_src_dir)):
print("folder not found "+root_src_dir)
sys.exit(1)
for src_dir, dirs, files in os.walk(root_src_dir):
for file_ in files:
root, ext = os.path.splitext(file_)
if file_==".DS_Store":
continue
if file_=="Thumbs.db":
continue
if not(ext == ".txt"):
continue
if file_=="train.txt":
continue
path = src_dir + file_
lines=open(path).readlines()
print(path)
jpg_path = file_.replace(".txt",".jpg")
f_annotation.write(root_src_dir+jpg_path+" ")
image=cv2.imread(root_src_dir+jpg_path)
imagew=image.shape[1]
imageh=image.shape[0]
for line in lines:
if line=="\n":
continue
data = line.split(" ")
xmin=float(data[1])-float(data[3])/2
ymin=float(data[2])-float(data[4])/2
xmax=xmin+float(data[3])
ymax=ymin+float(data[4])
xmin=int(xmin*imagew)
ymin=int(ymin*imageh)
xmax=int(xmax*imagew)
ymax=int(ymax*imageh)
category=int(data[0])
if category==0: #mask
symbol=MASK
elif category==1: #half
symbol=NO_MASK
elif category==2: #no mask
symbol=NO_MASK
f_annotation.write(""+str(xmin)+","+str(ymin)+","+str(xmax)+","+str(ymax)+","+str(symbol)+" ")
f_annotation.write("\n")
if MODE=="fddb":
fddb(f_annotation,DATASET_ROOT_PATH+"fddb/")
if MODE=="medical-mask-dataset":
medical_mask_dataset(f_annotation,DATASET_ROOT_PATH+"medical-mask-dataset/")
if MODE=="mixed":
fddb(f_annotation,DATASET_ROOT_PATH+"fddb/")
medical_mask_dataset(f_annotation,DATASET_ROOT_PATH+"medical-mask-dataset/")
f_annotation.close()