File size: 2,440 Bytes
5a26b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Face Recognition Attendance System (Windows + Linux).   python main.py [--config config/settings.json] [--no-login]"""
import argparse, logging, os, sys
if sys.stdout is None or sys.stderr is None:  # pythonw.exe has no console: give print/tqdm (model download) a sink
    sys.stdout, sys.stderr = sys.stdout or open(os.devnull, "w"), sys.stderr or open(os.devnull, "w")
os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "TRUE")  # Windows: tolerate two OpenMP runtimes (torch + others) in one process
from logging.handlers import RotatingFileHandler
from PyQt5.QtCore import Qt, QThread
from PyQt5.QtWidgets import QApplication, QMessageBox
from core import BASE, WINDOWS, Config


def on_error(kind, err, tb):
    """sys.excepthook: PyQt aborts the whole app on an error inside a slot. Log it instead (pythonw has no console) and tell the user."""
    logging.getLogger("ams").error("Unexpected error", exc_info=(kind, err, tb))
    app = QApplication.instance()
    if app and QThread.currentThread() == app.thread() and not isinstance(app.activeModalWidget(), QMessageBox):  # GUI thread, one box at a time
        QMessageBox.critical(app.activeModalWidget(), "FaceAttend", "Unexpected error: %s\n\nDetails are in logs/app.log" % err)


def main():
    ap = argparse.ArgumentParser(); ap.add_argument("--config", default=os.path.join(BASE, "config", "settings.json"))
    ap.add_argument("--no-login", action="store_true"); a = ap.parse_args()
    cfg = Config(a.config)
    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s", handlers=[logging.StreamHandler(),
                        RotatingFileHandler(os.path.join(cfg.dir("logs"), "app.log"), maxBytes=5 << 20, backupCount=5, encoding="utf-8")])
    sys.excepthook = on_error
    if WINDOWS:  # own taskbar entry/icon instead of python.exe's
        import ctypes; ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("FaceAttend.Attendance")
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling); QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    app = QApplication(sys.argv); app.setStyle("Fusion"); app.setApplicationName("FaceAttend")
    import gui
    app.setWindowIcon(gui.avatar("Face Attend", "", 0, 64))
    if not a.no_login and not gui.login(cfg): return 0
    ctx = gui.Ctx(cfg); win = gui.Main(ctx); win.show(); ctx.load_engine()
    return app.exec_()


if __name__ == "__main__":
    sys.exit(main())