test_tools
analyse-annotations.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 
5 # System
6 import os
7 
8 # TU/e Robotics
9 from robocup_knowledge import knowledge_loader
10 
11 """
12 Counts the images in the subdirectories of
13 ~/MEGA/data<ROBOT_ENV>/training_data/annotated. Both the verified and
14 unverified annotations are checked and a summary is printed to screen.
15 This contains:
16 - Per object that is present in the database: the amount of images present in the directory
17 - If images are not present in the database, a warning is printed
18 """
19 
20 
21 # Colors from printing on screen
22 class BColors:
23  HEADER = "\033[95m"
24  OKBLUE = "\033[94m"
25  OKGREEN = "\033[92m"
26  WARNING = "\033[93m"
27  FAIL = "\033[91m"
28  ENDC = "\033[0m"
29  BOLD = "\033[1m"
30  UNDERLINE = "\033[4m"
31 
32 
33 def count_images(objects, path):
34  """ Counts the images in the subdirectories of 'path'. The subdirectories are identified by the provided objects.
35  The results are printed to screen
36 
37  :param objects: list with strings
38  :param path: string indicating the path
39  """
40  # List the number of occurrences in each sub folder
41  ustats = [] # Unverified
42  for o in objects:
43  p = os.path.join(path, o)
44 
45  # If path doesn't exist, we probably don't have any images
46  if not os.path.exists(p):
47  ustats.append((o, 0))
48  else:
49  ustats.append((o, len(os.listdir(p))))
50 
51  # Sort and print the results
52  ustats.sort(key=lambda tup: tup[1], reverse=True)
53  for s in ustats:
54  if s[1] > 0:
55  print("{}: {}".format(s[0], s[1]))
56  else:
57  print(BColors.WARNING + "{}: {}".format(s[0], s[1]) + BColors.ENDC)
58 
59  # Sanity check: try to identify mismatches between object names and annotated images
60  print(BColors.BOLD + "\nPossible mismatches:" + BColors.ENDC)
61  print("Annotated but not in knowledge")
62  for candidate in os.listdir(path):
63  if candidate not in objects:
64  print(BColors.WARNING + candidate + BColors.ENDC)
65 
66  print
67 
68 
69 if __name__ == "__main__":
70 
71  # Get the names of the objects in which we are interested
72  common_knowledge = knowledge_loader.load_knowledge("common")
73  objects = common_knowledge.object_names
74  objects_set = set(objects)
75 
76 
77  robot_env = os.environ.get("ROBOT_ENV")
78  # get the names of the objects in output labels
79  tensorflow_labels_path = os.path.join(os.path.expanduser("~"), "MEGA", "data", robot_env, "models", "tensorflow_ros", "output_labels.txt")
80  with open(tensorflow_labels_path) as tensorflow_labels_file:
81  raw_labels_lines = tensorflow_labels_file.readlines()
82  tensorflow_labels = [line.strip() for line in raw_labels_lines]
83  tensorflow_set = set(tensorflow_labels)
84 
85  print("objects that are present in objects_set but not in tensorflow_set\n")
86  for o in objects_set.difference(tensorflow_set):
87  print(o)
88  print
89 
90  print("objects that are present in tensorflow_set but not in objects_set\n")
91  for t in tensorflow_set.difference(objects_set):
92  print(t)
93  print
94 
95  # Get the path to the folder where images are stored
96  path = os.path.join(os.path.expanduser("~"), "MEGA", "data", robot_env, "training_data", "annotated")
97 
98  # Count both verified and unverified
99  for v in ["verified", "unverified"]:
100  tpath = os.path.join(path, v)
101  print(BColors.HEADER + BColors.BOLD + v.upper() + BColors.ENDC + ':\n')
102  count_images(objects=objects, path=tpath)
analyse-annotations.count_images
def count_images(objects, path)
Definition: analyse-annotations.py:33
analyse-annotations.BColors
Definition: analyse-annotations.py:22