Skip to content

Commit 767e5cd

Browse files
committed
Added code to generate new images and other essential functions.
1 parent a1c48da commit 767e5cd

File tree

17 files changed

+601
-277
lines changed

17 files changed

+601
-277
lines changed

.circleci/config.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,7 @@ venv.bak/
112112
# mypy
113113
.mypy_cache/
114114
.dmypy.json
115-
dmypy.json
115+
dmypy.json
116+
117+
.idea/
118+
*_data/

.gitlab-ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test:
2+
script:
3+
4+
- apt-get install -y python-dev python-pip
5+
- pip install -r requirements.txt
6+

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "python-utils"]
2+
path = python-utils
3+
url = https://github.com/dyt811/python-utils
4+
[submodule "PythonUtils"]
5+
path = PythonUtils
6+
url = https://github.com/dyt811/PythonUtils

CNN.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
classes = ['marker', 'no-marker']
55
num_classes = len(classes)
66

7-
train_path = '/training_data/'
7+
train_path = 'merged_data/'
88

99
# validation split
1010
validation_size = 0.2
1111

1212
# batch size
1313
batch_size = 16
1414

15-
#data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)
15+
data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)
1616

1717

1818
# Declare the input images and the labels.

ImageCropper/extract.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
from PIL import Image
22
import random
3+
import os
4+
from PythonUtils.file import unique_name
5+
from PythonUtils.folder import recursive_list, get_abspath
36

47
"""
58
This class explores the images and crop out as many smaller conforming image section as possible.
69
"""
710

811
def area(image, x, y, width, height):
12+
"""
13+
Crop and image using the given coordinate based on the width and height given.
14+
:param image:
15+
:param x:
16+
:param y:
17+
:param width:
18+
:param height:
19+
:return:
20+
"""
921
img = Image.open(image)
1022
area = (x, y, x+width, y+height)
1123
cropped_img = img.crop(area)
1224
#cropped_img.show()
1325
return cropped_img
1426

27+
1528
def randomly(image, width, height):
29+
"""
30+
Randomly crop areas of the image that is equivalent to the given width and height.
31+
:param image:
32+
:param width:
33+
:param height:
34+
:return:
35+
"""
1636
img = Image.open(image)
1737
img_width, img_height = img.size
1838

@@ -25,5 +45,72 @@ def randomly(image, width, height):
2545

2646
return area(image, random_crop_x, random_crop_y, width, height)
2747

48+
def crop_folder(image_folder, output_folder, width, height):
49+
"""
50+
crop the background images and generate the cropped version of them that are only 500x500
51+
:param image_folder: folder contain downloads.
52+
:param width: width of the area will be cropped out.
53+
:param height: height of the area will be cropped out.
54+
:return:
55+
"""
56+
# Change into the directory
57+
# List all the relevant folders.
58+
files = recursive_list(image_folder)
59+
60+
# Make DIR if it does not already exist.
61+
if not os.path.exists(output_folder):
62+
os.makedirs(output_folder)
63+
64+
# For all the files, try to convert and export to that address
65+
for file in files:
66+
try:
67+
image_path = os.path.join(image_folder, file)
68+
image = randomly(image_path, width, height)
69+
except OSError:
70+
print("Found a bad file. Ignoring: " + file)
71+
continue
72+
73+
file_name = file.replace(" ", "_")
74+
file_name = os.path.basename(file_name)
75+
bg_cropped_path = os.path.join(output_folder, file_name)
76+
77+
if image is None:
78+
continue
79+
else:
80+
try:
81+
image.save(bg_cropped_path, "PNG")
82+
print("Saved ", bg_cropped_path)
83+
except OSError:
84+
print("Found a bad file. Ignoring: " + file + " from " + image_path)
85+
continue
86+
87+
def crop_folder_bg(image_folder, width, height):
88+
"""
89+
crop the background images and generate the cropped version of them that are only 500x500
90+
:param image_folder: folder contain downloads.
91+
:param width: width of the area will be cropped out.
92+
:param height: height of the area will be cropped out.
93+
:return:
94+
"""
95+
# Change into the directory
96+
97+
# Ge the root of the folder contain GoogleDownloads
98+
image_root = get_abspath(image_folder, 1)
99+
100+
# Generate the temp name that will be used to store the crop results.
101+
crop_folder_name = unique_name() + "_" + str(width) + "x" + str(height) + "crop"
102+
103+
# Make and crop path.
104+
output_folder = os.path.join(image_root, "cropped", crop_folder_name)
105+
106+
# Make DIR if it does not already exist.
107+
if not os.path.exists(output_folder):
108+
os.makedirs(output_folder)
109+
110+
crop_folder(image_folder, output_folder, width, height)
111+
112+
return output_folder
113+
28114
if __name__ == "__main__":
29-
randomly("../a.jpg", 400, 400)
115+
#randomly("../a.jpg", 400, 400)
116+
crop_folder_bg(r"E:\Gitlab\MarkerTrainer\bg_data\background\\", 400, 400)

