Beispiel #1
0
    def __init__(self):
        super(Demo, self).__init__()
        self.setupUi(self)

        self.save_ui = Save_demo()
        self.code_box = CodeBox()
        self.myrecognition = Face_recognition()
        # self.lock = MyLock()
        self.activation_timer = QTimer()
        self.face_recognition_timer = QTimer()

        self.slot_init()

        self.current_photo = ''

        self.save_button_init()
        self.unlock_button_init()
        self.delete_button_init()
        self.activation_timer_init()

        # 人脸识别模块初始化
        self.myrecognition.turn_on_camera()
        self.myrecognition.get_known_face_from_dir()

        self.face_recognition_timer_init()
 def __init__(self):
     super(Demo, self).__init__()
     self.setupUi(self)
     self.timer_camera = QTimer()  #初始化定时器
     self.cap = cv2.VideoCapture()  #初始化摄像头
     self.CAM_NUM = 0
     self.slot_init()
     self.save_photo_flag = False
     self.save_button.setEnabled(False)
     self.save_ui = Save_demo()
Beispiel #3
0
    def __init__(self):
        super(Demo, self).__init__()
        self.setupUi(self)

        self.save_ui = Save_demo()
        self.myrecognition = Face_recognition()
        self.mylock = MyLock()
        self.activation_timer = QTimer()

        self.slot_init()

        self.current_photo = ''
        self.save_photo_flag = False

        self.save_button_init()
        self.unlock_button_init()
        self.delete_button_init()
        self.activation_timer_init()

        self.recognition_thread = Thread(target=self.run_face_recognition)
        self.recognition_thread.start()
class Demo(QWidget, Ui_Form):
    def __init__(self):
        super(Demo, self).__init__()
        self.setupUi(self)
        self.timer_camera = QTimer()  #初始化定时器
        self.cap = cv2.VideoCapture()  #初始化摄像头
        self.CAM_NUM = 0
        self.slot_init()
        self.save_photo_flag = False
        self.save_button.setEnabled(False)
        self.save_ui = Save_demo()

    def slot_init(self):
        self.timer_camera.timeout.connect(self.show_camera)
        # 信号和槽连接
        self.enable_camera_button.clicked.connect(self.slotCameraButton)
        self.save_button.clicked.connect(self.savePhoto)

    # 在Label显示图片的核心方法
    def show_camera(self):
        flag, self.image = self.cap.read()
        if self.save_photo_flag:
            self.save_ui.image = self.image
            self.save_ui.show_picture()
            self.save_ui.exec_()
            self.save_photo_flag = False
        show = cv2.resize(self.image,
                          None,
                          fx=1.3,
                          fy=1.3,
                          interpolation=cv2.INTER_CUBIC)
        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
        showImage = QImage(show.data, show.shape[1], show.shape[0],
                           QImage.Format_RGB888)
        self.label.setPixmap(QPixmap.fromImage(showImage))

    # 打开关闭摄像头控制
    def slotCameraButton(self):
        if self.timer_camera.isActive() == False:
            # 打开摄像头并显示图像信息
            self.openCamera()
            self.save_button.setEnabled(True)
        else:
            # 关闭摄像头并清空显示信息
            self.closeCamera()
            self.save_button.setEnabled(False)

    # 打开摄像头
    def openCamera(self):
        flag = self.cap.open(self.CAM_NUM)
        if flag == False:
            msg = QMessageBox.Warning(self,
                                      u'Warning',
                                      u'请检测相机与电脑是否连接正确',
                                      buttons=QMessageBox.Ok,
                                      defaultButton=QMessageBox.Ok)
        else:
            self.timer_camera.start(30)  # 30ms调用一次show_camera
            self.enable_camera_button.setText('关闭摄像头')

    # 关闭摄像头
    def closeCamera(self):
        self.timer_camera.stop()
        self.cap.release()
        self.label.clear()
        self.enable_camera_button.setText('打开摄像头')

    # 保存图片
    def savePhoto(self):
        self.save_photo_flag = True
