quick_label.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (c) 2023 Felix Kleinsteuber and Computer Vision Group, Friedrich Schiller University Jena
  2. # Quick labeling script.
  3. # The user is displayed every image and can then assign an image as "normal" (1-key) or "anomalous" (2-key).
  4. # The list of all normal and anomalous images will be printed after every image to be copied to Labels.py.
  5. import cv2
  6. import argparse
  7. import os
  8. from py.Dataset import Dataset
  9. from py.FileUtils import list_jpegs_recursive
  10. def main():
  11. parser = argparse.ArgumentParser(description="BOW train script")
  12. parser.add_argument("dataset_dir", type=str, help="Directory of the dataset containing all session folders")
  13. parser.add_argument("session_name", type=str, help="Name of the session to use for Lapse images (e.g. marten_01)")
  14. parser.add_argument("--skip", type=int, help="Skip first n images", default=0)
  15. args = parser.parse_args()
  16. ds = Dataset(args.dataset_dir)
  17. session = ds.create_session(args.session_name)
  18. skip = args.skip
  19. if skip > 0:
  20. print(f"Skipping the first {skip} images...")
  21. normal = []
  22. anomalous = []
  23. motion_folder = session.get_motion_folder()
  24. quit = False
  25. # print(list_jpegs_recursive(motion_folder), motion_folder)
  26. for img_file in sorted(list_jpegs_recursive(motion_folder)):
  27. if skip > 0:
  28. skip -= 1
  29. continue
  30. img_nr = int(img_file[-9:-4])
  31. print(f"Labeling img #{img_nr} ({img_file})... ", end="")
  32. image = cv2.imread(os.path.join(motion_folder, img_file))
  33. cv2.imshow("labeler", image)
  34. # wait for user to press label or exit key
  35. while True:
  36. key = cv2.waitKey(0)
  37. if key == ord("1"):
  38. print("normal")
  39. normal.append(img_nr)
  40. elif key == ord("2"):
  41. print("anomalous")
  42. anomalous.append(img_nr)
  43. elif key == ord("x"):
  44. quit = True
  45. else:
  46. continue
  47. print(f"normal = {normal}")
  48. print(f"anomalous = {anomalous}")
  49. break
  50. if quit:
  51. break
  52. cv2.destroyAllWindows()
  53. print("Done.")
  54. print(f"normal = {normal}")
  55. print(f"anomalous = {anomalous}")
  56. if __name__ == "__main__":
  57. main()