-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_pet_labels.py
145 lines (121 loc) · 6.08 KB
/
get_pet_labels.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# */AIPND-revision/intropyproject-classify-pet-images/get_pet_labels.py
#
# PROGRAMMER: Pitson Mwakabila
# DATE CREATED: 08/11/2020
# REVISED DATE: 18/11/2020
# PURPOSE: Create the function get_pet_labels that creates the pet labels from
# the image's filename. This function inputs:
# - The Image Folder as image_dir within get_pet_labels function and
# as in_arg.dir for the function call within the main function.
# This function creates and returns the results dictionary as results_dic
# within get_pet_labels function and as results within main.
# The results_dic dictionary has a 'key' that's the image filename and
# a 'value' that's a list. This list will contain the following item
# at index 0 : pet image label (string).
#
##
# Imports python modules
"""
NOTE: Introduced the path module for a more generalized extraction of pet image labels from the file with a .jpg extension as suggeted by the previous reviewer
"""
from os import listdir,path
# TODO 2: Define get_pet_labels function below please be certain to replace None
# in the return statement with results_dic dictionary that you create
# with this function
#
def get_pet_labels(image_dir):
"""
Creates a dictionary of pet labels (results_dic) based upon the filenames
of the image files. These pet image labels are used to check the accuracy
of the labels that are returned by the classifier function, since the
filenames of the images contain the true identity of the pet in the image.
Be sure to format the pet labels so that they are in all lower case letters
and with leading and trailing whitespace characters stripped from them.
(ex. filename = 'Boston_terrier_02259.jpg' Pet label = 'boston terrier')
Parameters:
image_dir - The (full) path to the folder of images that are to be
classified by the classifier function (string)
Returns:
results_dic - Dictionary with 'key' as image filename and 'value' as a
List. The list contains for following item:
index 0 = pet image label (string)
"""
# Creates list of files in directory
in_files = listdir(image_dir)
# Processes each of the files to create a dictionary where the key
# is the filename and the value is the picture label (below).
# Creates empty dictionary for the results (pet labels, etc.)
results_dic = dict()
#items_in_dic = len(results_dic)
#print("\nEmpty Dictionary results_dic - n items=", items_in _dic)
# filenames = ["beagle_0239", "Boston_terrier_02259.jpg"]
# pet_labels = ["beagle","boston terrier"]
#ilenames = []
#pet_labels = []
#File names and labels
#filename_list = list_dir("pet_images/")
#filename_list = listdir(image_dir)
''''
for fn in range(len(filename_list)):
filenames.append(filename_list[fn])
#print(filenames)
#pet labels
for i in range(len(filenames)):
filename = filenames[i]
splited_fn = filename.split("_")
v = 0
label_string = []
while v < int(len(splited_fn)-1):
label_string.append(splited_fn[v])
v+=1
pet_labels.append(" ".join(label_string))
#print("\nPrinting labels\n")
#print(pet_labels)
for idx not in range (0,len(filenames),1):
if filenames[idx] not in results_dic:
results_dic[filenames[idx]] = [pet_labels[idx]]
else:
print("*** Warning: Keys=", filenames[idx], "already exists in results_dic with value=", results_dicfilenames[idx])
#print("")
# Replace None with the results_dic dictionary that you created with this
# function
'''
# Processes through each file in the directory, extracting only the words
# of the file that contain the pet image label
for idx in range(0, len(in_files), 1):
#print("File name=",in_files[idx])
# Skips file if starts with . (like .DS_Store of Mac OSX) because it
# isn't an pet image file
if in_files[idx][0] != ".":
# Creates temporary label variable to hold pet label name extracted
pet_label = " "
# TODO: 2a. BELOW REPLACE pass with CODE that will process each
# filename in the in_files list to extract the dog breed
# name from the filename. Recall that each filename can be
# accessed by in_files[idx]. Be certain to place the
# extracted dog breed name in the variable pet_label
# that's created as an empty string ABOVE
#pass
#Split the in_files[idx] list and then convert the list to string and remove whitespaces to the end of the string while converting to lowercase
splited_root_ext = path.splitext(in_files[idx])
#Conditiobal statement for assigning the splited list values and then append the results to pet_label
if type(splited_root_ext[0])!='str' and len(splited_root_ext[0])!=1:
pet_label = pet_label.join(splited_root_ext[0].split("_")[:-1]).lower().strip()
else:
pet_label = pet_label.join(splited_root_ext[0]).lower().strip()
# If filename doesn't already exist in dictionary add it and it's
# pet label - otherwise print an error message because indicates
# duplicate files (filenames)
if in_files[idx] not in results_dic:
results_dic[in_files[idx]] = [pet_label]
else:
print("** Warning: Duplicate files exist in directory:",
in_files[idx])
# TODO 2b. Replace None with the results_dic dictionary that you created
# with this function
#return None
#print("Printing initial results dic")
#print(results_dic)
return results_dic