Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert Carremans authored and Bert Carremans committed Nov 15, 2017
0 parents commit 1cbab0d
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
flickr/img/
flickr/.ipynb_checkpoints/
model/.ipynb_checkpoints/
data_augmentation/.ipynb_checkpoints/
29 changes: 29 additions & 0 deletions LICENSE.md
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2017, Bert Carremans
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions README.md
@@ -0,0 +1,2 @@
# vlindervinder
Tool built with Python and convolutional neural networks to classifiy butterfly images
62 changes: 62 additions & 0 deletions data_augmentation/data_augmentation_flickr_images.ipynb
@@ -0,0 +1,62 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Copyright 2014-2017 Bert Carremans\n",
"# Author: Bert Carremans <bertcarremans.be>\n",
"#\n",
"# License: BSD 3 clause\n",
"from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\n",
"\n",
"train_datagen = ImageDataGenerator(\n",
" rotation_range = 40, \n",
" width_shift_range = 0.2, \n",
" height_shift_range = 0.2, \n",
" rescale = 1./255, \n",
" shear_range = 0.2, \n",
" zoom_range = 0.2, \n",
" horizontal_flip = True)\n",
"\n",
"validation_datagen = ImageDataGenerator(rescale=1./255)\n",
"\n",
"train_generator = train_datagen.flow_from_directory(\n",
" 'data/train',\n",
" target_size\n",
" batch_size=32,\n",
" class_mode='binary')\n",
"\n",
"validation_generator = validation_datagen.flow_from_directory(\n",
" 'data/validation',\n",
" batch_size=32,\n",
" class_mode='binary')\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
4 changes: 4 additions & 0 deletions flickr/config.py
@@ -0,0 +1,4 @@
API_KEY = 'a269db2de679d8ff1186c3e385a9a30a'
API_SECRET = '68d696140c83cfa8'

IMG_FOLDER = '/Users/bertcarremans/Data Science/Projecten/Vlindervinder/flickr/img/'
137 changes: 137 additions & 0 deletions flickr/download_flickr_photos.ipynb
@@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Copyright 2014-2017 Bert Carremans\n",
"# Author: Bert Carremans <bertcarremans.be>\n",
"#\n",
"# License: BSD 3 clause\n",
"\n",
"\n",
"from flickrapi import FlickrAPI # https://pypi.python.org/pypi/flickrapi\n",
"import urllib\n",
"import os\n",
"import config\n",
"from random import randint\n",
"import time\n",
"\n",
"def download_flickr_photos(keywords, size='original', max_nb_img=-1):\n",
" \"\"\"\n",
" Downloads images based on keyword search on the Flickr website\n",
" \n",
" Parameters\n",
" ----------\n",
" keywords : string, list of strings\n",
" Keyword to search for or a list of keywords should be given.\n",
" size : one of the following strings 'thumbnail', 'square', 'medium', default: 'original'.\n",
" Size of the image to download. In this function we only provide\n",
" four options. More options are explained at \n",
" http://librdf.org/flickcurl/api/flickcurl-searching-search-extras.html\n",
" max_nb_img : int, default: -1\n",
" Maximum number of images per keyword to download. If given a value of -1, all images\n",
" will be downloaded\n",
" \n",
" Returns\n",
" ------\n",
" Images found based on the keyword are saved in a separate subfolder.\n",
" \n",
" Notes\n",
" -----\n",
" This function uses the Python package flickrapi and its walk method. \n",
" FlickrAPI.walk has same parameters as FlickrAPI.search\n",
" http://www.flickr.com/services/api/flickr.photos.search.html\n",
" \n",
" To use the Flickr API a set of API keys needs to be created on \n",
" https://www.flickr.com/services/api/misc.api_keys.html\n",
" \"\"\"\n",
" if not (isinstance(keywords, str) or isinstance(keywords, list)):\n",
" raise AttributeError('keywords must be a string or a list of strings')\n",
" \n",
" if not (size in ['thumbnail', 'square', 'medium', 'original']):\n",
" raise AttributeError('size must be \"thumbnail\", \"square\", \"medium\" or \"original\"')\n",
" \n",
" if not (max_nb_img == -1 or (max_nb_img > 0 and isinstance(max_nb_img, int))):\n",
" raise AttributeError('max_nb_img must be an integer greater than zero or equal to -1')\n",
" \n",
" flickr = FlickrAPI(config.API_KEY, config.API_SECRET)\n",
" \n",
" if isinstance(keywords, str):\n",
" keywords_list = []\n",
" keywords_list.append(keywords)\n",
" else:\n",
" keywords_list = keywords\n",
" \n",
" if size == 'thumbnail':\n",
" size_url = 'url_t'\n",
" elif size == 'square':\n",
" size_url = 'url_q'\n",
" elif size == 'medium':\n",
" size_url = 'url_c'\n",
" elif size == 'original':\n",
" size_url = 'url_o'\n",
" \n",
" for keyword in keywords_list:\n",
" count = 0\n",
" \n",
" #print('Downloading images for', keyword)\n",
"\n",
" results_folder = config.IMG_FOLDER + keyword.replace(\" \", \"_\") + \"/\"\n",
" if not os.path.exists(results_folder):\n",
" os.makedirs(results_folder)\n",
"\n",
" photos = flickr.walk(\n",
" text=keyword,\n",
" extras=size_url,\n",
" license='1,2,4,5',\n",
" per_page=50)\n",
" \n",
" urls = []\n",
" for photo in photos:\n",
" t = randint(1, 3)\n",
" time.sleep(t)\n",
" count += 1\n",
" if max_nb_img != -1:\n",
" if count > max_nb_img:\n",
" print('Reached maximum number of images to download')\n",
" break\n",
" try:\n",
" url=photo.get(size_url)\n",
" urls.append(url)\n",
" \n",
" urllib.request.urlretrieve(url, results_folder + str(count) +\".jpg\")\n",
" print('Downloading image #' + str(count) + ' from url ' + url)\n",
" except Exception as e:\n",
" print(e, 'Download failure')\n",
" \n",
" print(\"Total images downloaded:\", str(count - 1)) "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 1cbab0d

Please sign in to comment.