Advertisement
ZeroCool22

FaceSwap_GAN_v2_test_video.ipynb (B to A or A to B?)

Feb 9th, 2018
15,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.26 KB | None | 0 0
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "markdown",
  5.    "metadata": {},
  6.    "source": [
  7.     "<a id='1'></a>\n",
  8.     "# 1. Import packages"
  9.    ]
  10.   },
  11.   {
  12.    "cell_type": "code",
  13.    "execution_count": 1,
  14.    "metadata": {},
  15.    "outputs": [
  16.     {
  17.      "name": "stderr",
  18.      "output_type": "stream",
  19.      "text": [
  20.       "Using TensorFlow backend.\n"
  21.      ]
  22.     }
  23.    ],
  24.    "source": [
  25.     "from keras.models import Sequential, Model\n",
  26.     "from keras.layers import *\n",
  27.     "from keras.layers.advanced_activations import LeakyReLU\n",
  28.     "from keras.activations import relu\n",
  29.     "from keras.initializers import RandomNormal\n",
  30.     "from keras.applications import *\n",
  31.     "import keras.backend as K\n",
  32.     "from tensorflow.contrib.distributions import Beta\n",
  33.     "import tensorflow as tf\n",
  34.     "from keras.optimizers import Adam"
  35.    ]
  36.   },
  37.   {
  38.    "cell_type": "code",
  39.    "execution_count": 2,
  40.    "metadata": {
  41.     "collapsed": true
  42.    },
  43.    "outputs": [],
  44.    "source": [
  45.     "from image_augmentation import random_transform\n",
  46.     "from image_augmentation import random_warp\n",
  47.     "from utils import get_image_paths, load_images, stack_images\n",
  48.     "from pixel_shuffler import PixelShuffler"
  49.    ]
  50.   },
  51.   {
  52.    "cell_type": "code",
  53.    "execution_count": 3,
  54.    "metadata": {
  55.     "collapsed": true
  56.    },
  57.    "outputs": [],
  58.    "source": [
  59.     "import time\n",
  60.     "import numpy as np\n",
  61.     "from PIL import Image\n",
  62.     "import cv2\n",
  63.     "import glob\n",
  64.     "from random import randint, shuffle\n",
  65.     "from IPython.display import clear_output\n",
  66.     "from IPython.display import display\n",
  67.     "import matplotlib.pyplot as plt\n",
  68.     "%matplotlib inline"
  69.    ]
  70.   },
  71.   {
  72.    "cell_type": "markdown",
  73.    "metadata": {},
  74.    "source": [
  75.     "<a id='2'></a>\n",
  76.     "# 2. Install requirements\n",
  77.     "\n",
  78.     "## ========== CAUTION ========== \n",
  79.     "\n",
  80.     "If you are running this jupyter on local machine. Please read [this blog](http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/) before running the following cells which pip install packages."
  81.    ]
  82.   },
  83.   {
  84.    "cell_type": "code",
  85.    "execution_count": 4,
  86.    "metadata": {
  87.     "collapsed": true
  88.    },
  89.    "outputs": [],
  90.    "source": [
  91.     "# https://github.com/ageitgey/face_recognition\n",
  92.     "#!pip install face_recognition"
  93.    ]
  94.   },
  95.   {
  96.    "cell_type": "code",
  97.    "execution_count": 5,
  98.    "metadata": {
  99.     "collapsed": true
  100.    },
  101.    "outputs": [],
  102.    "source": [
  103.     "#!pip install moviepy"
  104.    ]
  105.   },
  106.   {
  107.    "cell_type": "markdown",
  108.    "metadata": {},
  109.    "source": [
  110.     "<a id='4'></a>\n",
  111.     "# 4. Config\n",
  112.     "\n",
  113.     "mixup paper: https://arxiv.org/abs/1710.09412\n",
  114.     "\n",
  115.     "Default training data directories: `./faceA/` and `./faceB/`"
  116.    ]
  117.   },
  118.   {
  119.    "cell_type": "code",
  120.    "execution_count": 6,
  121.    "metadata": {
  122.     "collapsed": true
  123.    },
  124.    "outputs": [],
  125.    "source": [
  126.     "K.set_learning_phase(1)"
  127.    ]
  128.   },
  129.   {
  130.    "cell_type": "code",
  131.    "execution_count": 7,
  132.    "metadata": {
  133.     "collapsed": true
  134.    },
  135.    "outputs": [],
  136.    "source": [
  137.     "channel_axis=-1\n",
  138.     "channel_first = False"
  139.    ]
  140.   },
  141.   {
  142.    "cell_type": "code",
  143.    "execution_count": 8,
  144.    "metadata": {
  145.     "collapsed": true
  146.    },
  147.    "outputs": [],
  148.    "source": [
  149.     "IMAGE_SHAPE = (64, 64, 3)\n",
  150.     "nc_in = 3 # number of input channels of generators\n",
  151.     "nc_D_inp = 6 # number of input channels of discriminators\n",
  152.     "\n",
  153.     "use_perceptual_loss = False\n",
  154.     "use_lsgan = True\n",
  155.     "use_instancenorm = False\n",
  156.     "use_mixup = True\n",
  157.     "mixup_alpha = 0.2 # 0.2\n",
  158.     "\n",
  159.     "batchSize = 32\n",
  160.     "lrD = 1e-4 # Discriminator learning rate\n",
  161.     "lrG = 1e-4 # Generator learning rate\n",
  162.     "\n",
  163.     "# Path of training images\n",
  164.     "img_dirA = './faceA/*.*'\n",
  165.     "img_dirB = './faceB/*.*'"
  166.    ]
  167.   },
  168.   {
  169.    "cell_type": "markdown",
  170.    "metadata": {},
  171.    "source": [
  172.     "<a id='5'></a>\n",
  173.     "# 5. Define models"
  174.    ]
  175.   },
  176.   {
  177.    "cell_type": "code",
  178.    "execution_count": 9,
  179.    "metadata": {
  180.     "collapsed": true
  181.    },
  182.    "outputs": [],
  183.    "source": [
  184.     "from model_GAN_v2 import *"
  185.    ]
  186.   },
  187.   {
  188.    "cell_type": "code",
  189.    "execution_count": 10,
  190.    "metadata": {
  191.     "collapsed": true,
  192.     "scrolled": true
  193.    },
  194.    "outputs": [],
  195.    "source": [
  196.     "encoder = Encoder()\n",
  197.     "decoder_A = Decoder_ps()\n",
  198.     "decoder_B = Decoder_ps()\n",
  199.     "\n",
  200.     "x = Input(shape=IMAGE_SHAPE)\n",
  201.     "\n",
  202.     "netGA = Model(x, decoder_A(encoder(x)))\n",
  203.     "netGB = Model(x, decoder_B(encoder(x)))"
  204.    ]
  205.   },
  206.   {
  207.    "cell_type": "code",
  208.    "execution_count": 11,
  209.    "metadata": {
  210.     "collapsed": true
  211.    },
  212.    "outputs": [],
  213.    "source": [
  214.     "netDA = Discriminator(nc_D_inp)\n",
  215.     "netDB = Discriminator(nc_D_inp)"
  216.    ]
  217.   },
  218.   {
  219.    "cell_type": "markdown",
  220.    "metadata": {},
  221.    "source": [
  222.     "<a id='6'></a>\n",
  223.     "# 6. Load Models"
  224.    ]
  225.   },
  226.   {
  227.    "cell_type": "code",
  228.    "execution_count": 12,
  229.    "metadata": {},
  230.    "outputs": [
  231.     {
  232.      "name": "stdout",
  233.      "output_type": "stream",
  234.      "text": [
  235.       "model loaded.\n"
  236.      ]
  237.     }
  238.    ],
  239.    "source": [
  240.     "try:\n",
  241.     "    encoder.load_weights(\"models/encoder.h5\")\n",
  242.     "    decoder_A.load_weights(\"models/decoder_A.h5\")\n",
  243.     "    decoder_B.load_weights(\"models/decoder_B.h5\")\n",
  244.     "    #netDA.load_weights(\"models/netDA.h5\") \n",
  245.     "    #netDB.load_weights(\"models/netDB.h5\") \n",
  246.     "    print (\"model loaded.\")\n",
  247.     "except:\n",
  248.     "    print (\"Weights file not found.\")\n",
  249.     "    pass"
  250.    ]
  251.   },
  252.   {
  253.    "cell_type": "markdown",
  254.    "metadata": {},
  255.    "source": [
  256.     "<a id='7'></a>\n",
  257.     "# 7. Define Inputs/Outputs Variables\n",
  258.     "\n",
  259.     "    distorted_A: A (batch_size, 64, 64, 3) tensor, input of generator_A (netGA).\n",
  260.     "    distorted_B: A (batch_size, 64, 64, 3) tensor, input of generator_B (netGB).\n",
  261.     "    fake_A: (batch_size, 64, 64, 3) tensor, output of generator_A (netGA).\n",
  262.     "    fake_B: (batch_size, 64, 64, 3) tensor, output of generator_B (netGB).\n",
  263.     "    mask_A: (batch_size, 64, 64, 1) tensor, mask output of generator_A (netGA).\n",
  264.     "    mask_B: (batch_size, 64, 64, 1) tensor, mask output of generator_B (netGB).\n",
  265.     "    path_A: A function that takes distorted_A as input and outputs fake_A.\n",
  266.     "    path_B: A function that takes distorted_B as input and outputs fake_B.\n",
  267.     "    path_mask_A: A function that takes distorted_A as input and outputs mask_A.\n",
  268.     "    path_mask_B: A function that takes distorted_B as input and outputs mask_B.\n",
  269.     "    path_abgr_A: A function that takes distorted_A as input and outputs concat([mask_A, fake_A]).\n",
  270.     "    path_abgr_B: A function that takes distorted_B as input and outputs concat([mask_B, fake_B]).\n",
  271.     "    real_A: A (batch_size, 64, 64, 3) tensor, target images for generator_A given input distorted_A.\n",
  272.     "    real_B: A (batch_size, 64, 64, 3) tensor, target images for generator_B given input distorted_B."
  273.    ]
  274.   },
  275.   {
  276.    "cell_type": "code",
  277.    "execution_count": 13,
  278.    "metadata": {
  279.     "collapsed": true
  280.    },
  281.    "outputs": [],
  282.    "source": [
  283.     "def cycle_variables(netG):\n",
  284.     "    distorted_input = netG.inputs[0]\n",
  285.     "    fake_output = netG.outputs[0]\n",
  286.     "    alpha = Lambda(lambda x: x[:,:,:, :1])(fake_output)\n",
  287.     "    rgb = Lambda(lambda x: x[:,:,:, 1:])(fake_output)\n",
  288.     "    \n",
  289.     "    masked_fake_output = alpha * rgb + (1-alpha) * distorted_input \n",
  290.     "\n",
  291.     "    fn_generate = K.function([distorted_input], [masked_fake_output])\n",
  292.     "    fn_mask = K.function([distorted_input], [concatenate([alpha, alpha, alpha])])\n",
  293.     "    fn_abgr = K.function([distorted_input], [concatenate([alpha, rgb])])\n",
  294.     "    return distorted_input, fake_output, alpha, fn_generate, fn_mask, fn_abgr"
  295.    ]
  296.   },
  297.   {
  298.    "cell_type": "code",
  299.    "execution_count": 14,
  300.    "metadata": {
  301.     "collapsed": true
  302.    },
  303.    "outputs": [],
  304.    "source": [
  305.     "distorted_A, fake_A, mask_A, path_A, path_mask_A, path_abgr_A = cycle_variables(netGA)\n",
  306.     "distorted_B, fake_B, mask_B, path_B, path_mask_B, path_abgr_B = cycle_variables(netGB)\n",
  307.     "real_A = Input(shape=IMAGE_SHAPE)\n",
  308.     "real_B = Input(shape=IMAGE_SHAPE)"
  309.    ]
  310.   },
  311.   {
  312.    "cell_type": "code",
  313.    "execution_count": null,
  314.    "metadata": {
  315.     "collapsed": true
  316.    },
  317.    "outputs": [],
  318.    "source": []
  319.   },
  320.   {
  321.    "cell_type": "markdown",
  322.    "metadata": {},
  323.    "source": [
  324.     "<a id='12'></a>\n",
  325.     "# 12. Make video clips\n",
  326.     "\n",
  327.     "Given a video as input, the following cells will detect face for each frame using dlib's cnn model. And use trained GAN model to transform detected face into target face. Then output a video with swapped faces."
  328.    ]
  329.   },
  330.   {
  331.    "cell_type": "code",
  332.    "execution_count": 15,
  333.    "metadata": {
  334.     "collapsed": true,
  335.     "scrolled": true
  336.    },
  337.    "outputs": [],
  338.    "source": [
  339.     "# Download ffmpeg if need, which is required by moviepy.\n",
  340.     "\n",
  341.     "#import imageio\n",
  342.     "#imageio.plugins.ffmpeg.download()"
  343.    ]
  344.   },
  345.   {
  346.    "cell_type": "code",
  347.    "execution_count": 16,
  348.    "metadata": {
  349.     "collapsed": true
  350.    },
  351.    "outputs": [],
  352.    "source": [
  353.     "import face_recognition\n",
  354.     "from moviepy.editor import VideoFileClip"
  355.    ]
  356.   },
  357.   {
  358.    "cell_type": "code",
  359.    "execution_count": 17,
  360.    "metadata": {
  361.     "collapsed": true
  362.    },
  363.    "outputs": [],
  364.    "source": [
  365.     "whom2whom = \"BtoA\" # default trainsforming faceB to faceA\n",
  366.     "\n",
  367.     "if whom2whom is \"AtoB\":\n",
  368.     "    path_func = path_abgr_B\n",
  369.     "elif whom2whom is \"BtoA\":\n",
  370.     "    path_func = path_abgr_A\n",
  371.     "else:\n",
  372.     "    print (\"whom2whom should be either AtoB or BtoA\")"
  373.    ]
  374.   },
  375.   {
  376.    "cell_type": "markdown",
  377.    "metadata": {},
  378.    "source": [
  379.     "<a id='13'></a>\n",
  380.     "# 13. Make video clips w/o face alignment\n",
  381.     "\n",
  382.     "### Default transform: face B to face A"
  383.    ]
  384.   },
  385.   {
  386.    "cell_type": "code",
  387.    "execution_count": 18,
  388.    "metadata": {
  389.     "collapsed": true
  390.    },
  391.    "outputs": [],
  392.    "source": [
  393.     "use_smoothed_mask = True\n",
  394.     "use_smoothed_bbox = True\n",
  395.     "\n",
  396.     "def is_higher_than_480p(x):\n",
  397.     "    return (x.shape[1] >= 858 and x.shape[0] >= 480)\n",
  398.     "\n",
  399.     "def is_higher_than_720p(x):\n",
  400.     "    return (x.shape[1] >= 1280 and x.shape[0] >= 720)\n",
  401.     "\n",
  402.     "def is_higher_than_1080p(x):\n",
  403.     "    return (x.shape[1] >= 1920 and x.shape[0] >= 1080)\n",
  404.     "\n",
  405.     "def calibrate_coord(faces, video_scaling_factor):\n",
  406.     "    for i, (x0, y1, x1, y0) in enumerate(faces):\n",
  407.     "        faces[i] = (x0*video_scaling_factor, y1*video_scaling_factor, \n",
  408.     "                    x1*video_scaling_factor, y0*video_scaling_factor)\n",
  409.     "    return faces\n",
  410.     "\n",
  411.     "def get_faces_bbox(image, model=\"hog\"):  \n",
  412.     "    if is_higher_than_1080p(image):\n",
  413.     "        video_scaling_factor = 4 + video_scaling_offset\n",
  414.     "        resized_image = cv2.resize(image, \n",
  415.     "                                   (image.shape[1]//video_scaling_factor, image.shape[0]//video_scaling_factor))\n",
  416.     "        faces = face_recognition.face_locations(resized_image, model=model)\n",
  417.     "        faces = calibrate_coord(faces, video_scaling_factor)\n",
  418.     "    elif is_higher_than_720p(image):\n",
  419.     "        video_scaling_factor = 3 + video_scaling_offset\n",
  420.     "        resized_image = cv2.resize(image, \n",
  421.     "                                   (image.shape[1]//video_scaling_factor, image.shape[0]//video_scaling_factor))\n",
  422.     "        faces = face_recognition.face_locations(resized_image, model=model)\n",
  423.     "        faces = calibrate_coord(faces, video_scaling_factor)  \n",
  424.     "    elif is_higher_than_480p(image):\n",
  425.     "        video_scaling_factor = 2 + video_scaling_offset\n",
  426.     "        resized_image = cv2.resize(image, \n",
  427.     "                                   (image.shape[1]//video_scaling_factor, image.shape[0]//video_scaling_factor))\n",
  428.     "        faces = face_recognition.face_locations(resized_image, model=model)\n",
  429.     "        faces = calibrate_coord(faces, video_scaling_factor)\n",
  430.     "    else:\n",
  431.     "        faces = face_recognition.face_locations(image, model=model)\n",
  432.     "    return faces\n",
  433.     "\n",
  434.     "def get_smoothed_coord(x0, x1, y0, y1):\n",
  435.     "    global prev_x0, prev_x1, prev_y0, prev_y1\n",
  436.     "    x0 = int(0.65*prev_x0 + 0.35*x0)\n",
  437.     "    x1 = int(0.65*prev_x1 + 0.35*x1)\n",
  438.     "    y1 = int(0.65*prev_y1 + 0.35*y1)\n",
  439.     "    y0 = int(0.65*prev_y0 + 0.35*y0)\n",
  440.     "    return x0, x1, y0, y1    \n",
  441.     "    \n",
  442.     "def set_global_coord(x0, x1, y0, y1):\n",
  443.     "    global prev_x0, prev_x1, prev_y0, prev_y1\n",
  444.     "    prev_x0 = x0\n",
  445.     "    prev_x1 = x1\n",
  446.     "    prev_y1 = y1\n",
  447.     "    prev_y0 = y0\n",
  448.     "    \n",
  449.     "def generate_face(ae_input, path_abgr, roi_size):\n",
  450.     "    result = np.squeeze(np.array([path_abgr([[ae_input]])]))\n",
  451.     "    result_a = result[:,:,0] * 255\n",
  452.     "    result_bgr = np.clip( (result[:,:,1:] + 1) * 255 / 2, 0, 255 )\n",
  453.     "    result_a = cv2.GaussianBlur(result_a ,(7,7),6)\n",
  454.     "    result_a = np.expand_dims(result_a, axis=2)\n",
  455.     "    result = (result_a/255 * result_bgr + (1 - result_a/255) * ((ae_input + 1) * 255 / 2)).astype('uint8')\n",
  456.     "    result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)\n",
  457.     "    result = cv2.resize(result, (roi_size[1],roi_size[0]))\n",
  458.     "    result_a = np.expand_dims(cv2.resize(result_a, (roi_size[1],roi_size[0])), axis=2)\n",
  459.     "    return result, result_a\n",
  460.     "\n",
  461.     "def get_init_mask_map(image):\n",
  462.     "    return np.zeros_like(image)\n",
  463.     "\n",
  464.     "def get_init_comb_img(input_img):\n",
  465.     "    comb_img = np.zeros([input_img.shape[0], input_img.shape[1]*2,input_img.shape[2]])\n",
  466.     "    comb_img[:, :input_img.shape[1], :] = input_img\n",
  467.     "    comb_img[:, input_img.shape[1]:, :] = input_img\n",
  468.     "    return comb_img    \n",
  469.     "\n",
  470.     "def get_init_triple_img(input_img, no_face=False):\n",
  471.     "    if no_face:\n",
  472.     "        triple_img = np.zeros([input_img.shape[0], input_img.shape[1]*3,input_img.shape[2]])\n",
  473.     "        triple_img[:, :input_img.shape[1], :] = input_img\n",
  474.     "        triple_img[:, input_img.shape[1]:input_img.shape[1]*2, :] = input_img      \n",
  475.     "        triple_img[:, input_img.shape[1]*2:, :] = (input_img * .15).astype('uint8')  \n",
  476.     "        return triple_img\n",
  477.     "    else:\n",
  478.     "        triple_img = np.zeros([input_img.shape[0], input_img.shape[1]*3,input_img.shape[2]])\n",
  479.     "        return triple_img\n",
  480.     "\n",
  481.     "def get_mask(roi_image, h, w):\n",
  482.     "    mask = np.zeros_like(roi_image)\n",
  483.     "    mask[h//15:-h//15,w//15:-w//15,:] = 255\n",
  484.     "    mask = cv2.GaussianBlur(mask,(15,15),10)\n",
  485.     "    return mask\n",
  486.     "\n",
  487.     "def process_video(input_img):   \n",
  488.     "    # modify this line to reduce input size\n",
  489.     "    #input_img = input_img[:, input_img.shape[1]//3:2*input_img.shape[1]//3,:] \n",
  490.     "    image = input_img\n",
  491.     "    faces = get_faces_bbox(image, model=\"hog\")\n",
  492.     "    \n",
  493.     "    if len(faces) == 0:\n",
  494.     "        comb_img = get_init_comb_img(input_img)\n",
  495.     "        triple_img = get_init_triple_img(input_img, no_face=True)\n",
  496.     "        \n",
  497.     "    mask_map = get_init_mask_map(image)\n",
  498.     "    comb_img = get_init_comb_img(input_img)\n",
  499.     "    global prev_x0, prev_x1, prev_y0, prev_y1\n",
  500.     "    global frames    \n",
  501.     "    for (x0, y1, x1, y0) in faces:        \n",
  502.     "        # smoothing bounding box\n",
  503.     "        if use_smoothed_bbox:\n",
  504.     "            if frames != 0:\n",
  505.     "                x0, x1, y0, y1 = get_smoothed_coord(x0, x1, y0, y1)\n",
  506.     "                set_global_coord(x0, x1, y0, y1)\n",
  507.     "            else:\n",
  508.     "                set_global_coord(x0, x1, y0, y1)\n",
  509.     "                frames += 1\n",
  510.     "        h = x1 - x0\n",
  511.     "        w = y1 - y0\n",
  512.     "            \n",
  513.     "        cv2_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
  514.     "        roi_image = cv2_img[x0+h//15:x1-h//15,y0+w//15:y1-w//15,:]\n",
  515.     "        roi_size = roi_image.shape  \n",
  516.     "        \n",
  517.     "        ae_input = cv2.resize(roi_image, (64,64))/255. * 2 - 1        \n",
  518.     "        result, result_a = generate_face(ae_input, path_abgr_A, roi_size)\n",
  519.     "        mask_map[x0+h//15:x1-h//15, y0+w//15:y1-w//15,:] = result_a\n",
  520.     "        mask_map = np.clip(mask_map + .15 * input_img, 0, 255 )     \n",
  521.     "        \n",
  522.     "        if use_smoothed_mask:\n",
  523.     "            mask = get_mask(roi_image, h, w)\n",
  524.     "            roi_rgb = cv2.cvtColor(roi_image, cv2.COLOR_BGR2RGB)\n",
  525.     "            smoothed_result = mask/255 * result + (1-mask/255) * roi_rgb\n",
  526.     "            comb_img[x0+h//15:x1-h//15, input_img.shape[1]+y0+w//15:input_img.shape[1]+y1-w//15,:] = smoothed_result\n",
  527.     "        else:\n",
  528.     "            comb_img[x0+h//15:x1-h//15, input_img.shape[1]+y0+w//15:input_img.shape[1]+y1-w//15,:] = result\n",
  529.     "            \n",
  530.     "        triple_img = get_init_triple_img(input_img)\n",
  531.     "        triple_img[:, :input_img.shape[1]*2, :] = comb_img\n",
  532.     "        triple_img[:, input_img.shape[1]*2:, :] = mask_map\n",
  533.     "    \n",
  534.     "    # ========== Change rthe following line to ==========\n",
  535.     "    return comb_img[:, input_img.shape[1]:, :]  # return only result image\n",
  536.     "    # return comb_img  # return input and result image combined as one\n",
  537.     "    #return triple_img #return input,result and mask heatmap image combined as one"
  538.    ]
  539.   },
  540.   {
  541.    "cell_type": "code",
  542.    "execution_count": 19,
  543.    "metadata": {},
  544.    "outputs": [
  545.     {
  546.      "name": "stdout",
  547.      "output_type": "stream",
  548.      "text": [
  549.       "[MoviePy] >>>> Building video OUTPUT_VIDEO.mp4\n",
  550.       "[MoviePy] Writing video OUTPUT_VIDEO.mp4\n"
  551.      ]
  552.     },
  553.     {
  554.      "name": "stderr",
  555.      "output_type": "stream",
  556.      "text": [
  557.       "100%|████████████████████████████████████████████████████████████████████████████████| 341/341 [00:46<00:00,  7.12it/s]\n"
  558.      ]
  559.     },
  560.     {
  561.      "name": "stdout",
  562.      "output_type": "stream",
  563.      "text": [
  564.       "[MoviePy] Done.\n",
  565.       "[MoviePy] >>>> Video ready: OUTPUT_VIDEO.mp4 \n",
  566.       "\n",
  567.       "Wall time: 46.9 s\n"
  568.      ]
  569.     }
  570.    ],
  571.    "source": [
  572.     "# Variables for smoothing bounding box\n",
  573.     "global prev_x0, prev_x1, prev_y0, prev_y1\n",
  574.     "global frames\n",
  575.     "prev_x0 = prev_x1 = prev_y0 = prev_y1 = 0\n",
  576.     "frames = 0\n",
  577.     "video_scaling_offset = 0 # Increase by 1 if OOM happens.\n",
  578.     "\n",
  579.     "output = 'OUTPUT_VIDEO.mp4'\n",
  580.     "clip1 = VideoFileClip(\"INPUT_VIDEO.mp4\")\n",
  581.     "clip = clip1.fl_image(process_video)#.subclip(11, 13) #NOTE: this function expects color images!!\n",
  582.     "%time clip.write_videofile(output, audio=False)"
  583.    ]
  584.   },
  585.   {
  586.    "cell_type": "markdown",
  587.    "metadata": {},
  588.    "source": [
  589.     "### gc.collect() sometimes solves memory error"
  590.    ]
  591.   },
  592.   {
  593.    "cell_type": "code",
  594.    "execution_count": 20,
  595.    "metadata": {},
  596.    "outputs": [
  597.     {
  598.      "data": {
  599.       "text/plain": [
  600.        "8409"
  601.       ]
  602.      },
  603.      "execution_count": 20,
  604.      "metadata": {},
  605.      "output_type": "execute_result"
  606.     }
  607.    ],
  608.    "source": [
  609.     "import gc\n",
  610.     "gc.collect()"
  611.    ]
  612.   },
  613.   {
  614.    "cell_type": "code",
  615.    "execution_count": null,
  616.    "metadata": {
  617.     "collapsed": true
  618.    },
  619.    "outputs": [],
  620.    "source": []
  621.   }
  622.  ],
  623.  "metadata": {
  624.   "kernelspec": {
  625.    "display_name": "Python 3",
  626.    "language": "python",
  627.    "name": "python3"
  628.   },
  629.   "language_info": {
  630.    "codemirror_mode": {
  631.     "name": "ipython",
  632.     "version": 3
  633.    },
  634.    "file_extension": ".py",
  635.    "mimetype": "text/x-python",
  636.    "name": "python",
  637.    "nbconvert_exporter": "python",
  638.    "pygments_lexer": "ipython3",
  639.    "version": "3.6.3"
  640.   }
  641.  },
  642.  "nbformat": 4,
  643.  "nbformat_minor": 2
  644. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement