6
0

LabelDialog.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. from PyQt5 import uic, QtWidgets, QtCore
  3. from pycs.ui.AnnotatedInteractiveImageView import AnnotatedInteractiveImageView
  4. class LabelDialog(QtWidgets.QDialog):
  5. def __init__(self, predictions, **kwargs):
  6. # call parent constructor
  7. super(LabelDialog, self).__init__(**kwargs)
  8. # handle properties
  9. self.__predictions = predictions
  10. self.__current = -1
  11. # load ui
  12. spath = os.path.dirname(__file__)
  13. uic.loadUi(os.path.join(spath, 'LabelDialog.ui'), self)
  14. self.button_next.clicked.connect(self._next)
  15. self.button_previous.clicked.connect(self._prev)
  16. # add annotated image view
  17. self.__image = AnnotatedInteractiveImageView(self, self.click)
  18. self.gridLayout.addWidget(self.__image)
  19. # call next image function to display first item
  20. self._next()
  21. def _next(self):
  22. """
  23. displays next item
  24. """
  25. if len(self.__predictions) > 0:
  26. self.__current += 1
  27. if self.__current >= len(self.__predictions):
  28. self.__current = 0
  29. self.__image.display(self.__predictions[self.__current])
  30. def _prev(self):
  31. """
  32. displays previous item
  33. """
  34. if len(self.__predictions) > 0:
  35. self.__current -= 1
  36. if self.__current <= 0:
  37. self.__current = len(self.__predictions) - 1
  38. self.__image.display(self.__predictions[self.__current])
  39. def click(self, x, y, event):
  40. """
  41. handles clicks on images
  42. :param x: x position in [0, 1]
  43. :param y: y position in [0, 1]
  44. :param event: actual QMouseEvent object
  45. """
  46. # return if there is no prediction
  47. if len(self.__predictions) == 0:
  48. return
  49. # add label object to current prediction
  50. if 'label' not in self.__predictions[self.__current].keys():
  51. self.__predictions[self.__current]['label'] = {
  52. 'faces': [{
  53. 'x1': None,
  54. 'y1': None,
  55. 'x2': None,
  56. 'y2': None
  57. }]
  58. }
  59. face = self.__predictions[self.__current]['label']['faces'][0]
  60. # set data depending on click
  61. if event.button() == QtCore.Qt.LeftButton:
  62. face['x1'] = x
  63. face['y1'] = y
  64. elif event.button() == QtCore.Qt.RightButton:
  65. face['x2'] = x
  66. face['y2'] = y
  67. # return if not at least two points selected
  68. if face['x1'] is None or face['y1'] is None or face['x2'] is None or face['y2'] is None:
  69. return
  70. # detect upper left point and calculate width and height
  71. face['x'] = min(face['x1'], face['x2'])
  72. face['y'] = min(face['y1'], face['y2'])
  73. face['w'] = max(face['x1'], face['x2']) - face['x']
  74. face['h'] = max(face['y1'], face['y2']) - face['y']
  75. # display rectangle
  76. self.__image.display(self.__predictions[self.__current], mode='label')