Segmenter une image pixel à pixel

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.

In [1]:
from jyquickhelper import add_notebook_menu
add_notebook_menu()
Out[1]:
run previous cell, wait for 2 seconds
In [2]:
%matplotlib inline

Chargement du modèle

In [3]:
from code_beatrix.ai import DLImageSegmentation
model = DLImageSegmentation(fLOG=print)
[DLImageSegmentation] download model 'FCN8s'
[DLImageSegmentation] [C:\Users\xavie/data/models/chainer/fcn8s_from_caffe.npz] Checking md5 (256c2a8235c1c65e62e48d3284fbd384)

[DLImageSegmentation] load_npz 'C:\Users\xavie/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

In [4]:
img = 'images/Tesla_circa_1890c.jpg'
In [5]:
feat, pred = model.predict(img)
pred.shape
Out[5]:
(295, 220)
In [6]:
viz = model.plot(img, pred)  # img ou feat
c:\Python363_x64\lib\site-packages\skimage\transform\_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 "
In [7]:
import skimage.io as skio
skio.imshow(viz)
Out[7]:
<matplotlib.image.AxesImage at 0x18000a1a8d0>

Sur une image dont on change la taille

In [8]:
from PIL import Image
img = 'images/Tesla_circa_1890c.jpg'
pilimg = Image.open(img)
In [9]:
si = pilimg.size
pilimg2 = pilimg.resize((si[0]//2, si[1]//2))
In [10]:
from skimage.io._plugins.pil_plugin import pil_to_ndarray
In [11]:
skimg = pil_to_ndarray(pilimg2)
skimg.shape
Out[11]:
(147, 110, 3)
In [12]:
feat, pred = model.predict(skimg)
pred.shape
Out[12]:
(147, 110)
In [13]:
viz = model.plot(skimg, pred)
c:\Python363_x64\lib\site-packages\skimage\transform\_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 "
In [14]:
skio.imshow(viz)
Out[14]:
<matplotlib.image.AxesImage at 0x18001a8bd68>

Sur une grande image

In [15]:
img = 'images/h2015_2.jpg'
In [16]:
pilimg = Image.open(img)
In [17]:
si = pilimg.size
pilimg2 = pilimg.resize((si[0]//2, si[1]//2))
In [18]:
skimg = pil_to_ndarray(pilimg2)
skimg.shape
Out[18]:
(456, 684, 3)
In [19]:
skio.imshow(skimg)
Out[19]:
<matplotlib.image.AxesImage at 0x1800210b9e8>
In [20]:
feat, pred = model.predict(skimg)
pred.shape
Out[20]:
(456, 684)
In [21]:
viz = model.plot(feat, pred)
c:\Python363_x64\lib\site-packages\skimage\transform\_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 "
In [22]:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(14, 12))
ax.imshow(viz)
Out[22]:
<matplotlib.image.AxesImage at 0x1800216b9e8>
In [23]: