Last active
December 5, 2019 22:45
-
-
Save btahir/b66c554d32b965bccd1b62eb8372c41f to your computer and use it in GitHub Desktop.
deep-cropper.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "deep-cropper.ipynb", | |
"version": "0.3.2", | |
"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/gist/btahir/b66c554d32b965bccd1b62eb8372c41f/deep-cropper.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "yJz8-yZRTzlW", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Deep Cropper" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "mkV2KcAN0Gxs", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Notebook inspired by this [tweet](https://twitter.com/chriseberly/status/1143215509640548360).\n", | |
"\n", | |
"Let's do some photoshopping using Deep Learning! \n", | |
"\n", | |
"In this notebook we will crop an object (person, animal etc.) out of an image and place it on another. \n", | |
"\n", | |
"Just for fun, let's put a Polar Bear next to Jeff Goldblum." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "xhoR_BL8LzUp", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# import model\n", | |
"import torch\n", | |
"from IPython.display import clear_output\n", | |
"\n", | |
"model = torch.hub.load('pytorch/vision', 'deeplabv3_resnet101', pretrained=True)\n", | |
"model.eval()\n", | |
"\n", | |
"clear_output()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "18N3BZflvH__", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# import packages\n", | |
"import urllib\n", | |
"import cv2\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"from ipywidgets import interactive\n", | |
"from PIL import Image\n", | |
"from torchvision import transforms\n", | |
"%matplotlib inline" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Rh20Wd-7Sdef", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# make working directory\n", | |
"mkdir work" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "WlzRV5E30pAJ", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Let's download the images for our task.\n", | |
"\n", | |
"Foreground image is the one you want to crop an object out of and background image is the image you want to overlay/paste it on." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UyF8PjMyPuUI", | |
"colab_type": "code", | |
"outputId": "f9e686b3-c870-4d30-d586-21a3e0c85c64", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 472 | |
} | |
}, | |
"source": [ | |
"#@title Filename Paths For Foreground and Background Images:\n", | |
"FOREGROUND = 'https://live.staticflickr.com/1399/1118093174_8b723e1ee5_o.jpg' #@param {type:\"string\"}\n", | |
"BACKGROUND = 'https://live.staticflickr.com/7860/46618564664_be235e82e8_b.jpg' #@param {type:\"string\"}\n", | |
"\n", | |
"# get background\n", | |
"url, filename = (BACKGROUND, \"work/background.jpg\")\n", | |
"\n", | |
"try: urllib.URLopener().retrieve(url, filename)\n", | |
"except: urllib.request.urlretrieve(url, filename)\n", | |
" \n", | |
"background = Image.open(filename).convert(\"RGB\")\n", | |
"background.save('work/background.jpg')\n", | |
"\n", | |
"# get foreground\n", | |
"url, filename = (FOREGROUND, \"work/foreground.jpg\")\n", | |
"\n", | |
"try: urllib.URLopener().retrieve(url, filename)\n", | |
"except: urllib.request.urlretrieve(url, filename)\n", | |
" \n", | |
"foreground = Image.open(filename).convert(\"RGB\")\n", | |
"foreground.save('work/foreground.jpg')\n", | |
"\n", | |
"# plot\n", | |
"fig = plt.figure(figsize=(20, 20))\n", | |
"\n", | |
"ax1 = fig.add_subplot(121)\n", | |
"ax1.imshow(background)\n", | |
"ax2 = fig.add_subplot(122)\n", | |
"ax2.imshow(foreground)\n", | |
"\n", | |
"ax1.title.set_text('Background Image')\n", | |
"ax2.title.set_text('Foreground Image')\n" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment