Skip to content

Instantly share code, notes, and snippets.

@hotstaff
Created January 9, 2020 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hotstaff/335dc9027cda0b28d3f9ff047b561a3c to your computer and use it in GitHub Desktop.
Save hotstaff/335dc9027cda0b28d3f9ff047b561a3c to your computer and use it in GitHub Desktop.
Waifu2x-chainer-colab
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Waifu2x-chainer-colab",
"provenance": [],
"collapsed_sections": [],
"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/gist/hotstaff/335dc9027cda0b28d3f9ff047b561a3c/waifu2x-chainer-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "YEM7aWZDhtot",
"colab_type": "code",
"colab": {}
},
"source": [
"# This notebook based \n",
"# https://github.com/hirios/Waifu2x-Colab/blob/master/2xcolab.ipynb.\n",
"# Thanks share usuful script.\n",
"# Write by Hideto Manjo github/hotstaff\n",
"import os\n",
"from google.colab import files\n",
"from google.colab import drive\n",
"!git clone https://github.com/tsurumeso/waifu2x-chainer\n",
"\n",
"try:\n",
" drive.mount('/content/drive/')\n",
"except:\n",
" exit()\n",
" \n",
"FFMPEG_LOG_OPTION = \"-loglevel error\"\n",
"\n",
"def setting():\n",
" %cp '/content/drive/My Drive/'{INPUT_MOVIE} /content/\n",
" print(f\"Input file name is: {INPUT_MOVIE}\")\n",
" print(f\"Output file name is {OUTPUT_MOVIE}\")\n",
" print(f\"Scale: {SCALE}, Noise Reduction: {NOISE}\")\n",
" if DEBUG:\n",
" FFMPEG_LOG_OPTION = \"\"\n",
"\n",
"def get_fps():\n",
" global FPS\n",
" fps = str(os.popen(f'ffmpeg -i \"/content/{INPUT_MOVIE}\" 2> fps.txt ; egrep -i \"fps\" fps.txt').read())\n",
" FPS = fps.split(\",\")[4].split()[0]\n",
" !rm \"fps.txt\"\n",
" \n",
" if FPS is None:\n",
" print(\"Error exit, ffmpeg can't get fps.\")\n",
" exit()\n",
"\n",
"\n",
"def get_frames():\n",
" %rm /content/frames -rf\n",
" %mkdir -p /content/frames\n",
"\n",
" print(\"Extracting frames from your video ...\")\n",
" !ffmpeg -y -i /content/{INPUT_MOVIE} /content/frames/%d.png {FFMPEG_LOG_OPTION}\n",
"\n",
"\n",
"def upscale_frames():\n",
" print(\"\\nUpscale each image ...\")\n",
" %cd /content/waifu2x-chainer\n",
" %rm /content/upframes -rf\n",
" %mkdir -p /content/upframes\n",
"\n",
" debug_option = \" > /dev/null 2>&1\"\n",
" if DEBUG:\n",
" debug_option = \"\"\n",
"\n",
" tta_option = \"\"\n",
" if TTA:\n",
" tta_option = \"--tta\"\n",
"\n",
" !python waifu2x.py -m noise_scale -n {NOISE} -s {SCALE}\\\n",
" -i /content/frames\\\n",
" --output /content/upframes\\\n",
" --arch {ARCH} -g 0 {tta_option} {debug_option}\n",
"\n",
"\n",
"def generating_video2x():\n",
" %cd /content/upframes\n",
" print(\"\\nGenerating new video file ...\")\n",
"\n",
" if SOUND:\n",
" !ffmpeg -y -i /content/{INPUT_MOVIE} -ab 192k /content/audiotrack.mp3\\\n",
" {FFMPEG_LOG_OPTION}\n",
" !ffmpeg -y -f image2 -r {FPS} -i %d.png\\\n",
" -i /content/audiotrack.mp3 -acodec copy {OUTPUT_MOVIE}\\\n",
" {FFMPEG_LOG_OPTION}\n",
" else:\n",
" !ffmpeg -y -f image2 -r {FPS} -i %d.png {OUTPUT_MOVIE}\\\n",
" {FFMPEG_LOG_OPTION}\n",
"\n",
"\n",
"def send_for_drive():\n",
" print(\"\\nTransferring to drive ...\")\n",
" %cp /content/upframes/{OUTPUT_MOVIE} '/content/drive/My Drive'"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-100c1txhznF",
"colab_type": "code",
"cellView": "both",
"colab": {}
},
"source": [
"#@title Waifu2x-chainer Upscale/Denoise Settings\n",
"\n",
"#@markdown **Note this notebook required GPU runtime, so please activate GPU.**\n",
"\n",
"#@markdown ---\n",
"#@markdown ### Requirements\n",
"#@markdown Please input original movie filename in your google drive \"~/My Drive/\".\n",
"INPUT_MOVIE = \"kakei.mp4\" #@param {type:\"string\"}\n",
"SCALE = 1 #@param {type:\"slider\", min:1, max:2, step:1}\n",
"NOISE = 3 #@param {type:\"slider\", min:0, max:3, step:1}\n",
"\n",
"#@markdown ---\n",
"#@markdown ### Optionals\n",
"PREFIX = 'waifu_' #@param [\"waifu_\"] {allow-input: true}\n",
"SOUND = True #@param {type:\"boolean\"}\n",
"ARCH = \"VGG7\" #@param [\"VGG7\", \"UpConv7\", \"ResNet10\", \"UpResNet10\"]\n",
"TTA = True #@param {type: \"boolean\"}\n",
"DEBUG = False #@param {type:\"boolean\"}\n",
"\n",
"OUTPUT_MOVIE = f\"{PREFIX}{INPUT_MOVIE[0:-4]}.mp4\"\n",
"FPS = None\n",
"\n",
"if __name__ == \"__main__\":\n",
" setting()\n",
" get_fps()\n",
" get_frames()\n",
" upscale_frames()\n",
" generating_video2x()\n",
" send_for_drive()"
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment