ImageAnnotator.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) 2023 Felix Kleinsteuber and Computer Vision Group, Friedrich Schiller University Jena
  2. import ipywidgets as widgets
  3. from IPython.display import display
  4. from py.Session import Session
  5. from py.ImageClassifier import AbstractImageClassifier
  6. import numpy as np
  7. # Old annotator script using IPython widgets, which are very slow and sometimes buggy.
  8. # It is preferred to use quick_label.py.
  9. class ImageAnnotator():
  10. def __init__(self, classifier: AbstractImageClassifier, session: Session, initial_scores = [], initial_annotations = [], load_from = None):
  11. self.scores = initial_scores
  12. self.annotations = initial_annotations
  13. self.score = -1
  14. self.classifier = classifier
  15. self.session = session
  16. if load_from is not None:
  17. data = np.load(load_from, allow_pickle=True)
  18. self.annotations = data[0]
  19. self.scores = data[1]
  20. normal_btn = widgets.Button(description = "Normal")
  21. anomalous_btn = widgets.Button(description = "Anomalous")
  22. self.button_box = widgets.HBox([normal_btn, anomalous_btn])
  23. self.output = widgets.Output(layout={"height": "400px"})
  24. display(self.button_box, self.output)
  25. normal_btn.on_click(self.mark_as_normal)
  26. anomalous_btn.on_click(self.mark_as_anomalous)
  27. self.next_image()
  28. # Click on normal button
  29. def mark_as_normal(self, _):
  30. with self.output:
  31. print("Marking as normal...")
  32. self.annotations.append(True)
  33. self.scores.append(self.score)
  34. self.next_image()
  35. # Click on anomalous button
  36. def mark_as_anomalous(self, _):
  37. with self.output:
  38. print("Marking as anomalous...")
  39. self.annotations.append(False)
  40. self.scores.append(self.score)
  41. self.next_image()
  42. # Show next image
  43. def next_image(self):
  44. img = self.session.get_random_motion_image(day_only=True)
  45. self.score = self.classifier.evaluate(img)
  46. self.output.clear_output()
  47. with self.output:
  48. display(img.to_ipython_image())
  49. print(f"score = {self.score}")
  50. # Save annotation data to file
  51. def save(self, filename: str):
  52. np.save(filename, [self.annotations, self.scores])