From db3ee86bc2518dfcfe8a8e20f9c94e146d5e5a9a Mon Sep 17 00:00:00 2001 From: boltgolt Date: Fri, 8 Jan 2021 18:49:58 +0100 Subject: [PATCH] Added video tab to window --- howdy-gtk/src/main.glade | 191 +++++++++++++++++++++++++++++++++---- howdy-gtk/src/tab_video.py | 79 +++++++++++++++ howdy-gtk/src/window.py | 13 ++- src/cli/test.py | 2 +- src/compare.py | 2 +- 5 files changed, 264 insertions(+), 23 deletions(-) create mode 100644 howdy-gtk/src/tab_video.py diff --git a/howdy-gtk/src/main.glade b/howdy-gtk/src/main.glade index 8733770..7cb12e7 100644 --- a/howdy-gtk/src/main.glade +++ b/howdy-gtk/src/main.glade @@ -5,16 +5,19 @@ True False + 5 gtk-add True False + 5 gtk-add True False + 5 gtk-delete @@ -24,10 +27,13 @@ center logo.png - + True True + 2 + left False + True @@ -37,9 +43,9 @@ True False - 5 - 5 - 5 + 10 + 10 + 10 5 @@ -72,6 +78,7 @@ True True True + 15 iconadduser none True @@ -95,15 +102,15 @@ True False - 5 - 5 + 10 + 10 vertical True False - 5 - 5 + 10 + 10 False @@ -122,9 +129,9 @@ True False - 5 - 7 - 5 + 10 + 8 + 10 @@ -152,7 +159,7 @@ True True True - 5 + 11 icondelete True @@ -178,7 +185,8 @@ True False - 2 + 10 + 10 Models @@ -190,21 +198,167 @@ True False - vertical + 10 + 10 + 10 + 10 - + True False - label + 10 + vertical + + + True + False + start + Camera ID: + + + + + + False + True + 0 + + + + + True + False + start + True + end + + + False + True + 1 + + + + + True + False + start + 10 + Real resolution: + + + + + + False + True + 2 + + + + + True + False + start + True + + + False + True + 3 + + + + + True + False + start + 10 + Used resolution: + + + + + + False + True + 4 + + + + + True + False + start + True + + + False + True + 5 + + + + + True + False + start + 10 + Recorder: + + + + + + False + True + 6 + + + + + True + False + start + True + + + False + True + 7 + + + + + False True + end 0 - + + 300 + 300 + True + False + + + True + False + gtk-execute + 6 + + + + + False + True + 1 + @@ -229,12 +383,13 @@ True False + center + center vertical True False - 40 20 12 logo_about.png diff --git a/howdy-gtk/src/tab_video.py b/howdy-gtk/src/tab_video.py new file mode 100644 index 0000000..cd67740 --- /dev/null +++ b/howdy-gtk/src/tab_video.py @@ -0,0 +1,79 @@ +import configparser + +from i18n import _ + +from gi.repository import Gtk as gtk +from gi.repository import Gdk as gdk +from gi.repository import GdkPixbuf as pixbuf +from gi.repository import GObject as gobject + +MAX_HEIGHT = 300 +MAX_WIDTH = 300 + + +def on_page_switch(self, notebook, page, page_num): + if page_num == 1: + path = "/dev/video1" + + try: + self.config = configparser.ConfigParser() + self.config.read("/lib/security/howdy/config.ini") + except Exception: + print(_("Can't open camera")) + + try: + # if not self.cv2: + import cv2 + self.cv2 = cv2 + except Exception: + print(_("Can't import OpenCV2")) + + try: + self.capture = cv2.VideoCapture(self.config.get("video", "device_path")) + except Exception: + print(_("Can't open camera")) + + opencvbox = self.builder.get_object("opencvbox") + opencvbox.modify_bg(gtk.StateType.NORMAL, gdk.Color(red=0, green=0, blue=0)) + + height = self.capture.get(self.cv2.CAP_PROP_FRAME_HEIGHT) or 1 + width = self.capture.get(self.cv2.CAP_PROP_FRAME_WIDTH) or 1 + + self.scaling_factor = (MAX_HEIGHT / height) or 1 + + if width * self.scaling_factor > MAX_WIDTH: + self.scaling_factor = (MAX_WIDTH / width) or 1 + + config_height = self.config.getfloat("video", "max_height", fallback=0.0) + config_scaling = (config_height / height) or 1 + + self.builder.get_object("videoid").set_text(path.split("/")[-1]) + self.builder.get_object("videores").set_text(str(int(width)) + "x" + str(int(height))) + self.builder.get_object("videoresused").set_text(str(int(width * config_scaling)) + "x" + str(int(height * config_scaling))) + self.builder.get_object("videorecorder").set_text(self.config.get("video", "recording_plugin", fallback=_("Unknown"))) + + gobject.timeout_add(10, self.capture_frame) + + elif self.capture is not None: + self.capture.release() + self.capture = None + + +def capture_frame(self): + if self.capture is None: + return + + ret, frame = self.capture.read() + + frame = self.cv2.resize(frame, None, fx=self.scaling_factor, fy=self.scaling_factor, interpolation=self.cv2.INTER_AREA) + + retval, buffer = self.cv2.imencode(".png", frame) + + loader = pixbuf.PixbufLoader() + loader.write(buffer) + loader.close() + buffer = loader.get_pixbuf() + + self.opencvimage.set_from_pixbuf(buffer) + + gobject.timeout_add(20, self.capture_frame) diff --git a/howdy-gtk/src/window.py b/howdy-gtk/src/window.py index 063adb1..1bd58ff 100644 --- a/howdy-gtk/src/window.py +++ b/howdy-gtk/src/window.py @@ -1,5 +1,4 @@ # Opens and controls main ui window -import cairo import gi import signal import sys @@ -15,7 +14,6 @@ gi.require_version("Gdk", "3.0") # Import them from gi.repository import Gtk as gtk -from gi.repository import Gdk as gdk class MainWindow(gtk.Window): @@ -34,6 +32,10 @@ class MainWindow(gtk.Window): self.window = self.builder.get_object("mainwindow") self.userlist = self.builder.get_object("userlist") self.modellistbox = self.builder.get_object("modellistbox") + self.opencvimage = self.builder.get_object("opencvimage") + + # Init capture for video tab + self.capture = None # Create a treeview that will list the model data self.treeview = gtk.TreeView() @@ -62,7 +64,6 @@ class MainWindow(gtk.Window): self.userlist.set_active(0) self.window.show_all() - # self.resize(300, 300) # Start GTK main loop gtk.main() @@ -102,6 +103,9 @@ class MainWindow(gtk.Window): def exit(self, widget, context): """Cleanly exit""" + if self.capture is not None: + self.capture.release() + gtk.main_quit() sys.exit(0) @@ -125,6 +129,9 @@ MainWindow.on_user_add = tab_models.on_user_add MainWindow.on_user_change = tab_models.on_user_change MainWindow.on_model_add = tab_models.on_model_add MainWindow.on_model_delete = tab_models.on_model_delete +import tab_video +MainWindow.on_page_switch = tab_video.on_page_switch +MainWindow.capture_frame = tab_video.capture_frame # Open the GTK window window = MainWindow() diff --git a/src/cli/test.py b/src/cli/test.py index c62fcef..37d5900 100644 --- a/src/cli/test.py +++ b/src/cli/test.py @@ -54,7 +54,7 @@ use_cnn = config.getboolean('core', 'use_cnn', fallback=False) if use_cnn: face_detector = dlib.cnn_face_detection_model_v1( - path + '/../dlib-data/mmod_human_face_detector.dat' + path + "/../dlib-data/mmod_human_face_detector.dat" ) else: face_detector = dlib.get_frontal_face_detector() diff --git a/src/compare.py b/src/compare.py index 770b75a..48a0172 100644 --- a/src/compare.py +++ b/src/compare.py @@ -29,7 +29,7 @@ def exit(code): global gtk_proc # Exit the auth ui process if there is one - if 'gtk_proc' in globals(): + if "gtk_proc" in globals(): gtk_proc.terminate() # Exit compare