-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_ours.py
executable file
·46 lines (42 loc) · 1.39 KB
/
filter_ours.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
import os
import cv2
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def label_process(image_path,label_path,save_path):
labeltxt_list=os.listdir(label_path)
for label_txt in labeltxt_list:
if label_txt=="":
continue
image_name = os.path.splitext(label_txt)[0]
img=cv2.imread(os.path.join(image_path,image_name))
size=[img.shape[1],img.shape[0]]
label_txt_path=os.path.join(label_path,label_txt)
fo=open(label_txt_path,'r')
content_list=fo.read().split("\r\n")
label_num=int(content_list[0])
new_label_file=os.path.splitext(image_name)[0]+".txt"
fw=open(os.path.join(save_path,new_label_file),'a')
for i in xrange(label_num):
bbox_str=content_list[i*3+1].split()
label_str=content_list[i*3+2]
ymin=float(bbox_str[0])
xmin=float(bbox_str[1])
ymax=float(bbox_str[2])
xmax=float(bbox_str[3])
bbox_norm=convert(size,[xmin,xmax,ymin,ymax])
eachline=label_str+" "+str(bbox_norm[0])+" "+str(bbox_norm[1])+" "+str(bbox_norm[2])+" "+str(bbox_norm[3])+'\n'
fw.write(eachline)
if __name__=="__main__":
label_process("/opt/jl/datasets/new_attribute/train/images",
"/opt/jl/datasets/new_attribute/train/labels",
"/opt/jl/datasets/new_attribute/train/change_labels")