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()
run previous cell, wait for 2 seconds

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='tRFHXMQP-QU')
name
'Supercalifragilisticexpialidocious (from Mary Poppins) - Julie Andrews Dick Van Dyke.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_02.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()
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 0x1ba00003b00>
../_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 0x1ba00055400>
../_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 0x1ba00279860>
../_images/image_mary_poppins_20_1.png

Combiner les personnages avec une autre image

pred[pred!=0]
array([15, 15, 15, ...,  8,  8,  8])
feat.shape
(720, 1280, 3)
feat[pred!=15] = 0
fig, ax = plt.subplots(1, 1, figsize=(7, 6))
ax.imshow(feat)
<matplotlib.image.AxesImage at 0x1ba001574a8>
../_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, (720, 1280)))
fig, ax = plt.subplots(1, 1, figsize=(7, 6))
ax.imshow(img)
WARNING:py.warnings: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 "
WARNING:py.warnings:c:Python363_x64libsite-packagesskimageutildtype.py:122: UserWarning: Possible precision loss when converting from float64 to uint8
  .format(dtypeobj_in, dtypeobj_out))
<matplotlib.image.AxesImage at 0x1ba052f39e8>
../_images/image_mary_poppins_26_2.png
img.shape, feat.shape
((720, 1280, 3), (720, 1280, 3))
img[pred==15] = feat[pred==15]
fig, ax = plt.subplots(1, 1, figsize=(7, 6))
ax.imshow(img)
<matplotlib.image.AxesImage at 0x1ba04fe3320>
../_images/image_mary_poppins_29_12.png

Mary Poppins danse sur l’eau