|
a |
|
b/scripts/subsample.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
import os |
|
|
3 |
import sys |
|
|
4 |
import json |
|
|
5 |
import numpy as np |
|
|
6 |
from PIL import Image |
|
|
7 |
from pycocotools.mask import encode, decode |
|
|
8 |
|
|
|
9 |
def get_outfile(infile, outdir): |
|
|
10 |
outfile = os.path.basename(infile) |
|
|
11 |
outsubdir = os.path.basename(os.path.dirname(infile)) |
|
|
12 |
outfile = os.path.join(outdir, outsubdir, outfile) |
|
|
13 |
return outfile |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def filegen(indir, ext='png'): |
|
|
17 |
for dd in os.scandir(indir): |
|
|
18 |
if not os.path.isdir(dd.path): |
|
|
19 |
continue |
|
|
20 |
for ff in os.scandir(dd.path): |
|
|
21 |
if not ff.name.endswith(ext): |
|
|
22 |
continue |
|
|
23 |
yield ff.path |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
def subsample_coco_mask(mask, factor): |
|
|
27 |
mask = Image.fromarray(mask) |
|
|
28 |
sz = np.asarray(mask.size) |
|
|
29 |
out_sz = (sz / factor).astype(int) |
|
|
30 |
mask.thumbnail(size, Image.ANTIALIAS) |
|
|
31 |
mask = np.asarray(mask, order='F') |
|
|
32 |
return mask |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
def subsample_verts(verts, factor): |
|
|
36 |
verts = (np.asarray(verts)//factor).tolist() |
|
|
37 |
|
|
|
38 |
vvprev = [None, None] |
|
|
39 |
|
|
|
40 |
newverts = [] |
|
|
41 |
for vv in verts: |
|
|
42 |
if np.all(vv == vvprev): |
|
|
43 |
continue |
|
|
44 |
else: |
|
|
45 |
newverts.append(vv) |
|
|
46 |
vvprev = vv.copy() |
|
|
47 |
return newverts |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
def subsample_roi(fn, fnout, factor): |
|
|
51 |
with open(fn) as fh: |
|
|
52 |
rois = json.load(fh) |
|
|
53 |
|
|
|
54 |
if len(rois) == 0: |
|
|
55 |
print("NO ROIS IN\t%s" % fn) |
|
|
56 |
|
|
|
57 |
for roi in rois: |
|
|
58 |
mask = decode(roi) |
|
|
59 |
mask = subsample_coco_mask(mask, factor) |
|
|
60 |
cocomask = encode(mask) |
|
|
61 |
cocomask['counts'] = cocomask['counts'].decode() |
|
|
62 |
roi.update(cocomask) |
|
|
63 |
|
|
|
64 |
verts = roi["vertices"] |
|
|
65 |
roi.update({"vertices": subsample_verts(verts, factor)}) |
|
|
66 |
if 'zoom' in roi: |
|
|
67 |
roi.pop('zoom') |
|
|
68 |
|
|
|
69 |
with open(fnout, 'w+') as fh: |
|
|
70 |
json.dump(rois, fh) |
|
|
71 |
|
|
|
72 |
#################################################################### |
|
|
73 |
if __name__ == '__main__': |
|
|
74 |
import sys |
|
|
75 |
import argparse |
|
|
76 |
|
|
|
77 |
parser = argparse.ArgumentParser() |
|
|
78 |
parser.add_argument( |
|
|
79 |
'indir', type=str, help = 'input directory') |
|
|
80 |
|
|
|
81 |
parser.add_argument( |
|
|
82 |
'--outdir', |
|
|
83 |
default='', |
|
|
84 |
type=str, |
|
|
85 |
help = 'output directory') |
|
|
86 |
|
|
|
87 |
parser.add_argument( |
|
|
88 |
'--original-side', |
|
|
89 |
type=int, |
|
|
90 |
default=1024, |
|
|
91 |
help='Original side (in pixels) of the image patches') |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
parser.add_argument('--img', dest='img', action='store_true') |
|
|
95 |
parser.add_argument('--no-img', dest='img', action='store_false') |
|
|
96 |
parser.set_defaults(img=True) |
|
|
97 |
|
|
|
98 |
group = parser.add_mutually_exclusive_group(required=True) |
|
|
99 |
|
|
|
100 |
group.add_argument( |
|
|
101 |
'--target-side', |
|
|
102 |
type=int, |
|
|
103 |
default=512, |
|
|
104 |
help='Requested side (in pixels) of the image patches') |
|
|
105 |
|
|
|
106 |
group.add_argument( |
|
|
107 |
'--factor', |
|
|
108 |
type=int, |
|
|
109 |
default=None, |
|
|
110 |
help='Requested subsampling rate') |
|
|
111 |
|
|
|
112 |
prms = parser.parse_args() |
|
|
113 |
|
|
|
114 |
if prms.factor is None: |
|
|
115 |
factor = prms.original_side // prms.target_side |
|
|
116 |
side = prms.target_side |
|
|
117 |
else: |
|
|
118 |
factor = prms.factor |
|
|
119 |
side = prms.original_side // prms.factor |
|
|
120 |
size = side, side |
|
|
121 |
|
|
|
122 |
if len(prms.outdir)==0: |
|
|
123 |
basedir, set_ = os.path.split(prms.indir.rstrip('/')) |
|
|
124 |
basedir = os.path.dirname(basedir) |
|
|
125 |
#basedir = os.path.dirname(basedir) |
|
|
126 |
outdir = "{}/data_{}_subsample_{:d}x/fullsplit/all".format(basedir, side, factor) |
|
|
127 |
else: |
|
|
128 |
outdir = prms.outdir |
|
|
129 |
|
|
|
130 |
print("SAVING TO:", outdir, sep='\t') |
|
|
131 |
os.makedirs(outdir, exist_ok = True) |
|
|
132 |
|
|
|
133 |
if prms.img: |
|
|
134 |
print("SUBSAMPLING IMAGES") |
|
|
135 |
for infile in filegen(prms.indir): |
|
|
136 |
outfile = get_outfile(infile, outdir=outdir) |
|
|
137 |
os.makedirs(os.path.dirname(outfile), exist_ok=True) |
|
|
138 |
if infile != outfile: |
|
|
139 |
try: |
|
|
140 |
im = Image.open(infile) |
|
|
141 |
im.thumbnail(size, Image.ANTIALIAS) |
|
|
142 |
im.save(outfile, "png") |
|
|
143 |
except IOError as ee: |
|
|
144 |
print( "cannot create thumbnail for '%s'" % infile) |
|
|
145 |
print(ee) |
|
|
146 |
|
|
|
147 |
print("SUBSAMPLING MASKS") |
|
|
148 |
for infile in filegen(prms.indir, ext = '.json'): |
|
|
149 |
outfile = get_outfile(infile, outdir=outdir) |
|
|
150 |
if infile != outfile: |
|
|
151 |
try: |
|
|
152 |
subsample_roi(infile, outfile, factor) |
|
|
153 |
except IOError as ee: |
|
|
154 |
print( "cannot create thumbnail for '%s'" % infile) |
|
|
155 |
print(ee) |
|
|
156 |
except Exception as ee: |
|
|
157 |
print( "cannot create thumbnail for '%s'" % infile) |
|
|
158 |
raise ee |