Last active
February 9, 2020 22:40
-
-
Save ivanlen/47234ff75e14e2794e25fe7ff2fed4a5 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from PIL import Image\n", | |
"import os\n", | |
"import itertools" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from PIL.ImageOps import flip, mirror\n", | |
"import PIL" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"343" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"folder_to_scan = './datasets/curated/'\n", | |
"files = os.listdir(folder_to_scan)\n", | |
"len(files)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"valid_file_types = ['.jpg']\n", | |
"valid_files = []\n", | |
"for file in files:\n", | |
" f, fe = os.path.splitext(file)\n", | |
" if fe in valid_file_types:\n", | |
" valid_files.append(file)\n", | |
"full_path_valid_files = [folder_to_scan+file for file in valid_files]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def generate_1d_limits(wind, limit, thresh):\n", | |
" x_left = []\n", | |
" x_right = []\n", | |
" if limit >= wind:\n", | |
" x_lim_reached = False\n", | |
" i = 0\n", | |
" while not x_lim_reached:\n", | |
" x_l = i * wind\n", | |
" x_r = (i + 1) * wind\n", | |
"\n", | |
" if x_r <= limit:\n", | |
" x_right.append(x_r)\n", | |
" x_left.append(x_l)\n", | |
" else:\n", | |
" x_lim_reached = True\n", | |
" # some extra padding\n", | |
" if (x_r - limit) / wind < thresh:\n", | |
" x_r = limit\n", | |
" x_l = limit - wind\n", | |
" x_right.append(x_r)\n", | |
" x_left.append(x_l)\n", | |
" i += 1\n", | |
" return (x_left, x_right)\n", | |
"\n", | |
"\n", | |
"def generate_cropping_boxes_from_limits(x_left, x_rigth, x_bottom, x_top):\n", | |
" croping_boxes = []\n", | |
" x_lims = [(x_l, x_r) for x_l, x_r in zip(x_left, x_rigth)]\n", | |
" y_lims = [(x_l, x_r) for x_l, x_r in zip(x_bottom, x_top)]\n", | |
" bounding_boxes = list(itertools.product(x_lims, y_lims))\n", | |
" for i in range(len(bounding_boxes)):\n", | |
" ((x1, x2), (y1, y2)) = bounding_boxes[i]\n", | |
" croping_boxes.append((x1, y1, x2, y2))\n", | |
" return croping_boxes\n", | |
"\n", | |
"\n", | |
"def generate_cropping_boxes(image, cropping_window, tresh):\n", | |
" image_width, image_height = image.size\n", | |
" x_left, x_rigth = generate_1d_limits(cropping_window, image_width, tresh)\n", | |
" x_bottom, x_top = generate_1d_limits(cropping_window, image_height, tresh)\n", | |
" croping_boxes = generate_cropping_boxes_from_limits(x_left, x_rigth, x_bottom, x_top)\n", | |
" return croping_boxes\n", | |
"\n", | |
"\n", | |
"def image_square_resize(im_input, new_size):\n", | |
" im = im_input.copy()\n", | |
" im = im.resize((new_size, new_size), PIL.Image.ANTIALIAS) \n", | |
" return im\n", | |
"\n", | |
"\n", | |
"def image_rotator(im_input, angle):\n", | |
" if angle==90:\n", | |
" return im_input.transpose(Image.ROTATE_90) \n", | |
" elif angle == 180:\n", | |
" return im_input.transpose(Image.ROTATE_180) \n", | |
" elif angle == 270:\n", | |
" return im_input.transpose(Image.ROTATE_270) \n", | |
" else:\n", | |
" raise ValueError('angle not supported') \n", | |
" \n", | |
" \n", | |
"def image_augmentator(im_input, return_orig = True):\n", | |
" im_aug = []\n", | |
" if return_orig:\n", | |
" im_aug.append(im_input.copy())\n", | |
" # 1.flip\n", | |
" im_aug.append(flip(im_input.copy()))\n", | |
" # 2. rot 180\n", | |
" im_aug.append(image_rotator(im_input.copy(), 180))\n", | |
" # 3. flip(rot_90)\n", | |
" im_aug.append(flip(image_rotator(im_input.copy(), 90)))\n", | |
" # 4. flip(rot_270)\n", | |
" im_aug.append(flip(image_rotator(im_input.copy(), 270)))\n", | |
" return im_aug " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# class SizeCounter:\n", | |
"# def __init__(self):\n", | |
"# self.counter = {}\n", | |
" \n", | |
"# def add(self, size):\n", | |
"# try:\n", | |
"# self.counter[size]\n", | |
"# self.counter[size] +=1\n", | |
"# except KeyError:\n", | |
"# self.counter[size] = 1\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# sizes = SizeCounter()\n", | |
"# for file in full_path_valid_files:\n", | |
"# im = Image.open(file)\n", | |
"# sizes.add(size=im.size)\n", | |
" \n", | |
"# # sizes.counter" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### No augmentation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cropping_window = 512\n", | |
"saving_folder_name = './datasets/cropped_files_no_aug/'\n", | |
"padding_tresh = 0.25\n", | |
"resize = False\n", | |
"image_output_size = 512\n", | |
"\n", | |
"os.makedirs(saving_folder_name, exist_ok=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4120\n" | |
] | |
} | |
], | |
"source": [ | |
"for file in full_path_valid_files:\n", | |
" im = Image.open(file)\n", | |
" croping_boxes = generate_cropping_boxes(im, cropping_window, padding_tresh) \n", | |
" file_name = os.path.basename(file)\n", | |
" base_name, file_ext = os.path.splitext(file_name)\n", | |
" for i,b in enumerate(croping_boxes):\n", | |
" imc = im.crop(b) #left bottom right upper\n", | |
" if resize: \n", | |
" imc = image_square_resize(imc,image_output_size )\n", | |
" f_name = saving_folder_name + base_name +'_{}_'.format(i) + file_ext\n", | |
" imc.save(f_name)\n", | |
"print(len(os.listdir(saving_folder_name)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Aumentation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cropping_window = 512\n", | |
"saving_folder_name = './datasets/cropped_files_aug/'\n", | |
"padding_tresh = 0.25\n", | |
"resize = False\n", | |
"image_output_size = 512\n", | |
"\n", | |
"os.makedirs(saving_folder_name, exist_ok=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"20600\n" | |
] | |
} | |
], | |
"source": [ | |
"for file in full_path_valid_files: \n", | |
" im = Image.open(file)\n", | |
" for ia, im in enumerate(image_augmentator(im)):\n", | |
" croping_boxes = generate_cropping_boxes(im, cropping_window, padding_tresh) \n", | |
" file_name = os.path.basename(file)\n", | |
" base_name, file_ext = os.path.splitext(file_name)\n", | |
" for i,b in enumerate(croping_boxes):\n", | |
" imc = im.crop(b) #left bottom right upper\n", | |
" if resize: \n", | |
" imc = image_square_resize(imc,image_output_size )\n", | |
" f_name = saving_folder_name + base_name +'_{}_{}_'.format(ia,i) + file_ext\n", | |
" imc.save(f_name)\n", | |
" \n", | |
"print(len(os.listdir(saving_folder_name)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "testing-kernel", | |
"language": "python", | |
"name": "testing-kernel" | |
}, | |
"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.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment