-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify_images.py
239 lines (201 loc) · 10.3 KB
/
classify_images.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# */AIPND-revision/intropyproject-classify-pet-images/classify_images.py
#
# PROGRAMMER: Pitson Mwakabila
# DATE CREATED: 10/11/2020
# REVISED DATE: 17/11/2020
# PURPOSE: Create a function classify_images that uses the classifier function
# to create the classifier labels and then compares the classifier
# labels to the pet image labels. This function inputs:
# -The Image Folder as image_dir within classify_images and function
# and as in_arg.dir for function call within main.
# -The results dictionary as results_dic within classify_images
# function and results for the functin call within main.
# -The CNN model architecture as model wihtin classify_images function
# and in_arg.arch for the function call within main.
# This function uses the extend function to add items to the list
# that's the 'value' of the results dictionary. You will be adding the
# classifier label as the item at index 1 of the list and the comparison
# of the pet and classifier labels as the item at index 2 of the list.
#
##
# Imports classifier function for using CNN to classify images
from classifier import classifier
from os import listdir
import ast
# TODO 3: Define classify_images function below, specifically replace the None
# below by the function definition of the classify_images function.
# Notice that this function doesn't return anything because the
# results_dic dictionary that is passed into the function is a mutable
# data type so no return is needed.
#
def classify_images(images_dir, results_dic, model):
"""
Creates classifier labels with classifier function, compares pet labels to
the classifier labels, and adds the classifier label and the comparison of
the labels to the results dictionary using the extend function. Be sure to
format the classifier labels so that they will match your pet image labels.
The format will include putting the classifier labels in all lower case
letters and strip the leading and trailing whitespace characters from them.
For example, the Classifier function returns = 'Maltese dog, Maltese terrier, Maltese'
so the classifier label = 'maltese dog, maltese terrier, maltese'.
Recall that dog names from the classifier function can be a string of dog
names separated by commas when a particular breed of dog has multiple dog
names associated with that breed. For example, you will find pet images of
a 'dalmatian'(pet label) and it will match to the classifier label
'dalmatian, coach dog, carriage dog' if the classifier function correctly
classified the pet images of dalmatians.
PLEASE NOTE: This function uses the classifier() function defined in
classifier.py within this function. The proper use of this function is
in test_classifier.py Please refer to this program prior to using the
classifier() function to classify images within this function
Parameters:
images_dir - The (full) path to the folder of images that are to be
classified by the classifier function (string)
results_dic - Results Dictionary with 'key' as image filename and 'value'
as a List. Where the list will contain the following items:
index 0 = pet image label (string)
--- where index 1 & index 2 are added by this function ---
NEW - index 1 = classifier label (string)
NEW - index 2 = 1/0 (int) where 1 = match between pet image
and classifer labels and 0 = no match between labels
model - Indicates which CNN model architecture will be used by the
classifier function to classify the pet images,
values must be either: resnet alexnet vgg (string)
Returns:
None - results_dic is mutable data type so no return needed.
"""
'''
filenames = []
filename_list = listdir(images_dir)
for fn in range(len(filename_list)):
filenames.append(filename_list[fn])
classifier_labels = []
classifier_label_set = open("imagenet1000_clsid_to_human.txt")
set_label = ast.literal_eval(classifier_label_set.read())
for i in range(0,len(set_label),1):
classifier_labels.append(set_label[i].strip().lower())
#classifier_labels.append(set_label[i])
#print(set_label)
#print(classifier_labels)
#for sl in range(0,len(set_label),1):
#for sl in set_label:
# print(sl)
#print(set_label)
#Pet labels
pet_labels = []
#Iterating through results_dic to get labels
for idx in results_dic:
pet_labels.append(results_dic[idx][0].strip().lower())
#print(results_dic[idx][0])
#print("Printing Pet Labels")
#print(pet_labels)
#Pet labels is dog
pet_label_is_dog = []
#classifier label as dog
classifier_label_is_dog = []
#Populate a list for matching dogs
for i in range(len(filenames)):
if(int(len(filenames)-len(pet_label_is_dog))==1):
pet_label_is_dog.append(0)
#classifier_label_is_dog.append(0)
else:
pet_label_is_dog.append(1)
#classifier_label_is_dog.append(1)
for i in range(len(filenames)):
if(int(len(filenames)-len(classifier_label_is_dog))==1):
#pet_label_is_dog.append(0)
classifier_label_is_dog.append(0)
else:
#pet_label_is_dog.append(1)
classifier_label_is_dog.append(1)
#Modifying the dictionary with both labels and indicates if they match
for idx in range(0,len(filenames),1):
#print(filenames[idx])
#
if filenames[idx] in results_dic:
results_dic[filenames[idx]] = [pet_labels[idx], classifier_labels[idx]]
#print(results_dic[filenames[idx]])
#print(pet_labels[idx])
#print(classifier_labels[idx])
#print(filenames[idx])
#print("Pet Labels Against Classifier Labels");
#print("Dog " + pet_labels[idx] + " : Classifier " + classifier_labels[idx])
if pet_labels[idx] in classifier_labels[idx]:
results_dic[filenames[idx]].append(1)
#classifier_label_is_dog.append(1)
else:
results_dic[filenames[idx]].append(0)
#classifier_label_is_dog.append(0)
"""
for idx in range(len(filenames)):
results_dic[filenames[idx]].extend([pet_label_is_dog[idx], classifier_label_is_dog[idx]])
#print(results_dic)
# Iterates through the list to print the results for each filename
for key in results_dic:
print("\nFilename=", key, "\npet_image Label=", results_dic[key][0],
"\nClassifier Label=", results_dic[key][1], "\nmatch=",
results_dic[key][2], "\nImage is dog=", results_dic[key][3],
"\nClassifier is dog=", results_dic[key][4])
# Provides classifications of the results
if sum(results_dic[key][2:]) == 3:
print("*Breed Match*")
if sum(results_dic[key][3:]) == 2:
print("*Is-a-Dog Match*")
if sum(results_dic[key][3:]) == 0 and results_dic[key][2] == 1:
print("*NOT-a-Dog Match*")
#print(classifier_labels)
"""
'''
# Process all files in the results_dic - use images_dir to give fullpath
# that indicates the folder and the filename (key) to be used in the
# classifier function
for key in results_dic:
# TODO: 3a. Set the string variable model_label to be the string that's
# returned from using the classifier function instead of the
# empty string below.
#
# Runs classifier function to classify the images classifier function
# inputs: path + filename and model, returns model_label
# as classifier label
model_label = ""
# TODO: 3b. BELOW REPLACE pass with CODE to process the model_label to
# convert all characters within model_label to lowercase
# letters and then remove whitespace characters from the ends
# of model_label. Be certain the resulting processed string
# is named model_label.
#
# Processes the results so they can be compared with pet image labels
# set labels to lowercase (lower) and stripping off whitespace(strip)
#pass
model_label = classifier(images_dir + key, model).lower().strip()
# defines truth as pet image label
truth = results_dic[key][0]
#print("Truth", truth)
#print("Model Label",model_label)
# TODO: 3c. REPLACE pass BELOW with CODE that uses the extend list function
# to add the classifier label (model_label) and the value of
# 1 (where the value of 1 indicates a match between pet image
# label and the classifier label) to the results_dic dictionary
# for the key indicated by the variable key
#
# If the pet image label is found within the classifier label list of terms
# as an exact match to on of the terms in the list - then they are added to
# results_dic as an exact match(1) using extend list function
if truth in model_label:
#pass
results_dic[key].extend([model_label,1])
# TODO: 3d. REPLACE pass BELOW with CODE that uses the extend list function
# to add the classifier label (model_label) and the value of
# 0 (where the value of 0 indicates NOT a match between the pet
# image label and the classifier label) to the results_dic
# dictionary for the key indicated by the variable key
#
# if not found then added to results dictionary as NOT a match(0) using
# the extend function
else:
#pass
results_dic[key].extend([model_label,0])
#print(results_dic)
None