Segmenter une image pixel à pixel

Links: notebook, html, PDF, python, slides, GitHub

Il est aujourd’hui assez facile de construire une boîte englobante autour d’un visage dans une image. Le deep learning permet d’extraire précisément les pixels du visage.

from jyquickhelper import add_notebook_menu
add_notebook_menu()
%matplotlib inline

Chargement du modèle

from code_beatrix.ai import DLImageSegmentation
model = DLImageSegmentation(fLOG=print)
[DLImageSegmentation] download model 'FCN8s'
[DLImageSegmentation] [C:Usersxavie/data/models/chainer/fcn8s_from_caffe.npz] Checking md5 (256c2a8235c1c65e62e48d3284fbd384)
[DLImageSegmentation] load_npz 'C:Usersxavie/data/models/chainer/fcn8s_from_caffe.npz'
[DLImageSegmentation] class_name '['background' 'aeroplane' 'bicycle' 'bird' 'boat' 'bottle' 'bus' 'car'
 'cat' 'chair' 'cow' 'diningtable' 'dog' 'horse' 'motorbike' 'person'
 'potted plant' 'sheep' 'sofa' 'train' 'tv/monitor']'
[DLImageSegmentation] cpu

Sur une petite image

img = 'images/Tesla_circa_1890c.jpg'
feat, pred = model.predict(img)
pred.shape
(295, 220)
viz = model.plot(img, pred)  # img ou feat
c:Python363_x64libsite-packagesskimagetransform_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
import skimage.io as skio
skio.imshow(viz)
<matplotlib.image.AxesImage at 0x18000a1a8d0>
../_images/image_segmentation_9_1.png

Sur une image dont on change la taille

from PIL import Image
img = 'images/Tesla_circa_1890c.jpg'
pilimg = Image.open(img)
si = pilimg.size
pilimg2 = pilimg.resize((si[0]//2, si[1]//2))
from skimage.io._plugins.pil_plugin import pil_to_ndarray
skimg = pil_to_ndarray(pilimg2)
skimg.shape
(147, 110, 3)
feat, pred = model.predict(skimg)
pred.shape
(147, 110)
viz = model.plot(skimg, pred)
c:Python363_x64libsite-packagesskimagetransform_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
skio.imshow(viz)
<matplotlib.image.AxesImage at 0x18001a8bd68>
../_images/image_segmentation_17_1.png

Sur une grande image

img = 'images/h2015_2.jpg'
pilimg = Image.open(img)
si = pilimg.size
pilimg2 = pilimg.resize((si[0]//2, si[1]//2))
skimg = pil_to_ndarray(pilimg2)
skimg.shape
(456, 684, 3)
skio.imshow(skimg)
<matplotlib.image.AxesImage at 0x1800210b9e8>
../_images/image_segmentation_23_1.png
feat, pred = model.predict(skimg)
pred.shape
(456, 684)
viz = model.plot(feat, pred)
c:Python363_x64libsite-packagesskimagetransform_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(14, 12))
ax.imshow(viz)
<matplotlib.image.AxesImage at 0x1800216b9e8>
../_images/image_segmentation_26_1.png