Exemple #1
0
def processImage(imagePath, bbox):
    image = Image.open(imagePath)
    image.thumbnail((width, height), Image.ANTIALIAS)
    background = Image.new('RGB', (width, height), (0, 0, 0))
    background.paste(image, (int(
        (width - image.size[0]) / 2), int((height - image.size[1]) / 2)))
    return background
Exemple #2
0
def convert_to_shap_values(heatmap, verbose=0):
    plt.matshow(heatmap)
    plt.axis('off')
    plt.margins(0, 0)
    # cb = fig.colorbar(im, ax=axes.ravel().tolist(), label="SHAP value", orientation="horizontal", aspect=60)
    # cb.outline.set_visible(False)
    plt.margins(0, 0)
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
    plt.savefig("heatmap.png", bbox_inches='tight', pad_inches=0)
    plt.close()
    #     img = image.load_img("heatmap.png", target_size=(7, 7, 1))

    fname = 'heatmap.png'
    image = Image.open(fname).convert("L")
    image.thumbnail((7, 7))
    arr = np.asarray(image)
    if verbose:
        plt.imshow(arr, vmin=0, vmax=255)
        plt.show()


#     print(arr)

    shap_values = []
    for i in range(5):
        shap_values.append([[item for sublist in arr for item in sublist]])
    shap_values = np.array(shap_values)
    shap_values = shap_values / np.max(shap_values)
    #     print(shap_values)
    return shap_values
Exemple #3
0
def make_image_thumbnail(filename):
    base_filename, file_extension = os.path.splitext(filename)
    thumbnail_filename = f"{base_filename}_thumbnail{file_extension}"
    image = Image.open(filename)
    image.thumbnail(size=(128, 128))
    image.save(thumbnail_filename, "JPEG")

    return thumbnail_filename
def scale_image(image, max_size, method=Image.ANTIALIAS):
    """
    resize 'image' to 'max_size' keeping the aspect ratio
    and place it in center of white 'max_size' image
    """
    image.thumbnail(max_size, method)
    offset = (int((max_size[0] - image.size[0]) / 2),
              int((max_size[1] - image.size[1]) / 2))
    back = Image.new("RGB", max_size, "white")
    back.paste(image, offset)

    return back
               anti_aliasing=False)

    # convert to 3 channel (RGB)
    x = np.stack((x, ) * 3, axis=-1)

    # Make sure it is a float32, here is why
    # https://www.quora.com/When-should-I-use-tf-float32-vs-tf-float64-in-TensorFlow
    return x.astype(np.float32)


labels = {0: 'left', 1: 'right', 2: 'upright', 3: 'upsidedown'}
image_path = '/home/ivo/Downloads/airplane_0010.jpg'
# convert image to grayscale.
image = Image.open(image_path).convert('L')
# resize the image to 28 28 to make sure it is similar to our dataset
image.thumbnail((TARGET_IMAGE_SIZE, TARGET_IMAGE_SIZE))
image = preprocess_image(np.array(image))

# setup the request
full_url = "https://image-rotation-detector.herokuapp.com/v1/models/tf_serving_keras_mobilenet/versions/1:predict"

data = {
    "signature_name": "prediction",
    "instances": [{
        "images": image.tolist()
    }]
}
data = json.dumps(data)

response = requests.post(full_url, data=data)
response = response.json()