Advertisement
Guest User

get_pet_labels.py

a guest
Oct 19th, 2019
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # Imports python modules
  2. from os import listdir
  3.  
  4. # TODO 2: Define get_pet_labels function below please be certain to replace None
  5. # in the return statement with results_dic dictionary that you create
  6. # with this function
  7. #
  8. def get_pet_labels(pet_images):
  9. """
  10. Creates a dictionary of pet labels (results_dic) based upon the filenames
  11. of the image files. These pet image labels are used to check the accuracy
  12. of the labels that are returned by the classifier function, since the
  13. filenames of the images contain the true identity of the pet in the image.
  14. Be sure to format the pet labels so that they are in all lower case letters
  15. and with leading and trailing whitespace characters stripped from them.
  16. (ex. filename = 'Boston_terrier_02259.jpg' Pet label = 'boston terrier')
  17. Parameters:
  18. image_dir - The (full) path to the folder of images that are to be
  19. classified by the classifier function (string)
  20. Returns:
  21. results_dic - Dictionary with 'key' as image filename and 'value' as a
  22. List. The list contains for following item:
  23. index 0 = pet image label (string)
  24. """
  25.  
  26. in_files = listdir(pet_images)
  27. results_dic = dict()
  28. for idx in range[0, len(in_files), 1]:
  29. if in_files[idx][0] != ".":
  30. pet_label = ""
  31. pet_label += in_files[idx].lower().split("_").strip()
  32. if in_files[idx] not in results_dic:
  33. results_dic[in_files[idx]] = [pet_label]
  34. else:
  35. print("** Warning: Duplicate files exist in directory:",
  36. in_files[idx])
  37. return results_dic
  38. print(results_dic)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement