{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Deeper-3D-Ken-Burns-single-pic.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/mrm8488/shared_colab_notebooks/blob/master/Deeper_3D_Ken_Burns_single_pic.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uCEixJujApc8",
        "colab_type": "text"
      },
      "source": [
        "# Deeper 3D Ken Burns\n",
        "A fork of the repo metioned below to obtain more \"exaggerated\" results as shown in this Twitter thread: https://twitter.com/alexeev_eu/status/1219655482501140480?s=20\n",
        "\n",
        "## 3D Ken Burns\n",
        "This is a reference implementation of 3D Ken Burns Effect from a Single Image [1] using PyTorch. Given a single input image, it animates this still image with a virtual camera scan and zoom subject to motion parallax. Should you be making use of our work, please cite our paper [1].\n",
        "\n",
        "<a href=\"https://arxiv.org/abs/1909.05483\" rel=\"Paper\"><img src=\"http://content.sniklaus.com/kenburns/paper.jpg\" alt=\"Paper\" width=\"100%\"></a>\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cJFpObViMXDH",
        "colab_type": "text"
      },
      "source": [
        "> Colab created by [Manuel Romero](https://twitter.com/mrm8488)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YXK9VNecNQEF",
        "colab_type": "text"
      },
      "source": [
        "### 1. Clone the project"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZbYRw_GlCL5w",
        "colab_type": "code",
        "outputId": "9fcd4daf-10c8-48a2-8abc-b7198ba1393b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        }
      },
      "source": [
        "!git clone https://github.com/mrm8488/3d-ken-burns.git"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into '3d-ken-burns'...\n",
            "remote: Enumerating objects: 33, done.\u001b[K\n",
            "remote: Counting objects:   3% (1/33)\u001b[K\rremote: Counting objects:   6% (2/33)\u001b[K\rremote: Counting objects:   9% (3/33)\u001b[K\rremote: Counting objects:  12% (4/33)\u001b[K\rremote: Counting objects:  15% (5/33)\u001b[K\rremote: Counting objects:  18% (6/33)\u001b[K\rremote: Counting objects:  21% (7/33)\u001b[K\rremote: Counting objects:  24% (8/33)\u001b[K\rremote: Counting objects:  27% (9/33)\u001b[K\rremote: Counting objects:  30% (10/33)\u001b[K\rremote: Counting objects:  33% (11/33)\u001b[K\rremote: Counting objects:  36% (12/33)\u001b[K\rremote: Counting objects:  39% (13/33)\u001b[K\rremote: Counting objects:  42% (14/33)\u001b[K\rremote: Counting objects:  45% (15/33)\u001b[K\rremote: Counting objects:  48% (16/33)\u001b[K\rremote: Counting objects:  51% (17/33)\u001b[K\rremote: Counting objects:  54% (18/33)\u001b[K\rremote: Counting objects:  57% (19/33)\u001b[K\rremote: Counting objects:  60% (20/33)\u001b[K\rremote: Counting objects:  63% (21/33)\u001b[K\rremote: Counting objects:  66% (22/33)\u001b[K\rremote: Counting objects:  69% (23/33)\u001b[K\rremote: Counting objects:  72% (24/33)\u001b[K\rremote: Counting objects:  75% (25/33)\u001b[K\rremote: Counting objects:  78% (26/33)\u001b[K\rremote: Counting objects:  81% (27/33)\u001b[K\rremote: Counting objects:  84% (28/33)\u001b[K\rremote: Counting objects:  87% (29/33)\u001b[K\rremote: Counting objects:  90% (30/33)\u001b[K\rremote: Counting objects:  93% (31/33)\u001b[K\rremote: Counting objects:  96% (32/33)\u001b[K\rremote: Counting objects: 100% (33/33)\u001b[K\rremote: Counting objects: 100% (33/33), done.\u001b[K\n",
            "remote: Compressing objects: 100% (33/33), done.\u001b[K\n",
            "remote: Total 69 (delta 17), reused 1 (delta 0), pack-reused 36\u001b[K\n",
            "Unpacking objects: 100% (69/69), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "06tpWil4A6Ck",
        "colab_type": "text"
      },
      "source": [
        "### 2. Download pretrained models"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jy-x7Cm7AymM",
        "colab_type": "code",
        "outputId": "5c5807fe-dafd-42fe-88e6-9ba41ff50e14",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 604
        }
      },
      "source": [
        "!wget -O ./3d-ken-burns/models/disparity-estimation.pytorch http://content.sniklaus.com/kenburns/network-disparity.pytorch\n",
        "!wget -O ./3d-ken-burns/models/disparity-refinement.pytorch http://content.sniklaus.com/kenburns/network-refinement.pytorch\n",
        "!wget -O ./3d-ken-burns/models/pointcloud-inpainting.pytorch http://content.sniklaus.com/kenburns/network-inpainting.pytorch"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "--2020-01-27 15:17:10--  http://content.sniklaus.com/kenburns/network-disparity.pytorch\n",
            "Resolving content.sniklaus.com (content.sniklaus.com)... 51.15.58.24\n",
            "Connecting to content.sniklaus.com (content.sniklaus.com)|51.15.58.24|:80... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 357438463 (341M) [application/octet-stream]\n",
            "Saving to: ‘./3d-ken-burns/models/disparity-estimation.pytorch’\n",
            "\n",
            "./3d-ken-burns/mode 100%[===================>] 340.88M   109MB/s    in 3.1s    \n",
            "\n",
            "2020-01-27 15:17:13 (109 MB/s) - ‘./3d-ken-burns/models/disparity-estimation.pytorch’ saved [357438463/357438463]\n",
            "\n",
            "--2020-01-27 15:17:15--  http://content.sniklaus.com/kenburns/network-refinement.pytorch\n",
            "Resolving content.sniklaus.com (content.sniklaus.com)... 51.15.58.24\n",
            "Connecting to content.sniklaus.com (content.sniklaus.com)|51.15.58.24|:80... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 2438928 (2.3M) [application/octet-stream]\n",
            "Saving to: ‘./3d-ken-burns/models/disparity-refinement.pytorch’\n",
            "\n",
            "./3d-ken-burns/mode 100%[===================>]   2.33M  12.2MB/s    in 0.2s    \n",
            "\n",
            "2020-01-27 15:17:15 (12.2 MB/s) - ‘./3d-ken-burns/models/disparity-refinement.pytorch’ saved [2438928/2438928]\n",
            "\n",
            "--2020-01-27 15:17:17--  http://content.sniklaus.com/kenburns/network-inpainting.pytorch\n",
            "Resolving content.sniklaus.com (content.sniklaus.com)... 51.15.58.24\n",
            "Connecting to content.sniklaus.com (content.sniklaus.com)|51.15.58.24|:80... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 33179067 (32M) [application/octet-stream]\n",
            "Saving to: ‘./3d-ken-burns/models/pointcloud-inpainting.pytorch’\n",
            "\n",
            "./3d-ken-burns/mode 100%[===================>]  31.64M  71.7MB/s    in 0.4s    \n",
            "\n",
            "2020-01-27 15:17:17 (71.7 MB/s) - ‘./3d-ken-burns/models/pointcloud-inpainting.pytorch’ saved [33179067/33179067]\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w5PBRLqUGYbk",
        "colab_type": "code",
        "outputId": "3e23d8b3-42cb-4068-dc6c-401c49ecbbc1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 284
        }
      },
      "source": [
        "import chainer\n",
        "chainer.print_runtime_info()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Platform: Linux-4.14.137+-x86_64-with-Ubuntu-18.04-bionic\n",
            "Chainer: 6.5.0\n",
            "ChainerX: Not Available\n",
            "NumPy: 1.17.5\n",
            "CuPy:\n",
            "  CuPy Version          : 6.5.0\n",
            "  CUDA Root             : /usr/local/cuda\n",
            "  CUDA Build Version    : 10010\n",
            "  CUDA Driver Version   : 10010\n",
            "  CUDA Runtime Version  : 10010\n",
            "  cuDNN Build Version   : 7603\n",
            "  cuDNN Version         : 7603\n",
            "  NCCL Build Version    : 2402\n",
            "  NCCL Runtime Version  : 2402\n",
            "iDeep: 2.0.0.post3\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EkrEO7vIIPym",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import os\n",
        "os.environ['CUDA_HOME'] = \"/usr/local/cuda\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vtd0th84B0Zg",
        "colab_type": "code",
        "outputId": "0be443be-65e6-453f-aac6-405ab61b0d6a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        }
      },
      "source": [
        "!pip install moviepy"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: moviepy in /usr/local/lib/python3.6/dist-packages (0.2.3.5)\n",
            "Requirement already satisfied: imageio<3.0,>=2.1.2 in /usr/local/lib/python3.6/dist-packages (from moviepy) (2.4.1)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from moviepy) (1.17.5)\n",
            "Requirement already satisfied: tqdm<5.0,>=4.11.2 in /usr/local/lib/python3.6/dist-packages (from moviepy) (4.28.1)\n",
            "Requirement already satisfied: decorator<5.0,>=4.0.2 in /usr/local/lib/python3.6/dist-packages (from moviepy) (4.4.1)\n",
            "Requirement already satisfied: pillow in /usr/local/lib/python3.6/dist-packages (from imageio<3.0,>=2.1.2->moviepy) (6.2.2)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xXFs3ii5Co82",
        "colab_type": "code",
        "outputId": "0f1286c0-694c-4d88-f119-4cb011ed3abf",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "cd /content/3d-ken-burns"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/content/3d-ken-burns\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2z5hGImGGOEj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!rm images/README.md\n",
        "!mkdir results"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ndl8FGKxM_hk",
        "colab_type": "text"
      },
      "source": [
        "### 3. Upload one picture your filesystem"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wgRSDEfRJ6ha",
        "colab_type": "code",
        "outputId": "5d050c63-c04a-4a7c-9a8e-be932fcd451d",
        "colab": {
          "resources": {
            "http://localhost:8080/nbextensions/google.colab/files.js": {
              "data": "[encoded data, 7324 characters]",
              "ok": true,
              "headers": [
                [
                  "content-type",
                  "application/javascript"
                ]
              ],
              "status": 200,
              "status_text": ""
            }
          },
          "base_uri": "https://localhost:8080/",
          "height": 75
        }
      },
      "source": [
        "from google.colab import files\n",
        "\n",
        "uploaded = files.upload()\n",
        "pic_name = list(uploaded.keys())[0]\n",
        "!mv ./$pic_name ./images/$pic_name"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "\n",
              "     <input type=\"file\" id=\"files-82db8875-730b-4e8b-a93c-e7ada6f052e9\" name=\"files[]\" multiple disabled />\n",
              "     <output id=\"result-82db8875-730b-4e8b-a93c-e7ada6f052e9\">\n",
              "      Upload widget is only available when the cell has been executed in the\n",
              "      current browser session. Please rerun this cell to enable.\n",
              "      </output>\n",
              "      <script src=\"/nbextensions/google.colab/files.js\"></script> "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Saving doublestrike.jpg to doublestrike.jpg\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "G9vz3wSqmSNz",
        "colab_type": "text"
      },
      "source": [
        "Params set as [jonathanfly](https://twitter.com/jonathanfly) proposed"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VfONcdQblmny",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "dblShift = 10\n",
        "dblZoom = 10000000000000000000000000000000000000000000000000000000\n",
        "#dblSteps = \"0.0 8.0 400\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Daw9Ptv5P_EG",
        "colab_type": "text"
      },
      "source": [
        "### 4. Apply 3D Ken Burns Effect\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tNsCub5OdVTl",
        "colab_type": "code",
        "outputId": "adc56f2e-b8e3-485b-9efb-bb978d28ed9d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        }
      },
      "source": [
        "!python autozoom.py \\\n",
        "--dblShift $dblShift \\\n",
        "--dblZoom $dblZoom \\\n",
        "--dblSteps \"0.0 8.0 400\" \\\n",
        "--in /content/3d-ken-burns/images/$pic_name \\\n",
        "--out ./results/autozoom.mp4"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.0\n",
            "[MoviePy] >>>> Building video ./results/autozoom.mp4\n",
            "[MoviePy] Writing video ./results/autozoom.mp4\n",
            "100% 799/799 [00:47<00:00, 21.08it/s]\n",
            "[MoviePy] Done.\n",
            "[MoviePy] >>>> Video ready: ./results/autozoom.mp4 \n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vkoT7P4biRJe",
        "colab_type": "text"
      },
      "source": [
        "### 5. Play the generated video"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zL107cWdhk0t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from IPython.display import HTML\n",
        "from base64 import b64encode\n",
        "\n",
        "mp4 = open(\"./results/autozoom.mp4\",'rb').read()\n",
        "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
        "HTML('<video width=500 controls loop> <source src=\"%s\" type=\"video/mp4\"></video>' % data_url)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wOyjsg55NeI0",
        "colab_type": "text"
      },
      "source": [
        "### 6. Download the generated video\n",
        "(It can take a while)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7vYU0HNyP2Yu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "files.download(\"./results/autozoom.mp4\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "72XSBhKkPKMA",
        "colab_type": "text"
      },
      "source": [
        " Did you enjoy? Buy me a coffe :)\n",
        " \n",
        "[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y3VYYE)"
      ]
    }
  ]
}