-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsbd.py
54 lines (41 loc) · 1.49 KB
/
sbd.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
from scipy.io import loadmat
import os
import my_accumarray as accum
import numpy as np
sbddir = 'cache/benchmark_RELEASE/dataset'
def load_gt(name):
if not os.path.isdir(sbddir):
raise IOError('Dataset does not exist in {:s}. Please download SBD from http://www.cs.berkeley.edu/~bharath2/codes/SBD/download.html'.format(sbddir))
filename = os.path.join(sbddir, 'cls', name+'.mat')
output = loadmat(filename)
filename = os.path.join(sbddir, 'inst', name+'.mat')
output = loadmat(filename)
inst = output['GTinst'][0,0]['Segmentation']
categories = output['GTinst'][0,0]['Categories']
return inst, categories
def get_bboxes(inst):
x = np.arange(0, inst.shape[1])
y = np.arange(0, inst.shape[0])
xv, yv = np.meshgrid(x, y)
maxinst = np.max(inst)
inst1 = inst.reshape(-1)
xv = xv.reshape(-1)
yv = yv.reshape(-1)
idx = inst1>0
inst1=inst1[idx]
xv=xv[idx]
yv = yv[idx]
instxmin = accum.my_accumarray(inst1-1, xv, maxinst, 'min')
instymin = accum.my_accumarray(inst1-1, yv, maxinst, 'min')
instxmax = accum.my_accumarray(inst1-1, xv, maxinst, 'max')
instymax = accum.my_accumarray(inst1-1, yv, maxinst, 'max')
boxes = np.hstack((instxmin.reshape(-1,1),instymin.reshape(-1,1),instxmax.reshape(-1,1), instymax.reshape(-1,1)))
return boxes
def get_all_sbdboxes(names):
boxes=[]
for i, name in enumerate(names):
inst, categories = load_gt(name)
boxes.append(get_bboxes(inst.astype(int)))
if i%10 == 0:
print str(i)+':'+name
return boxes