def proc_img(target_size, squarecrop, is_string=False, compute_global=False, imgfile=None, ):
    imgfile = StringIO(imgfile) if is_string else imgfile
    im = PILImage.open(imgfile)

    scale_factor = target_size / np.float32(min(im.size))
    if scale_factor == 1 and im.size[0] == im.size[1] and is_string is False:
        return np.fromfile(imgfile, dtype=np.uint8)

    (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)

    if compute_global:
        nim = np.array(im).mean(axis=0).mean(axis=0)
        with r.get_lock():
            r.value += nim[0]
        with g.get_lock():
            g.value += nim[1]
        with b.get_lock():
            b.value += nim[2]

    return buf.getvalue()
示例#2
0
def proc_img(target_size, imgfile=None):
    im = Image.open(imgfile)
    scale_factor = target_size / np.float32(min(im.size))
    filt = Image.BICUBIC if scale_factor > 1 else Image.ANTIALIAS
    im = im.resize((target_size, target_size), filt)
    buf = StringIO()
    im.save(buf, format='JPEG', quality=95)
    return buf.getvalue()
示例#3
0
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()
示例#4
0
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()
示例#5
0
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', subsampling=0, quality=95)
    return buf.getvalue()
示例#6
0
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()
示例#7
0
def proc_img(target_size, squarecrop, is_string=False, imgfile=None):
    imgfile = StringIO(imgfile) if is_string else imgfile
    im = PILImage.open(imgfile)

    scale_factor = target_size / np.float32(min(im.size))
    if scale_factor == 1 and im.size[0] == im.size[1] and is_string is False:
        return np.fromfile(imgfile, dtype=np.uint8)

    (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()
示例#8
0
    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