{ "cells": [ { "cell_type": "markdown", "metadata": { "hide_input": false }, "source": [ "# Creating your own dataset from Google Images\n", "\n", "*by: Francisco Ingham and Jeremy Howard. Inspired by [Adrian Rosebrock](https://www.pyimagesearch.com/2017/12/04/how-to-create-a-deep-learning-dataset-using-google-images/)*" ] }, { "cell_type": "markdown", "metadata": { "hide_input": true }, "source": [ "In this tutorial we will see how to easily create an image dataset through Google Images. **Note**: You will have to repeat these steps for any new category you want to Google (e.g once for dogs and once for cats)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get a list of URLs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Search and scroll" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Go to [Google Images](http://images.google.com) and search for the images you are interested in. The more specific you are in your Google Search, the better the results and the less manual pruning you will have to do.\n", "\n", "Scroll down until you've seen all the images you want to download, or until you see a button that says 'Show more results'. All the images you scrolled past are now available to download. To get more, click on the button, and continue scrolling. The maximum number of images Google Images shows is 700.\n", "\n", "It is a good idea to put things you want to exclude into the search query, for instance if you are searching for the Eurasian wolf, \"canis lupus lupus\", it might be a good idea to exclude other variants:\n", "\n", " \"canis lupus lupus\" -dog -arctos -familiaris -baileyi -occidentalis\n", "\n", "You can also limit your results to show only photos by clicking on Tools and selecting Photos from the Type dropdown." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download into file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you must run some Javascript code in your browser which will save the URLs of all the images you want for you dataset.\n", "\n", "Press CtrlShiftJ in Windows/Linux and CmdOptJ in Mac, and a small window the javascript 'Console' will appear. That is where you will paste the JavaScript commands.\n", "\n", "You will need to get the urls of each of the images. You can do this by running the following commands:\n", "\n", "```javascript\n", "urls = Array.from(document.querySelectorAll('.rg_di .rg_meta')).map(el=>JSON.parse(el.textContent).ou);\n", "window.open('data:text/csv;charset=utf-8,' + escape(urls.join('\\n')));\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create directory and upload urls file into your server" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [], "source": [ "from fastai import *\n", "from fastai.vision import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose an appropriate name for your labeled images. You can run these steps multiple times to grab different labels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "folder = 'black'\n", "file = 'urls_black.txt'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "folder = 'teddys'\n", "file = 'urls_teddys.txt'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "folder = 'grizzly'\n", "file = 'urls_grizzly.txt'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will need to run this line once per each category." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = Path('data/bears')\n", "dest = path/folder\n", "dest.mkdir(parents=True, exist_ok=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, upload your urls file. You just need to press 'Upload' in your working directory and select your file, then click 'Upload' for each of the displayed files.\n", "\n", "![uploaded file](images/download_images/upload.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you will need to download you images from their respective urls.\n", "\n", "fast.ai has a function that allows you to do just that. You just have to specify the urls filename and the destination folder and this function will download and save all images that can be opened. If they have some problem in being opened, they will not be saved.\n", "\n", "Let's download our images! Notice you can choose a maximum number of images to be downloaded. In this case we will not download all the urls.\n", "\n", "You will need to run this line once for every category." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "classes = ['teddys','grizzly','black']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "download_images(path/file, dest, max_pics=200)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If you have problems download, try with `max_workers=0` to see exceptions:\n", "# download_images(path/file, dest, max_pics=20, max_workers=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can remove any images that can't be opened:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "teddys\n" ] }, { "data": { "text/html": [ "\n", "