              ``True`` if there has been an error in the background thread otherwise ``False``
          """
          self._output_startup_info()
          keypress = KBHit(is_gui=self._args.redirect_gui)
          window_created = False
          err = False
          while True:
              try:
                  if self._args.preview:
                      self._do_preview(window_created)
 
</s> remove                     self._do_preview(window_created)
                    cv_key = cv2.waitKey(1000)  # pylint: disable=no-member
                else:
                    cv_key = None
 </s> add                     self._preview.display_preview() </s> remove     @classmethod
    def _create_resizable_window(cls, name: str, image_shape: tuple) -> None:
        """ Create a resizable OpenCV window to hold the preview image.

        Parameters
        ----------
        name: str
            The name to display in the window header and for window identification
        shape: tuple
            The (`rows`, `columns`, `channels`) of the image to be displayed
        """
        logger.debug("Creating named window '%s' for image shape %s", name, image_shape)
        height, width = image_shape[:2]
        cv2.namedWindow(name, cv2.WINDOW_GUI_EXPANDED)
        cv2.resizeWindow(name, width, height)

    def _do_preview(self, window_created: bool) -> bool:
        """" Display an image preview in a resizable window

        Parameters
        ----------
        window_created: bool
            ``True`` if a preview window has been created otherwise ``False``

        Returns
        -------
        bool
            ``True`` if a preview window has been created otherwise ``False``
        """
        with self._lock:
            for name, image in self._preview_buffer.items():
                if not window_created:
                    self._create_resizable_window(name, image.shape)
                cv2.imshow(name, image)  # pylint: disable=no-member
            window_created = bool(self._preview_buffer) if not window_created else window_created
        return window_created

 </s> add  </s> remove     def _preview_monitor(self, key_press: str) -> bool:
        """ Monitors keyboard presses on the pop-up OpenCV Preview Window.

        Parameters
        ----------
        key_press: str
            The key press received from OpenCV or ``None`` if no press received

        Returns
        -------
        bool
            ``True`` if the process should continue training. ``False`` if an exit has been
            requested and process should terminate
        """
        if not self._args.preview:
            return True

        if key_press == ord("\n") or key_press == ord("\r"):
            logger.debug("Exit requested")
            return False

        if key_press == ord("s"):
            print("\n")
            logger.info("Save requested")
            self._save_now = True
        if key_press == ord("r"):
            print("\n")
            logger.info("Refresh preview requested")
            self._refresh_preview = True
        if key_press == ord("m"):
            print("\n")
            logger.verbose("Toggle mask display requested")  # type:ignore
            self._toggle_preview_mask = True

        return True

 </s> add  </s> remove         self._toggle_preview_mask: bool = False
        self._refresh_preview: bool = False
        self._preview_buffer: Dict[str, np.ndarray] = {}
        self._lock = Lock()
 </s> add         self._preview = Preview() </s> remove         parent_flags = dict(mask_toggle="_toggle_preview_mask", update="_refresh_preview")
 </s> add         parent_flags = dict(mask_toggle="request_mask_toggle", update="request_refresh")
              ``True`` if there has been an error in the background thread otherwise ``False``
          """
          self._output_startup_info()
          keypress = KBHit(is_gui=self._args.redirect_gui)

          err = False
          while True:
              try:
                  if self._args.preview:
                      self._do_preview(window_created)
keep keep keep keep replace keep keep keep keep keep
 Live Preview - Replace cv2 with matplotlib viewer 
scripts/train.py