PythonUtils

Submodule PythonUtils added at df70cdd

bg_data/__init__.py

Whitespace-only changes.

bg_data/bg_grabber.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from google_images_download import google_images_download
2+
import os
3+
4+
def downloadGoogleImages(arguments, inputfolder):
5+
"""
6+
Using the GoogleDownloaderArgument to obtain a bunch of images.
7+
:param arguments:
8+
:return: a list of all the files downloaded
9+
"""
10+
os.chdir(inputfolder)
11+
download_instance = google_images_download.googleimagesdownload()
12+
13+
arguments = \
14+
{"keywords": "Office,patterns,logos,home,design,man-made",
15+
"limit": 150,
16+
"chromedriver": r"C:\bin\chromedriver.exe",
17+
"size": ">800*600",
18+
"format": "jpg",
19+
"print_urls": True} # creating list of arguments
20+
21+
absolute_image_paths = download_instance.download(arguments)
22+
print(absolute_image_paths) # printing absolute paths of the downloaded images
23+
24+
return(absolute_image_paths)

marker_data/augmentation_sequence.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
from imgaug import augmenters as iaa
2+
3+
def Aug100px():
4+
"""
5+
Augment 100 pixel images.
6+
:return:
7+
"""
8+
aug_seq = iaa.Sequential([
9+
#iaa.Crop(px=(0, 15)), # crop images from each side by 0 to 33px (randomly chosen)
10+
iaa.Fliplr(0.5), # horizontally flip 50% of the images
11+
iaa.Flipud(0.5), # horizontally flip 50% of the images
12+
iaa.GaussianBlur(sigma=(0, 2.0)), # blur images with a sigma of 0 to 2.0
13+
iaa.Multiply((0.25, 1.75), per_channel=True),
14+
iaa.AddToHueAndSaturation((-25, 25)),
15+
iaa.Dropout((0.01, 0.2), per_channel=True),
16+
iaa.SaltAndPepper((0.01,0.05), per_channel=True),
17+
iaa.Affine(
18+
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
19+
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
20+
rotate=(-45, 45), # rotate by -45 to +45 degrees
21+
shear=(-16, 16), # shear by -16 to +16 degrees
22+
)
23+
])
24+
25+
return aug_seq
26+
27+
def Aug200px():
28+
"""
29+
Augment 200 pixel images.
30+
:return:
31+
"""
32+
aug_seq = iaa.Sequential([
33+
#iaa.Crop(px=(0, 15)), # crop images from each side by 0 to 33px (randomly chosen)
34+
iaa.Fliplr(0.5), # horizontally flip 50% of the images
35+
iaa.Flipud(0.5), # horizontally flip 50% of the images
36+
iaa.GaussianBlur(sigma=(0, 2.0)), # blur images with a sigma of 0 to 2.0
37+
iaa.Multiply((0.25, 1.75), per_channel=True),
38+
iaa.AddToHueAndSaturation((-25, 25)),
39+
iaa.Dropout((0.01, 0.2), per_channel=True),
40+
iaa.SaltAndPepper((0.01,0.05), per_channel=True),
41+
iaa.Affine(
42+
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
43+
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
44+
rotate=(-45, 45), # rotate by -45 to +45 degrees
45+
shear=(-16, 16), # shear by -16 to +16 degrees
46+
)
47+
])
48+
49+
return aug_seq
50+
51+
def Aug300px():
52+
"""
53+
Augment 300 pixel images.
54+
:return:
55+
"""
56+
aug_seq = iaa.Sequential([
57+
#iaa.Crop(px=(0, 15)), # crop images from each side by 0 to 33px (randomly chosen)
58+
iaa.Fliplr(0.5), # horizontally flip 50% of the images
59+
iaa.Flipud(0.5), # horizontally flip 50% of the images
60+
iaa.GaussianBlur(sigma=(0, 2.0)), # blur images with a sigma of 0 to 2.0
61+
iaa.Multiply((0.25, 1.75), per_channel=True),
62+
iaa.AddToHueAndSaturation((-25, 25)),
63+
iaa.Dropout((0.01, 0.2), per_channel=True),
64+
iaa.SaltAndPepper((0.01,0.05), per_channel=True),
65+
iaa.Affine(
66+
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
67+
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
68+
rotate=(-45, 45), # rotate by -45 to +45 degrees
69+
shear=(-16, 16), # shear by -16 to +16 degrees
70+
)
71+
])
72+
73+
return aug_seq
74+
75+
def Aug400px():
76+
"""
77+
Augment 400 pixel images.
78+
:return:
79+
"""
80+
aug_seq = iaa.Sequential([
81+
#iaa.Crop(px=(0, 15)), # crop images from each side by 0 to 33px (randomly chosen)
82+
iaa.Fliplr(0.5), # horizontally flip 50% of the images
83+
iaa.Flipud(0.5), # horizontally flip 50% of the images
84+
iaa.GaussianBlur(sigma=(0, 2.0)), # blur images with a sigma of 0 to 2.0
85+
iaa.Multiply((0.25, 1.75), per_channel=True),
86+
iaa.AddToHueAndSaturation((-25, 25)),
87+
iaa.Dropout((0.01, 0.2), per_channel=True),
88+
iaa.SaltAndPepper((0.01,0.05), per_channel=True),
89+
iaa.Affine(
90+
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
91+
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
92+
rotate=(-45, 45), # rotate by -45 to +45 degrees
93+
shear=(-16, 16), # shear by -16 to +16 degrees
94+
)
95+
])
96+
97+
return aug_seq
98+
99+
def Aug500px():
100+
"""
101+
Augment 500 pixel images.
102+
:return:
103+
"""
104+
aug_seq = iaa.Sequential([
105+
#iaa.Crop(px=(0, 15)), # crop images from each side by 0 to 33px (randomly chosen)
106+
iaa.Fliplr(0.5), # horizontally flip 50% of the images
107+
iaa.Flipud(0.5), # horizontally flip 50% of the images
108+
iaa.GaussianBlur(sigma=(0, 2.0)), # blur images with a sigma of 0 to 2.0
109+
iaa.Multiply((0.25, 1.75), per_channel=True),
110+
iaa.AddToHueAndSaturation((-25, 25)),
111+
iaa.Dropout((0.01, 0.2), per_channel=True),
112+
iaa.SaltAndPepper((0.01,0.05), per_channel=True),
113+
iaa.Affine(
114+
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
115+
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
116+
rotate=(-45, 45), # rotate by -45 to +45 degrees
117+
shear=(-16, 16), # shear by -16 to +16 degrees
118+
)
119+
])
120+
121+
return aug_seq
122+
123+
def BackgroundAug500px():
124+
"""
125+
Augment 500 pixel images.
126+
:return:
127+
"""
128+
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
129+
130+
aug_seq = iaa.Sequential([
131+
#iaa.Crop(px=(0, 15)), # crop images from each side by 0 to 33px (randomly chosen)
132+
sometimes(iaa.Fliplr(0.5)), # horizontally flip 50% of the images
133+
sometimes(iaa.Flipud(0.5)), # horizontally flip 50% of the images
134+
sometimes(iaa.GaussianBlur(sigma=(0, 2.0))), # blur images with a sigma of 0 to 2.0
135+
sometimes(iaa.Multiply((0.25, 1.75), per_channel=True)),
136+
sometimes(iaa.AddToHueAndSaturation((-25, 25))),
137+
sometimes(iaa.Dropout((0.01, 0.2), per_channel=True)),
138+
sometimes(iaa.SaltAndPepper((0.01,0.05), per_channel=True)),
139+
sometimes(iaa.Affine(
140+
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
141+
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
142+
rotate=(-180, 180), # rotate randomly
143+
shear=(-16, 16), # shear by -16 to +16 degrees
144+
)),
145+
sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
146+
])
147+
148+
return aug_seq

0 commit comments

Comments
 (0)