Python | 人脸识别系统 —— 管理员操作

本博客为人脸识别系统的管理员操作代码解释

人脸识别系统博客汇总:人脸识别系统-博客索引

项目GitHub地址:Su-Face-Recognition: A face recognition for user logining

注意:阅读本博客前请先参考以下博客

工具安装、环境配置:人脸识别系统-简介

UI界面设计:人脸识别系统-UI界面设计

UI事件处理:人脸识别系统-UI事件处理

一、数据库操作

# -*- coding: utf-8 -*-
import pymysql


def init_conn():
    conn = pymysql.connect(
        host="127.0.0.1",  # 数据库的IP地址
        user="root",  # 数据库用户名称
        password="root",  # 数据库用户密码
        db="contest",  # 数据库名称
        port=3306,  # 数据库端口名称
        charset="utf8"  # 数据库的编码方式
    )
    return conn


def execute_with_bool(sql_str, args=()):
    conn = init_conn()
    cursor = conn.cursor()
    try:
        cursor.execute(sql_str, args)
        conn.commit()
        return True
    except Exception as e:
        conn.rollback()
        print(e)
        return False
    finally:
        cursor.close()


def execute_with_list(sql_str):
    conn = init_conn()
    cursor = conn.cursor()
    results = []
    try:
        cursor.execute(sql_str)
        results = cursor.fetchall()
    except Exception as e:
        conn.rollback()
        print(e)
    finally:
        cursor.close()
    return results


def search_by_name(name):
    return execute_with_list("select * from admin where name = " + name)


def delete_by_name(name):
    return execute_with_bool("delete from warn where name = %s", name)

二、管理员登录

# 管理员登录界面
class LoginWindow(QDialog, AdminLoginUi):
    def __init__(self, parent=None):
        super(LoginWindow, self).__init__(parent)
        self.setupUi(self)

        self.minimize_button.clicked.connect(self.showMinimized)
        self.close_button.clicked.connect(self.cancel_login)

        self.confirm_button.clicked.connect(self.search_user)
        self.cancel_button.clicked.connect(self.cancel_login)

    # 点击确认,搜索管理员
    def search_user(self):
        input_name = self.name_lineEdit.text()
        input_password = self.password_lineEdit.text()

        if input_name == "":
            QMessageBox.about(self, '提示', '姓名不能为空')
        elif input_password == "":
            QMessageBox.about(self, '提示', '密码不能为空')
        else:
            row = AdminSqlUtil.search_by_name("\"" + input_name + "\"")
            if row:
                result = row[0]
                password = result[1]
                if input_password != password:
                    QMessageBox.about(self, '提示', '密码输入错误')
                else:
                    global ADMIN_LOGIN_FLAG
                    ADMIN_LOGIN_FLAG = True
                    QMessageBox.about(self, '提示', '登录成功')
                    self.close_window()
            else:
                QMessageBox.about(self, '提示', '该用户不存在')

    # 点击取消按钮
    def cancel_login(self):
        global ADMIN_LOGIN_FLAG
        ADMIN_LOGIN_FLAG = False
        self.close_window()

    # 关闭窗口
    def close_window(self):
        self.name_lineEdit.setPlaceholderText("请输入姓名")
        self.password_lineEdit.setPlaceholderText("请输入密码")
        self.close()

三、用户解锁

# 用户解锁
class UnloadWindow(QDialog, AdminUnloadUi):
    def __init__(self, parent=None):
        super(UnloadWindow, self).__init__(parent)
        self.setupUi(self)

        self.minimize_button.clicked.connect(self.showMinimized)
        self.close_button.clicked.connect(self.close_window)

        self.confirm_button.clicked.connect(self.unload_user)
        self.cancel_button.clicked.connect(self.close_window)

    # 解锁用户
    def unload_user(self):
        input_name = self.name_lineEdit.text()
        input_password = self.password_lineEdit.text()

        if input_name == "":
            QMessageBox.about(self, '提示', '姓名不能为空')
        elif input_password == "":
            QMessageBox.about(self, '提示', '密码不能为空')
        else:
            row = UserSqlUtil.search_by_name("\"" + input_name + "\"")
            if row:
                result = row[0]
                password = result[1]
                if input_password != password:
                    QMessageBox.about(self, '提示', '密码输入错误')
                else:
                    if AdminSqlUtil.delete_by_name(row[0]):
                        QMessageBox.about(self, '提示', '解锁成功')
                    else:
                        QMessageBox.about(self, '提示', '解锁失败,请重试')
                    self.close_window()
            else:
                QMessageBox.about(self, '提示', '该用户不存在')

    # 关闭窗口
    def close_window(self):
        self.name_lineEdit.setPlaceholderText("请输入姓名")
        self.password_lineEdit.setPlaceholderText("请输入密码")
        self.close()

四、用户删除

# 用户删除
class DeleteWindow(QDialog, AdminDeleteUi):
    def __init__(self, parent=None):
        super(DeleteWindow, self).__init__(parent)
        self.setupUi(self)

        self.minimize_button.clicked.connect(self.showMinimized)
        self.close_button.clicked.connect(self.close_window)

        self.confirm_button.clicked.connect(self.user_delete)
        self.cancel_button.clicked.connect(self.close_window)

    def user_delete(self):
        input_name = self.name_lineEdit.text()
        input_password = self.password_lineEdit.text()

        if input_name == "":
            QMessageBox.about(self, '提示', '姓名不能为空')
        elif input_password == "":
            QMessageBox.about(self, '提示', '密码不能为空')
        else:
            row = UserSqlUtil.search_by_name("\"" + input_name + "\"")
            if row:
                result = row[0]
                password = result[1]
                if input_password != password:
                    QMessageBox.about(self, '提示', '密码输入错误')
                else:
                    flag = UserSqlUtil.delete_by_name(input_name)
                    if flag:
                        QMessageBox.about(self, '提示', '删除成功')
                    else:
                        QMessageBox.about(self, '提示', '删除失败,请重试')
                    self.close_window()
            else:
                QMessageBox.about(self, '提示', '该用户不存在')

    # 关闭窗口
    def close_window(self):
        self.name_lineEdit.setPlaceholderText("请输入姓名")
        self.password_lineEdit.setPlaceholderText("请输入密码")
        self.close()

 阅读完本博客后可以继续阅读:

摄像头画面展示:人脸识别系统-摄像头画面展示

用户端逻辑:

  • 人脸识别:Python | 人脸识别系统 — 人脸识别
  • 活体检测:Python | 人脸识别系统 — 活体检测
  • 背景模糊:Python | 人脸识别系统 — 背景模糊
  • 姿态检测:Python | 人脸识别系统 — 姿态检测
  • 人脸比对:Python | 人脸识别系统 — 人脸比对
  • 用户操作:Python | 人脸识别系统 — 用户操作

注:以上代码仅为参考,若需要运行,请参考项目GitHub完整源代码: Su-Face-Recognition: A face recognition for user logining

你可能感兴趣的:(Python,python,pyqt,mysql,pycharm)