Face Classroom Sign-in Management System (Summary Five) Solve the problem of screen freeze

1. Solve the problem of screen freeze

Summary Four: Face Classroom Sign-in Management System (Summary Four) Real-time data transmission

The screen still has the cause of the freeze : the signal is generated by the window, but the signal is directly related to the network request detection function in the thread, which is equivalent to being directly called by the window, so the jam

Solution : Define a function to read screen data in the thread and set it as a slot function, so that the signal of the window is associated with the slot function of the thread for data transfer, and not directly for network requests. Network requests are in thread the run function

Two, the core code

  1. Create a new function get_base64 in the thread class ( detectThread ) to receive the read screen data

        def get_base64(self, base64_image):
            # 当窗口产生信号,调用该槽函数,将传递数据存放在线程变量中
            self.base64_img = base64_image
            self.condition = True
    
  2. Rewrite the run function in the thread class ( detectThread ), and cyclically listen for whether to send network requests

        def run(self):
            while self.flag:
                if self.condition:
                    self.detect_data(self.base64_img)
                    self.condition = False
    
  3. The fourth summary also mentioned the problem of the wrong return result, which is mainly the problem of the wrong type of the transmitted picture data. The transmitted data type should be bytes , not the string type. Redefine the signal and slot.

    detect_data_signal = pyqtSignal(bytes)
    

    The generated signal and the data passed should also be of type bytes

    self.detect_data_signal.emit(bytes(base64_image))

  4. When closing the sign-in ( on_actionclose ), the timer used to open the sign-in ( on_actionopen ) should be turned off, otherwise the program will crash and exit

        def on_actionclose(self):
            # 关闭摄像头
            self.camera_data.camera_close()
            # 关闭定时器
            self.timeshow.stop()
            self.detection_time.stop()
            # 断开连接
            self.timeshow.timeout.disconnect(self.show_camera)
            self.detection_time.timeout.disconnect(self.get_cameradata)
            self.detect_thread.transmit_data.disconnect(self.get_detectdata)
            self.detect_data_signal.disconnect(self.detect_thread.get_base64)
            # 关闭线程
            self.detect_thread.flag = False
            self.detect_thread.quit()
            self.detect_thread.wait()
    

    Tip: Disconnect can be omitted, because the timer is turned off, there will be no information associated slot function; however, when you click "Start sign-in" again, a new timer object, a new timer object, will be redefined Re-associate the slot function, the previous timer will be automatically recycled by python.

  5. The effect is as follows:

Three, create a face library

  1. You can view your own face library in the face recognition-application list

  2. Implementation code

        def add_group(self):
            # 创建输入对话框
            group, ret = QInputDialog.getText(self, "添加用户组", "输入id(由数字、字母、下划线组成)")
            request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add"
            params = {
          
          
                "group_id": group
            }
            access_token = self.access_token
            request_url = request_url + "?access_token=" + access_token
            headers = {
          
          'content-type': 'application/json'}
            response = requests.post(request_url, data=params, headers=headers)
            if response:
                message = response.json()
                if message['error_code'] == 0:
                    QMessageBox.information(self, "addgroup", "添加成功!")
                else:
                    QMessageBox.warning(self, "addgroup", "添加失败!")
    

    Technical Document: Create User Group

  3. The effect is as follows:

Summary Six Portal: Face Classroom Sign-in Management System (Summary Six) to add and delete users

Guess you like

Origin blog.csdn.net/xwmrqqq/article/details/109249866