def proc_img(target_size, squarecrop, is_string=False, imgfile=None): imgfile = StringIO(imgfile) if is_string else imgfile im = PILImage.open(imgfile) # This part does the processing scale_factor = target_size / np.float32(min(im.size)) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = PILImage.BICUBIC if scale_factor > 1 else PILImage.ANTIALIAS im = im.resize((wnew, hnew), filt) if squarecrop is True: (cx, cy) = map(lambda x: (x - target_size) // 2, (wnew, hnew)) im = im.crop((cx, cy, cx+target_size, cy+target_size)) buf = StringIO() im.save(buf, format='JPEG', subsampling=0, quality=95) return buf.getvalue()
def proc_img(imgfile, is_string=False): from PIL import Image if is_string: imgfile = StringIO(imgfile) im = Image.open(imgfile) # This part does the processing scale_factor = TARGET_SIZE / np.float32(min(im.size)) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = Image.BICUBIC if scale_factor > 1 else Image.ANTIALIAS im = im.resize((wnew, hnew), filt) if SQUARE_CROP is True: (cx, cy) = map(lambda x: (x - TARGET_SIZE) / 2, (wnew, hnew)) im = im.crop((cx, cy, cx + TARGET_SIZE, cy + TARGET_SIZE)) buf = StringIO() im.save(buf, format='JPEG') return buf.getvalue()
def parse_dev_meta(self, ilsvrc_devkit_tar): tf = self.open_tar(ilsvrc_devkit_tar, 'devkit tar') fmeta = tf.extractfile( tf.getmember('ILSVRC2012_devkit_t12/data/meta.mat')) import scipy.io meta_mat = scipy.io.loadmat(StringIO(fmeta.read())) labels_dic = dict( (m[0][1][0], m[0][0][0][0] - 1) for m in meta_mat['synsets'] if m[0][0][0][0] >= 1 and m[0][0][0][0] <= 1000) label_names_dic = dict( (m[0][1][0], m[0][2][0]) for m in meta_mat['synsets'] if (m[0][0][0][0] >= 1 and m[0][0][0][0] <= 1000)) label_names = [tup[1] for tup in sorted( [(v, label_names_dic[k]) for k, v in labels_dic.items()], key=lambda x:x[0])] fvgtruth = tf.extractfile(tf.getmember( 'ILSVRC2012_devkit_t12/data/' + 'ILSVRC2012_validation_ground_truth.txt')) vgtruth = [[int(line.strip()) - 1] for line in fvgtruth.readlines()] tf.close() return labels_dic, label_names, vgtruth