From f16dbe478ffd65a5aa8a753414e764ed80069e26 Mon Sep 17 00:00:00 2001 From: boltgolt Date: Sat, 9 Jan 2021 20:47:32 +0100 Subject: [PATCH] Gtk ui now works within rubberstamps --- howdy-gtk/src/authsticky.py | 4 ++-- src/compare.py | 25 +++++++++++++++++-------- src/dlib-data/Readme.md | 4 +++- src/rubberstamps/__init__.py | 26 ++++++++++++++++++++++++-- src/rubberstamps/nod.py | 2 ++ 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/howdy-gtk/src/authsticky.py b/howdy-gtk/src/authsticky.py index 2c0c590..e88c535 100644 --- a/howdy-gtk/src/authsticky.py +++ b/howdy-gtk/src/authsticky.py @@ -134,10 +134,10 @@ class StickyWindow(gtk.Window): if comm: # Parse a message if comm[0] == "M": - message = comm[2:] + message = comm[2:].strip() # Parse subtext if comm[0] == "S": - subtext = comm[2:] + subtext = comm[2:].strip() # Redraw the ui self.queue_draw() diff --git a/src/compare.py b/src/compare.py index e75e489..796b142 100644 --- a/src/compare.py +++ b/src/compare.py @@ -17,6 +17,7 @@ import configparser import dlib import cv2 import datetime +import atexit import subprocess import snapshot import numpy as np @@ -26,7 +27,7 @@ from recorders.video_capture import VideoCapture from i18n import _ -def exit(code): +def exit(code=None): """Exit while closeing howdy-gtk properly""" global gtk_proc @@ -35,7 +36,8 @@ def exit(code): gtk_proc.terminate() # Exit compare - sys.exit(code) + if code is not None: + sys.exit(code) def init_detector(lock): @@ -82,15 +84,15 @@ def send_to_ui(type, message): global gtk_proc # Only execute of the proccess started - if 'gtk_proc' in globals(): + if "gtk_proc" in globals(): # Format message so the ui can parse it message = type + "=" + message + " \n" # Try to send the message to the auth ui, but it's okay if that fails try: - gtk_proc.stdin.write(bytearray(message.encode("ascii"))) + gtk_proc.stdin.write(bytearray(message.encode("utf-8"))) gtk_proc.stdin.flush() - except IOError as err: + except IOError: pass @@ -122,10 +124,11 @@ face_detector = None pose_predictor = None face_encoder = None -# Start the auth ui +# Start the auth ui, register it to be always be closed on exit try: gtk_proc = subprocess.Popen(["howdy-gtk", "--start-auth-ui"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) -except FileNotFoundError as err: + atexit.register(exit) +except FileNotFoundError: pass # Write to the stdin to redraw ui @@ -337,7 +340,13 @@ while True: # Run rubberstamps if enabled if config.getboolean("rubberstamps", "enabled", fallback=False): import rubberstamps - rubberstamps.execute(config, { + + send_to_ui("S", "") + + if "gtk_proc" not in vars(): + gtk_proc = None + + rubberstamps.execute(config, gtk_proc, { "video_capture": video_capture, "face_detector": face_detector, "pose_predictor": pose_predictor, diff --git a/src/dlib-data/Readme.md b/src/dlib-data/Readme.md index a940a5f..ead2d9d 100644 --- a/src/dlib-data/Readme.md +++ b/src/dlib-data/Readme.md @@ -1,5 +1,7 @@ Download and unpack `dlib` data files from https://github.com/davisking/dlib-models repository: -```shell + +``` +shell wget https://github.com/davisking/dlib-models/raw/master/dlib_face_recognition_resnet_model_v1.dat.bz2 wget https://github.com/davisking/dlib-models/raw/master/mmod_human_face_detector.dat.bz2 wget https://github.com/davisking/dlib-models/raw/master/shape_predictor_5_face_landmarks.dat.bz2 diff --git a/src/rubberstamps/__init__.py b/src/rubberstamps/__init__.py index 79fc6c0..5b00d51 100644 --- a/src/rubberstamps/__init__.py +++ b/src/rubberstamps/__init__.py @@ -6,14 +6,36 @@ from importlib.machinery import SourceFileLoader class RubberStamp: + UI_TEXT = "ui_text" + UI_SUBTEXT = "ui_subtext" + def create_shorthands(self): self.video_capture = self.opencv["video_capture"] self.face_detector = self.opencv["face_detector"] self.pose_predictor = self.opencv["pose_predictor"] self.clahe = self.opencv["clahe"] + def set_ui_text(self, text, type=None): + typedec = "M" -def execute(config, opencv): + if type == self.UI_SUBTEXT: + typedec = "S" + + return self.send_ui_raw(typedec + "=" + text) + + def send_ui_raw(self, command): + if self.config.getboolean("debug", "verbose_stamps", fallback=False): + print("Sending command to howdy-gtk:") + print(" " + command) + + command += " \n" + + if self.gtk_proc: + self.gtk_proc.stdin.write(bytearray(command.encode("utf-8"))) + self.gtk_proc.stdin.flush() + + +def execute(config, gtk_proc, opencv): verbose = config.getboolean("debug", "verbose_stamps", fallback=False) dir_path = os.path.dirname(os.path.realpath(__file__)) installed_stamps = [] @@ -56,8 +78,8 @@ def execute(config, opencv): instance = constructor() instance.config = config + instance.gtk_proc = gtk_proc instance.opencv = opencv - print(regex_result.group(3)) instance.options = { "timeout": int(re.sub("[a-zA-Z]", "", regex_result.group(2))), diff --git a/src/rubberstamps/nod.py b/src/rubberstamps/nod.py index a2b208e..e4595e6 100644 --- a/src/rubberstamps/nod.py +++ b/src/rubberstamps/nod.py @@ -9,6 +9,8 @@ class nod(RubberStamp): self.options["min_directions"] = 3 def run(self): + self.set_ui_text("Authorised, nod to confirm", self.UI_TEXT) + last_reldist = -1 last_nosepoint = {"x": -1, "y": -1} recorded_nods = {"x": [], "y": []}