-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (133 loc) · 7.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Module: This file is considered the main file and should be run first
This module should be runned first; the functions used in this file are imported from functions.py.
This module creates the 'processed_dataset', which contains augmented photos.
This module creates the metadata_model.csv, which contains all the features of the augmented photos.
"""
import os
import csv
import shutil
from pillow_heif import register_heif_opener
from functions import (augmentation_function,
convert_heic_jpg,
geometric_feature,
ignore_files,
lowpass_filter,
fourier,
invariant_moments,
texture,
color)
register_heif_opener()
# load photos and store in directory which is named after the class
current_directory = os.getcwd()
DATA_FOLDER_PATH = os.path.join(current_directory, 'datasets/final_dataset')
DATA_FOLDER_PATH = os.path.normpath(DATA_FOLDER_PATH).replace("\\", "/")
# create augmentated directory which has same buildup as original directory
if not os.path.exists(os.path.join(current_directory, 'datasets/processed_dataset')):
shutil.copytree(DATA_FOLDER_PATH,
os.path.join(current_directory, 'datasets/processed_dataset'),
ignore=ignore_files)
else:
for folder in os.listdir(os.path.join(current_directory, 'datasets/processed_dataset')):
folder_path = os.path.join(os.path.join(current_directory, 'datasets/processed_dataset'), folder)
folder_path = os.path.normpath(folder_path)
folder = os.listdir(folder_path)
for photo in folder:
photo_path = os.path.join(folder_path, photo)
os.remove(photo_path)
AUGMENT_DATA_FOLDER_PATH = os.path.join(current_directory, 'datasets/processed_dataset')
# go over all the folder and photos and convert heic to jpg
species = os.listdir(DATA_FOLDER_PATH)
for heic_folder in species:
specie_folder_path = os.path.join(DATA_FOLDER_PATH, heic_folder)
specie_folder_path = os.path.normpath(specie_folder_path)
convert_heic_jpg(specie_folder_path)
# go over photo inside species folder and add to annotation file, called metadata_model.csv
metadata = []
for specie in species:
specie_folder_path = os.path.join(DATA_FOLDER_PATH, specie)
specie_folder_path = os.path.normpath(specie_folder_path)
# go over all photos to preprocess photo and extract features
specie_folder = os.listdir(specie_folder_path)
for photo in specie_folder:
photo_path = os.path.join(specie_folder_path, photo)
photo_path = os.path.normpath(photo_path)
photo_path = photo_path.replace("\\", "\\\\")
# lowpass filter to smoothen photo en reduce noise
lowpass_filtered_photo = lowpass_filter(photo_path, 5)
# augmentate photos to get more data
augmentations = augmentation_function(lowpass_filtered_photo)
augmentation_names = [
"resized", "rot90", "rot190", "rot270", "randomcrop",
"centercrop", "rot90flip", "rot180flip", "rot270flip"
]
# save augmented photos and add to processed_dataset folder
for augmentation, augment_data in enumerate(augmentations):
augment_path = os.path.join(AUGMENT_DATA_FOLDER_PATH,
specie,
f"{augmentation_names[augmentation]} {photo}")
augment_data.save(augment_path)
# feature extraction of augmented and blurred pictures
geometric_features = geometric_feature(augment_path)
spatial_frequencies = fourier(augment_path)
hu_moments = invariant_moments(augment_path)
texture_features = texture(augment_path)
color_features = color(augment_path)
# put metadata of photo in metadata_model.csv file (annotation file)
metadata_photo = {'augment_specie_folder_path': {augment_path},
'species_id': {photo},
'augmentation': {augmentation_names[augmentation]},
'area': {geometric_features[0]},
'perimeter': {geometric_features[1]},
'circularity_ratio': {geometric_features[2]},
'eccentricity': {geometric_features[3]},
'major_axis_length': {geometric_features[4]},
'minor_axis_length': {geometric_features[5]},
'convex_area': {geometric_features[6]},
'solidity': {geometric_features[7]},
'equivalent_diameter_area': {geometric_features[8]},
'spatial_frequency_1': {spatial_frequencies[0]},
'spatial_frequency_2': {spatial_frequencies[1]},
'hu_moment_1': {hu_moments[0]},
'hu_moment_2': {hu_moments[1]},
'hu_moment_3': {hu_moments[2]},
'hu_moment_4': {hu_moments[3]},
'hu_moment_5': {hu_moments[4]},
'hu_moment_6': {hu_moments[5]},
'hu_moment_7': {hu_moments[6]},
'contrast': {texture_features[0]},
'dissimilarity': {texture_features[1]},
'homogeneity': {texture_features[2]},
'energy': {texture_features[3]},
'correlation': {texture_features[4]},
'ASM': {texture_features[5]},
'mean_hue_hsv': {color_features[0]},
'std_hue_hsv': {color_features[1]},
'mean_sat_hsv': {color_features[2]},
'std_sat_hsv': {color_features[3]},
'mean_hue_LCH': {color_features[4]},
'std_hue_LCH': {color_features[5]},
'mean_sat_LCH': {color_features[6]},
'std_sat_LCH': {color_features[7]},
'mean_luminance': {color_features[8]},
'std_sat_lab': {color_features[9]}}
metadata.append(metadata_photo)
# create csv file and reader object to read CSV file
CSV_FILE = 'annotation_files/metadata_model.csv'
# put metadata in csv file
with open(CSV_FILE, 'a', newline='', encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=[
"augment_specie_folder_path", "species_id", "augmentation",
'area', 'perimeter', 'circularity_ratio', 'eccentricity',
'major_axis_length', 'minor_axis_length', 'convex_area',
'solidity', 'equivalent_diameter_area', 'spatial_frequency_1',
'spatial_frequency_2', 'hu_moment_1', 'hu_moment_2', 'hu_moment_3',
'hu_moment_4', 'hu_moment_5', 'hu_moment_6', 'hu_moment_7',
'contrast', 'dissimilarity', 'homogeneity', 'energy',
'correlation', 'ASM', 'mean_hue_hsv', 'std_hue_hsv', 'mean_sat_hsv',
'std_sat_hsv', 'mean_hue_LCH', 'std_hue_LCH', 'mean_sat_LCH',
'std_sat_LCH', 'mean_luminance', 'std_sat_lab',
])
writer.writeheader()
for item in metadata:
writer.writerow(item)