Beispiel #5
0
class Demo(QWidget, Ui_Form):
    recognition_ready_signal = pyqtSignal()
    start_timer_signal = pyqtSignal()

    def __init__(self):
        super(Demo, self).__init__()
        self.setupUi(self)

        self.save_ui = Save_demo()
        self.code_box = CodeBox()
        self.myrecognition = Face_recognition()
        # self.lock = MyLock()
        self.activation_timer = QTimer()

        self.slot_init()

        self.current_photo = ''
        self.save_photo_flag = False

        self.save_button_init()
        self.unlock_button_init()
        self.delete_button_init()
        self.activation_timer_init()

        self.recognition_thread = Thread(target=self.run_face_recognition)
        self.recognition_thread.start()

    def slot_init(self):
        # 信号和槽连接
        self.recognition_ready_signal.connect(self.show_picture)
        self.save_button.clicked.connect(self.savePhoto)
        self.activation_timer.timeout.connect(self.deactivate_button)
        self.start_timer_signal.connect(self.start_timer)
        self.delete_button.clicked.connect(self.go_delete_photo)
        self.code_box_button.clicked.connect(self.run_code_box)
        self.code_box.login_successfully.connect(self.login_successfully)

    def save_button_init(self):
        # 如果没有保存人,则保存按键可以点击,否则不能点击
        if self.myrecognition.is_dir_empty():
            self.save_button.setEnabled(True)
        else:
            self.save_button.setEnabled(False)

    def unlock_button_init(self):
        self.unlock_button.setEnabled(False)

    def delete_button_init(self):
        self.delete_button.setEnabled(False)

    def activation_timer_init(self):
        self.activation_timer.setSingleShot(True)

    def run_face_recognition(self):
        self.myrecognition.turn_on_camera()
        self.myrecognition.get_known_face_from_dir()
        while True:
            self.myrecognition.face_recognition()
            # 检测是否有识别出的任务,有则运行按键按下
            self.count = self.myrecognition.found_known_people_number()
            if self.count != 0:
                self.activate_button()
                self.start_timer_signal.emit()
            # 检测是否按下保存
            if self.save_photo_flag:
                self.photo_to_save = self.myrecognition.frame.copy()
                self.save_photo_flag = False
            self.myrecognition.add_mark()
            self.current_photo = self.myrecognition.frame
            self.recognition_ready_signal.emit()

    def show_picture(self):
        show = cv2.resize(self.current_photo, None, fx=1.3, fy=1.3, interpolation=cv2.INTER_CUBIC)
        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
        showImage = QImage(show.data, show.shape[1], show.shape[0], QImage.Format_RGB888)
        self.label.setPixmap(QPixmap.fromImage(showImage))


    # 保存图片
    def savePhoto(self):
        self.save_photo_flag = True
        while self.save_photo_flag:
            pass
        self.save_ui.image = self.photo_to_save
        self.save_ui.show_picture()
        self.save_ui.exec_()
        # 刷新已经记录的人
        self.reload_people()

    def reload_people(self):
        # 刷新存的人的资料
        self.myrecognition.get_known_face_from_dir()

    def start_timer(self):
        self.activation_timer.start(4000)

    def activate_button(self):
        if not self.activation_timer.isActive():
            self.save_button.setEnabled(True)
            self.delete_button.setEnabled(True)
            self.unlock_button.setEnabled(True)
            self.message.setText("认证成功")

    def deactivate_button(self):
        if self.myrecognition.is_dir_empty():
            self.save_button.setEnabled(True)
        else:
            self.save_button.setEnabled(False)
        self.delete_button.setEnabled(False)
        self.unlock_button.setEnabled(False)
        self.message.setText("欢迎^_^")

    def go_delete_photo(self):
        delete_page = DeletePage()
        delete_page.exec_()
        # 刷新已经记录的人
        self.reload_people()
        # 检测人是否空了,如果空了,要允许保存
        if self.myrecognition.is_dir_empty():
            self.save_button.setEnabled(True)

    def run_code_box(self):
        self.code_box.exec_()

    def login_successfully(self):
        self.activate_button()
        self.start_timer_signal.emit()