-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify_leaf_disc.py
274 lines (230 loc) · 8.89 KB
/
classify_leaf_disc.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
import os
import sys
import keras
import image_slicer
from keras.preprocessing.image import load_img, img_to_array
import shutil
import time
import random
import datetime
# Version 0.2
# license http://creativecommons.org/licenses/by-nc-sa/4.0/
# Print iterations progress (https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console)
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = 'X', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + ' ' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
# Parse the input from run_classification
def input():
if len(sys.argv) == 5:
if os.path.isdir(sys.argv[1]) == True:
f_leaf_discs = sys.argv[1]
else:
print("Leaf disc image folder does not exist")
exit()
try:
f_model_leaf_vs_back = keras.models.load_model(sys.argv[2])
# Do something with the file
except IOError:
print("Model file leaf vs back not accessible")
exit()
try:
f_model_nospo_vs_spo = keras.models.load_model(sys.argv[3])
# Do something with the file
except IOError:
print("Model file nospo vs spo not accessible")
exit()
if sys.argv[4] == '':
print("No experiment name given.")
exit()
else:
f_exp_name=sys.argv[4]
return f_leaf_discs, f_model_leaf_vs_back, f_model_nospo_vs_spo, f_exp_name
else:
print(sys.argv)
exit("Usage: python /path/to/leaf/disc /path/to/CNN_model_leaf_vs_back /path/to/CNN_model_nospo_vs_spo experiment_name")
# Run CNN1 for leaf vs back
def predict_1(x):
global model_leaf_vs_back
img = load_img(x)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
return (model_leaf_vs_back.predict(x)> 0.5).astype("int32")
# Run CNN2 for spo vs nospo
def predict_2(x):
global model_nospo_vs_spo
img = load_img(x)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
return (model_nospo_vs_spo.predict(x)> 0.5).astype("int32")
# Generate a random string for the temp folder
def get_random_string():
sample_letters = 'abcdefghi'
result_str = ''.join((random.choice(sample_letters) for i in range(5)))
return result_str
# Do the image classification
def classify_img_slices(f_image):
global sample
sample = sample + 1
# get the path and file name
leaf_discs_path = os.path.dirname(f_image)+"/"
leaf_disc_name = os.path.basename(f_image)
rnd = get_random_string()
# Create a temporary directory for the image slices
try:
os.mkdir(leaf_discs_path + "tmp"+rnd+"/")
except OSError:
print ("[warning]\t\tCreation of the directory %s/tmp/ failed" % leaf_discs_path)
# Slice the leaf disc image in 500 pieces and save them in the tmp file
tiles = image_slicer.slice(f_image, 500, save=False)
image_slicer.save_tiles(tiles, directory=leaf_discs_path + "tmp" + rnd, prefix=leaf_disc_name, format='png')
# Set the counter to zero and initialize the variables
count = 0
count_1 = 0
count_0 = 0
leafdisc = []
back = []
images = []
# First CNN for leaf disc vs background (CNN1)
for f in os.listdir(leaf_discs_path + "tmp" + rnd):
if f.endswith('.png'):
path_file = leaf_discs_path + "tmp" + rnd + "/" + f
classes = predict_1(path_file)
count = count + 1
if classes[0, 0] == 1:
count_1 = count_1 + 1
leafdisc.append(path_file)
else:
count_0 = count_0 + 1
back.append(path_file)
continue
else:
continue
# Set counter 0 and initialize variables
count_11 = 0
count_1_1 = 0
count_0_1 = 0
spo = []
no_spo = []
# Second CNN for sporangia vs no sporangia (CNN2)
for l in leafdisc:
# Call predict_2() function
classes = predict_2(l)
count_11 = count_11 + 1
if classes[0, 0] == 1:
count_1_1 = count_1_1 + 1
spo.append(l)
else:
count_0_1 = count_0_1 + 1
no_spo.append(l)
continue
# Open a coordinate file for images with sporangia
with open(f_image+"spo_coord.txt", 'w') as f:
for k in spo:
if k.endswith('.png'):
# Copy the file to the folder spo/ for later inspection
shutil.copy2(k, leaf_discs_path+"spo/")
# Split the
n = k.split("/")
n1 = n[-1].split(".")
n2 = n1[1].split("_")
y = 23-int(n2[1])
x = int(n2[2])
f.write(str(x)+"\t"+str(y)+"\n")
else:
continue
# Open the created results file in 'append' mode
with open(leaf_discs_path+"classify_results.txt", 'a') as results:
# Write the results from the classification to file
results.write(input()[3]+"\t"+f_image+"\t"+str(sample)+"\t"+str(count_1) + "\t" + str(count_0) + "\t" + str(round((count_1 / count) * 100))
+ "\t" + str(round((count_0 / count) * 100)) + "\t" + str(count_1_1)
+ "\t" + str(count_0_1) + "\t" + str(round((count_1_1 / count_11) * 100)) + "\t" + str(round((count_0_1 / count_11) * 100)) + "\n")
results.close()
# Clear the content of tmp/ directory
shutil.rmtree(leaf_discs_path+"tmp"+rnd)
# Main function
def main():
# Get global variables
global model_leaf_vs_back
global model_nospo_vs_spo
global sample
# Get the input
input_1 = ()
input_1 = input()
leaf_discs = input_1[0]
start = time.time()
# Initialize sample name variable
i = 0
leaf_disc_imgs = []
a = ''
# Check the number of images
num_img = 0
for image in os.listdir(leaf_discs):
if image.endswith('.jpg'):
num_img = num_img + 1
else:
continue
# Print the run parameters
print("[info]\t\t# leaf discs:\t"+str(num_img))
print("[info]\t\tFolder:\t\t"+leaf_discs)
print("[info]\t\tModel 1:\t"+str(sys.argv[2]))
print("[info]\t\tModel 2:\t"+str(sys.argv[3]))
# Create a temporary directory for the image slices
try:
os.mkdir(leaf_discs + "spo/")
except OSError:
print ("[warning]\t\tCreation of the directory %s/spo/ failed" % leaf_discs)
# Open the output file for the results (creates it if not existing)
with open(leaf_discs+"classify_results.txt", 'w') as results:
# Print the header of the results file: Date and time, the folder with leaf discs, CNN1 and CNN2
results.write("# Date time: " + str(datetime.datetime.now()))
results.write("# Folder:\t\t"+leaf_discs)
results.write("# Model 1:\t"+sys.argv[2])
results.write("# Model 1:\t"+sys.argv[3])
results.write("Exp_name\tSample\tNumber\tLeaf_disc\tAgar\tperc_leaf_disc\tperc_agar\tspo\tno_spo\tperc_spo\tperc_no_spo\n")
results.close()
# Create a list with all the images in the folder
for image in sorted(os.listdir(leaf_discs)):
# Only do somthing with files that are a image
if image.endswith('.jpg'):
a = leaf_discs+image
leaf_disc_imgs.append(a)
else:
continue
# Print the progress bar to stdout
printProgressBar(0, num_img, prefix = '[info] Progress:', suffix = 'Complete', length = 50)
# Start iterating over the images in the directory
for image in leaf_disc_imgs:
# Only do somthing with files that are a image
if image.endswith('.jpg'):
# Run function classify_img_slices()
classify_img_slices(image)
i = i + 1
sample = sample + 1
printProgressBar(i, num_img, prefix = '[info] Progress:', suffix = 'Complete', length = 50)
else:
continue
end = time.time()
print("[info] Elapsed time: " + str(round((end - start)/60))+" min")
if __name__ == '__main__':
model_leaf_vs_back = input()[1]
model_nospo_vs_spo = input()[2]
sample = 0
main()