Created
June 8, 2019 14:29
-
-
Save ksasao/cbf99dcba5ebc1e86a82e6a7ab24b852 to your computer and use it in GitHub Desktop.
人物が映っている写真をクソコラ化します。Apache License 2.0。元ネタなど https://twitter.com/ksasao/status/1137349313875419141
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on https://github.com/tensorflow/models/blob/master/research/deeplab/deeplab_demo.ipynb | |
import os | |
from io import BytesIO | |
import tarfile | |
import tempfile | |
from six.moves import urllib | |
from matplotlib import gridspec | |
from matplotlib import pyplot as plt | |
import numpy as np | |
from PIL import Image | |
import tensorflow as tf | |
import cv2 | |
import sys | |
class DeepLabModel(object): | |
"""Class to load deeplab model and run inference.""" | |
INPUT_TENSOR_NAME = 'ImageTensor:0' | |
OUTPUT_TENSOR_NAME = 'SemanticPredictions:0' | |
INPUT_SIZE = 513 | |
FROZEN_GRAPH_NAME = 'frozen_inference_graph' | |
def __init__(self, tarball_path): | |
"""Creates and loads pretrained deeplab model.""" | |
self.graph = tf.Graph() | |
graph_def = None | |
# Extract frozen graph from tar archive. | |
tar_file = tarfile.open(tarball_path) | |
for tar_info in tar_file.getmembers(): | |
if self.FROZEN_GRAPH_NAME in os.path.basename(tar_info.name): | |
file_handle = tar_file.extractfile(tar_info) | |
graph_def = tf.GraphDef.FromString(file_handle.read()) | |
break | |
tar_file.close() | |
if graph_def is None: | |
raise RuntimeError('Cannot find inference graph in tar archive.') | |
with self.graph.as_default(): | |
tf.import_graph_def(graph_def, name='') | |
self.sess = tf.Session(graph=self.graph) | |
def run(self, image): | |
"""Runs inference on a single image. | |
Args: | |
image: A PIL.Image object, raw input image. | |
Returns: | |
resized_image: RGB image resized from original input image. | |
seg_map: Segmentation map of `resized_image`. | |
""" | |
width, height = image.size | |
resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height) | |
target_size = (int(resize_ratio * width), int(resize_ratio * height)) | |
resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS) | |
batch_seg_map = self.sess.run( | |
self.OUTPUT_TENSOR_NAME, | |
feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]}) | |
seg_map = batch_seg_map[0] | |
return resized_image, seg_map | |
def create_pascal_label_colormap(): | |
"""Creates a label colormap used in PASCAL VOC segmentation benchmark. | |
Returns: | |
A Colormap for visualizing segmentation results. | |
""" | |
colormap = np.zeros((256, 3), dtype=int) | |
ind = np.arange(256, dtype=int) | |
for shift in reversed(range(8)): | |
for channel in range(3): | |
colormap[:, channel] |= ((ind >> channel) & 1) << shift | |
ind >>= 3 | |
return colormap | |
def label_to_color_image(label): | |
"""Adds color defined by the dataset colormap to the label. | |
Args: | |
label: A 2D array with integer type, storing the segmentation label. | |
Returns: | |
result: A 2D array with floating type. The element of the array | |
is the color indexed by the corresponding element in the input label | |
to the PASCAL color map. | |
Raises: | |
ValueError: If label is not of rank 2 or its value is larger than color | |
map maximum entry. | |
""" | |
if label.ndim != 2: | |
raise ValueError('Expect 2-D input label') | |
colormap = create_pascal_label_colormap() | |
if np.max(label) >= len(colormap): | |
raise ValueError('label value too large.') | |
return colormap[label] | |
def vis_segmentation(image, seg_map): | |
"""Visualizes input image, segmentation map and overlay view.""" | |
plt.figure(figsize=(15*4, 5*4)) | |
plt.rcParams["font.size"] = 12*4 | |
grid_spec = gridspec.GridSpec(1, 3, width_ratios=[6, 6, 6]) | |
plt.subplot(grid_spec[0]) | |
plt.imshow(image) | |
plt.axis('off') | |
plt.title('input image') | |
plt.subplot(grid_spec[1]) | |
# extract person | |
seg_map[seg_map != 15] = 0 | |
seg_image = label_to_color_image(seg_map).astype(np.uint8) | |
seg_image[seg_image !=0] = 255 | |
plt.imshow(seg_image) | |
plt.axis('off') | |
plt.title('extract person') | |
im1 = np.float32(image)/255.0 | |
im2 = np.float32(seg_image)/255.0 | |
blended = cv2.addWeighted(src1=im1,alpha=1.0,src2=im2,beta=0.2,gamma=0) | |
im_output = cv2.cvtColor(blended, cv2.COLOR_RGB2BGR) | |
im_output[im_output > 1] = 1.0; | |
cv2.imwrite('output.jpg',(im_output*255).astype(np.uint8)) | |
plt.subplot(grid_spec[2]) | |
plt.imshow(blended) | |
# plt.imshow(seg_image, alpha=0.7) | |
plt.axis('off') | |
plt.title('star collage') | |
plt.savefig('result.png') | |
plt.show() | |
LABEL_NAMES = np.asarray([ | |
'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', | |
'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', | |
'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv' | |
]) | |
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1) | |
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP) | |
MODEL_NAME = 'mobilenetv2_coco_voctrainaug' # @param ['mobilenetv2_coco_voctrainaug', 'mobilenetv2_coco_voctrainval', 'xception_coco_voctrainaug', 'xception_coco_voctrainval'] | |
_DOWNLOAD_URL_PREFIX = 'http://download.tensorflow.org/models/' | |
_MODEL_URLS = { | |
'mobilenetv2_coco_voctrainaug': | |
'deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz', | |
'mobilenetv2_coco_voctrainval': | |
'deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz', | |
'xception_coco_voctrainaug': | |
'deeplabv3_pascal_train_aug_2018_01_04.tar.gz', | |
'xception_coco_voctrainval': | |
'deeplabv3_pascal_trainval_2018_01_04.tar.gz', | |
} | |
_TARBALL_NAME = 'deeplab_model.tar.gz' | |
model_dir = tempfile.mkdtemp() | |
tf.gfile.MakeDirs(model_dir) | |
download_path = os.path.join(model_dir, _TARBALL_NAME) | |
print('downloading model, this might take a while...') | |
urllib.request.urlretrieve(_DOWNLOAD_URL_PREFIX + _MODEL_URLS[MODEL_NAME], | |
download_path) | |
print('download completed! loading DeepLab model...') | |
MODEL = DeepLabModel(download_path) | |
print('model loaded successfully!') | |
def run_visualization(url): | |
"""Inferences DeepLab model and visualizes result.""" | |
try: | |
original_im = Image.open(url) | |
except IOError: | |
print('Cannot retrieve image. Please check url: ' + url) | |
return | |
print('running deeplab on image %s...' % url) | |
resized_im, seg_map = MODEL.run(original_im) | |
vis_segmentation(resized_im, seg_map) | |
image_path = sys.argv[1] | |
run_visualization(image_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment