Quelques images de Mary Poppins

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

Le notebook télécharge une petite vidéo de Mary Poppins, extrait une dizaine de secondes, convertit la vidéo sous forme d’images, enlève le fond de chaque image et recompose une vidéo.

from jyquickhelper import add_notebook_menu
add_notebook_menu()

Le module utilise moviepy et pytube.

%matplotlib inline

Télécharger une vidéo et extraire une partie

On choisit une vidéo de Mary Poppins : Supercalifragilisticexpialidocious.

import os
name = 'Supercalifragilisticexpialidocious (from Mary Poppins) - Julie Andrews Dick Van Dyke.mp4'
if not os.path.exists(name):
    from code_beatrix.art.video import download_youtube_video
    name = download_youtube_video(tag='DYacXAd3eAo', res=None)
name
'Marie Poppins - Supercalifragilisticexpilialidocious.mp4'

On extraire une dizaine de secondes.

from code_beatrix.art.video import video_extract_video, video_save
vid = video_extract_video(name, '00:00:32', '00:00:45')
video_save(vid, "mpoppins.mp4")

Convertir la vidéo en images

folder = "images"
import os
if not os.path.exists(folder):
    os.mkdir(folder)
from code_beatrix.art.video import video_enumerate_frames
imgs = list(video_enumerate_frames("mpoppins.mp4", folder=folder))
imgs[:5]
['images\images_0000.jpg',
 'images\images_0001.jpg',
 'images\images_0002.jpg',
 'images\images_0003.jpg',
 'images\images_0004.jpg']
from IPython.display import Image
Image(imgs[0], width=360)
../_images/image_mary_poppins_11_0.jpeg
Image(imgs[10], width=360)
../_images/image_mary_poppins_12_01.jpeg
Image(imgs[-1], width=360)
../_images/image_mary_poppins_13_0.jpeg

Extraire les personnages de l’image

On essaye différentes résolutions. On s’aperçoit que les personnages de dessin animé sont aussi considérés comme des personnes.

from code_beatrix.ai import DLImageSegmentation
model = DLImageSegmentation()
499MB [05:59, 1.39MB/s]
feat, pred = model.predict(imgs[0], resize=('max2', 400))
viz = model.plot(feat, pred)
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(14, 12))
ax.imshow(viz)
<matplotlib.image.AxesImage at 0x26927df8e10>
../_images/image_mary_poppins_18_1.png
feat, pred = model.predict(imgs[10], resize=('max2', 400))
viz = model.plot(feat, pred)
fig, ax = plt.subplots(1, 1, figsize=(14, 12))
ax.imshow(viz)
<matplotlib.image.AxesImage at 0x26900433320>
../_images/image_mary_poppins_19_1.png
feat, pred = model.predict(imgs[10])
viz = model.plot(feat, pred)
fig, ax = plt.subplots(1, 1, figsize=(14, 12))
ax.imshow(viz)
<matplotlib.image.AxesImage at 0x26900630ba8>
../_images/image_mary_poppins_20_1.png

Combiner les personnages avec une autre image

pred[pred!=0]
array([15, 15, 15, ..., 15, 15, 15])
feat.shape
(360, 480, 3)
feat[pred!=15] = 0
fig, ax = plt.subplots(1, 1, figsize=(7, 6))
ax.imshow(feat)
<matplotlib.image.AxesImage at 0x26900698400>
../_images/image_mary_poppins_25_1.png
from skimage.io import imread
from skimage.transform import resize
from skimage import img_as_ubyte
img = imread("data/plage.jpg")
img = img_as_ubyte(resize(img, (360, 480)))
fig, ax = plt.subplots(1, 1, figsize=(7, 6))
ax.imshow(img)
<matplotlib.image.AxesImage at 0x26900ee8940>
../_images/image_mary_poppins_26_1.png
img.shape, feat.shape
((360, 480, 3), (360, 480, 3))
img[pred==15] = feat[pred==15]
fig, ax = plt.subplots(1, 1, figsize=(7, 6))
ax.imshow(img)
<matplotlib.image.AxesImage at 0x269009ab0b8>
../_images/image_mary_poppins_29_11.png