Skip to content

Commit 403f71f

Browse files
committed
combine txt and deal with overlap pixels
1 parent 7c00ee9 commit 403f71f

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

RLE_check.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pycocotools import mask as cocomask
2+
import cv2
3+
4+
# the RLE input format for coco api.
5+
a={'size':[375,1242],'counts':"aaU19];3M5J6L2M1O2N1O101O00000O2O0O1O1O010O001O1O100O1O1O100000O0100O1O100O10000000001O0000001N2O001O0O100O2O000000O10O1000O100O1O1O100O1O1O1000000000000000000000000000000O1000000O100O1001O001O001O000010O01O000100O001N2N3M3N2M4J6K4Laag;"}
6+
b=cocomask.decode(a)
7+
cv2.imshow('tt',b*255)
8+
cv2.waitKey(0)

combine_txt_no_overlap.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import os
2+
from tqdm import tqdm
3+
from pycocotools import mask as cocomask
4+
import cv2
5+
import numpy as np
6+
7+
# this file combine_txt.py is to
8+
# combine mots_seg_track txt outputs of each class to one file,
9+
# and make the overlapping pixels only belong to one object.
10+
# Overlap rule: assign pixels to the object with smaller area.
11+
12+
val_in_trainval_seqmap=[2,6,7,8,10,13,14,16,18]
13+
seqmap=val_in_trainval_seqmap
14+
15+
def get_RLE(line_one):
16+
split_all=line_one.split(' ')
17+
RLE={'size':[int(split_all[3]),int(split_all[4])],'counts':split_all[5]}
18+
return RLE
19+
20+
file_dir="Adelaidet_result/training_dir/COCO_pretrain/BoxInst_MS_R_50_1x_kitti_mots/to_mots_txt/mots_seg_track/val_in_trainval/"
21+
for seq_one in tqdm(seqmap):
22+
car_file=file_dir+'car/'+str(seq_one).zfill(4)+'.txt'
23+
pedestrian_file=file_dir+'pedestrian/'+str(seq_one).zfill(4)+'.txt'
24+
file_inputs=[car_file,pedestrian_file]
25+
out_dir=file_dir+'both_car_pedestrian_no_overlap/'
26+
# out_dir=file_dir+'tt/'
27+
out_path=out_dir+str(seq_one).zfill(4)+'.txt'
28+
os.makedirs(out_dir,exist_ok=True)
29+
30+
with open(car_file,'r') as f:
31+
# read to a list and remove ending line character.
32+
car_content=f.read().splitlines()
33+
with open(pedestrian_file,'r') as f:
34+
ped_content=f.read().splitlines()
35+
36+
# need to get the max frame id.
37+
combine_content=car_content+ped_content
38+
frame_list=[int(x.split(' ')[0]) for x in combine_content]
39+
max_frame=max(frame_list)
40+
41+
# below deal with the overlapping in one same frame.
42+
combine_content_sort=sorted(combine_content,key=lambda x: int(x.split(' ')[0]))
43+
new_content=[]
44+
frame_overlap=[]
45+
for frame_id in range(0,max_frame+1):
46+
frame_one_content=list(filter(lambda x: int(x.split(' ')[0])==frame_id,combine_content_sort))
47+
# skip if the frame has no mask output.
48+
if not len(frame_one_content):
49+
continue
50+
# compute area and sort by it.
51+
area_list=[]
52+
for item in frame_one_content:
53+
RLE=get_RLE(item)
54+
area_list.append(cocomask.area(RLE))
55+
objects_sort=[x for _,x in sorted(zip(area_list,frame_one_content))]
56+
57+
objects_disjoint = [objects_sort[0]]
58+
used_pixels = get_RLE(objects_sort[0])
59+
for obj in objects_sort[1:]:
60+
new_mask = get_RLE(obj)
61+
# for cocomask.merge func, the argument intersect=1 gets intersection part, else gets union part,
62+
# this is defined in coco official repo-common/maskApi.c.
63+
if cocomask.area(cocomask.merge([used_pixels, get_RLE(obj)], intersect=True)) > 0.0:
64+
obj_mask_decoded = cocomask.decode(get_RLE(obj))
65+
used_pixels_decoded = cocomask.decode(used_pixels)
66+
# below remove the overlapping pixels.
67+
obj_mask_decoded[np.where(used_pixels_decoded > 0)] = 0
68+
# have to decode bytes to string, since cocomask.encode(obj_mask_decoded) outputs bytes,
69+
# and bytes starts with b', the b' at the begining has to be removed,
70+
# and then its remaining part is the content could be converted to string.
71+
new_mask = cocomask.encode(obj_mask_decoded)
72+
new_mask['counts']=new_mask['counts'].decode('UTF-8')
73+
frame_overlap.append(frame_id)
74+
used_pixels = cocomask.merge([used_pixels, get_RLE(obj)], intersect=False)
75+
# str(new_mask['counts']) is to change bytes type counts,
76+
# generated by new_mask = cocomask.encode(obj_mask_decoded), to string.
77+
obj_refine=' '.join(obj.split(' ')[:3]+[str(new_mask['size'][0]),str(new_mask['size'][1]),new_mask['counts']])
78+
objects_disjoint.append(obj_refine)
79+
new_content.extend(objects_disjoint)
80+
81+
# print(frame_id)
82+
print(f"Seq {str(seq_one).zfill(4)} has overlap frame: ", frame_overlap)
83+
84+
with open(out_path,'w') as outfile:
85+
for line in new_content:
86+
line_split=line.split(' ')
87+
if int(line_split[2])==2:
88+
line_split[1]=str(int(line_split[1])+1000)
89+
line_out=' '.join(line_split)+'\n'
90+
outfile.write(line_out)
91+

0 commit comments

Comments
 (0)