quick_label.py 2.1 KB

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