# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="8nKdL3Crw81q"
# **Remember to change runtime type to GPU.**
#
# Runtime > Change runtime type > GPU

# + [markdown] id="IYCkiK58apNm"
# **Clone git repo**

# + colab={"base_uri": "https://localhost:8080/"} id="RGRkm-Hkn6Tl" executionInfo={"status": "ok", "timestamp": 1620635105101, "user_tz": -480, "elapsed": 8946, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}} outputId="f1b017be-57c8-47ad-beb2-b670a991581c"
# !git clone https://github.com/wein98/webapp-first-order-motion.git

# + colab={"base_uri": "https://localhost:8080/"} id="bSB2YeZEoCiJ" executionInfo={"status": "ok", "timestamp": 1620635107572, "user_tz": -480, "elapsed": 802, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}} outputId="7f4d3467-e500-43fc-acf3-f2dd0a2283d1"
# cd webapp-first-order-motion/first-order-motion

# + [markdown] id="R6l5IVMrxbDt"
# **Image and video input here.** 
#
# Upload the your input image and video under /content/webapp-first-order-motion/first-order-motion.

# + [markdown] id="WXNmh9efHHTx"
# **Download first order motion pretrained model.**

# + colab={"base_uri": "https://localhost:8080/"} id="EX7GytT7HDpG" executionInfo={"status": "ok", "timestamp": 1620635134538, "user_tz": -480, "elapsed": 13105, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}} outputId="1f2ba425-a9a7-4a14-b37d-6e8774587850"
# !gdown https://drive.google.com/uc?id=19eg-JkeauMAOlIBJPdIrAzgocAjRWp7T

# + [markdown] id="llamfcYueQow"
# Function to display output in Google Colab

# + id="R2k6gBtMbByc" executionInfo={"status": "ok", "timestamp": 1620636611937, "user_tz": -480, "elapsed": 809, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}}
import imageio
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from skimage.transform import resize
from IPython.display import HTML
import warnings
warnings.filterwarnings("ignore")

def display(image_path, video_path, generated_vid_path):
  source_image = imageio.imread(image_path)
  reader = imageio.get_reader(video_path)  
  generateReader = imageio.get_reader(generated_vid_path) 

  source = resize(source_image, (256, 256))[..., :3]

  fps = reader.get_meta_data()['fps']
  driving_video = []
  try:
      for im in reader:
          driving_video.append(im)
  except RuntimeError:
      pass
  reader.close()

  driving = [resize(frame, (256, 256))[..., :3] for frame in driving_video]

  fps = generateReader.get_meta_data()['fps']
  generated_video = []
  try:
      for im in generateReader:
          generated_video.append(im)
  except RuntimeError:
      pass
  generateReader.close()

  generated = [resize(frame, (256, 256))[..., :3] for frame in generated_video]
  
  fig = plt.figure(figsize=(8 + 4 * (generated is not None), 6))

  ims = []
  for i in range(len(driving)):
      cols = [source]
      cols.append(driving[i])
      if generated is not None:
          cols.append(generated[i])
      im = plt.imshow(np.concatenate(cols, axis=1), animated=True)
      plt.axis('off')
      ims.append([im])

  ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=1000)
  plt.close()
  return ani


# + [markdown] id="e-xoIIeKaBIF"
# ## Run the demo.py with our provided inputs
#
# change the `src_img` and `src_vid` arguments respectively to your uploaded filename to run your own input.

# + colab={"base_uri": "https://localhost:8080/"} id="dRGwEkoKo8Ti" executionInfo={"status": "ok", "timestamp": 1620634837817, "user_tz": -480, "elapsed": 97937, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}} outputId="7f22082f-d048-4695-a01c-92c31574ed9f"
# !python demo.py

# + colab={"base_uri": "https://localhost:8080/", "height": 452} id="IyRUdGWKdu8K" executionInfo={"status": "ok", "timestamp": 1620636647561, "user_tz": -480, "elapsed": 33191, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}} outputId="69cce4fb-dda5-44eb-f28e-a40628f544a0"
HTML(display('stage1_image.jpg', "stage1_video.mp4", "generated.mp4").to_html5_video())

# + id="udsezZ51pCrv" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1620635201528, "user_tz": -480, "elapsed": 61055, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "03685829748003472020"}} outputId="908a6254-c136-452c-9412-3c988e762060"
# !python demo.py --src_img obama.jpg --src_vid musk.mp4

StarcoderdataJupyterScriptsDedupFiltered