Skip to content

Caffe:Python

byte buffer to Numpy image

import numpy as np
import skimage.io

def load_image_from_buffer(buffer, color=True):
    img_buffer = np.frombuffer(buffer, dtype=np.uint8)
    img_buffer.shape = (300, 300, 3)
    img = skimage.img_as_float(img_buffer).astype(np.float32)
    if img.ndim == 2:
        img = img[:, :, np.newaxis]
        if color:
            img = np.tile(img, (1, 1, 3))
    elif img.shape[2] == 4:
        img = img[:, :, :3]
    return img

def load_image(filename, color=True):
    img = skimage.img_as_float(skimage.io.imread(filename, as_grey=not color)).astype(np.float32)
    if img.ndim == 2:
        img = img[:, :, np.newaxis]
        if color:
            img = np.tile(img, (1, 1, 3))
    elif img.shape[2] == 4:
        img = img[:, :, :3]
    return img

See also

Favorite site