Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  - extract - debug landmarks - fix non integer errror
  - training (gui) - fix mask toggling
  • Loading branch information
torzdf committed May 30, 2021
1 parent b27c05e commit 60a4231
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion scripts/fsmedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def process(self, extract_media):
for idx, face in enumerate(extract_media.detected_faces):
logger.trace("Drawing Landmarks. Frame: '%s'. Face: %s", frame, idx)
# Landmarks
for (pos_x, pos_y) in face.aligned.landmarks:
for (pos_x, pos_y) in face.aligned.landmarks.astype("int32"):
cv2.circle(face.aligned.face, (pos_x, pos_y), 1, (0, 255, 255), -1)
# Pose
center = tuple(np.int32((face.aligned.size / 2, face.aligned.size / 2)))
Expand Down
96 changes: 60 additions & 36 deletions scripts/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,6 @@ def _run_training_cycle(self, model, trainer):
if self._args.redirect_gui: # Remove any gui trigger files following an update
print("\n")
logger.info("[Preview Updated]")
for filename in self._gui_triggers.values():
if os.path.isfile(filename):
logger.debug("Removing gui trigger file: %s", filename)
os.remove(filename)

self._refresh_preview = False

if save_iteration:
Expand All @@ -360,12 +355,10 @@ def _monitor(self, thread):
bool
``True`` if there has been an error in the background thread otherwise ``False``
"""
is_preview = self._args.preview
preview_trigger_set = False
logger.debug("Launching Monitor")
logger.info("===================================================")
logger.info(" Starting")
if is_preview:
if self._args.preview:
logger.info(" Using live preview")
logger.info(" Press '%s' to save and quit",
"Stop" if self._args.redirect_gui or self._args.colab else "ENTER")
Expand All @@ -377,7 +370,7 @@ def _monitor(self, thread):
err = False
while True:
try:
if is_preview:
if self._args.preview:
with self._lock:
for name, image in self._preview_buffer.items():
cv2.imshow(name, image) # pylint: disable=no-member
Expand All @@ -394,21 +387,8 @@ def _monitor(self, thread):
break

# Preview Monitor
if is_preview and (cv_key == ord("\n") or cv_key == ord("\r")):
logger.debug("Exit requested")
if not self._preview_monitor(cv_key):
break
if is_preview and cv_key == ord("s"):
print("\n")
logger.info("Save requested")
self._save_now = True
if is_preview and cv_key == ord("r"):
print("\n")
logger.info("Refresh preview requested")
self._refresh_preview = True
if is_preview and cv_key == ord("m"):
print("\n")
logger.verbose("Toggle mask display requested")
self._toggle_preview_mask = True

# Console Monitor
if keypress.kbhit():
Expand All @@ -421,19 +401,7 @@ def _monitor(self, thread):
self._save_now = True

# GUI Preview trigger update monitor
if self._args.redirect_gui:
if os.path.isfile(self._gui_triggers["mask_toggle"]):
self._toggle_preview_mask = True

if not preview_trigger_set and os.path.isfile(self._gui_triggers["update"]):
print("\n")
logger.info("Refresh preview requested")
self._refresh_preview = True
preview_trigger_set = True

if preview_trigger_set and not self._refresh_preview:
logger.debug("Resetting GUI preview trigger")
preview_trigger_set = False
self._process_gui_triggers()

sleep(1)
except KeyboardInterrupt:
Expand All @@ -443,6 +411,62 @@ def _monitor(self, thread):
logger.debug("Closed Monitor")
return err

def _preview_monitor(self, key_press):
""" 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")
self._toggle_preview_mask = True

return True

def _process_gui_triggers(self):
""" Check whether a file drop has occurred from the GUI to manually update the preview. """
if not self._args.redirect_gui:
return

parent_flags = dict(mask_toggle="_toggle_preview_mask", update="_refresh_preview")
for trigger in ("mask_toggle", "update"):
filename = self._gui_triggers[trigger]
if os.path.isfile(filename):
logger.debug("GUI Trigger received for: '%s'", trigger)

logger.debug("Removing gui trigger file: %s", filename)
os.remove(filename)

if trigger == "update":
print("\n")
logger.info("Refresh preview requested")

setattr(self, parent_flags[trigger], True)

def _show(self, image, name=""):
""" Generate the preview and write preview file output.
Expand Down

0 comments on commit 60a4231

Please sign in to comment.