Article Directory
1. Real-time data transmission
Summary three: face classroom sign-in management system (summary three) Baidu face detection API call
In a real situation, the information collection and transmission of face data should be real-time, instead of transmitting a picture in the form of a dialog box, so we need to associate the data collected by the camera with the sending network request.
Two, the core code
-
The accesstoken value is obtained when the window is created, and the initialization function ( __init__ ) in the myWindow class is added:
self.get_accesstoken()
-
Create a thread class ( detectThread ) to use and execute post requests for face detection
When sending a network request and waiting for the return result, there will be a certain waiting time, the program will be blocked here, and there will be a certain degree of lag
Use multithreading to solve the stuck phenomenon
a.
The accesstoken value should be obtained when the object is initializeddef __init__(self, token): super(detectThread, self).__init__() self.access_token = token
b.
Rewrite the run function (the end of the function execution will represent the end of the thread)def run(self): print("run") self.exec_()
c.
Get the picture data of the window at a certain moment and send a network request:def detect_data(self, base64_image): # 请求地址 request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" # 请求参数 params = { "image": base64_image, "image_type": "BASE64", "face_field": "age,beauty,gender,glasses,face_type" } # 获取的access_token access_token = self.access_token # 合并的网络请求地址 request_url = request_url + "?access_token=" + access_token # 通过json格式请求 headers = { 'content-type': 'application/json'} # 发送网络post请求,就会存在一定的返回结果等待时间,程序就会阻塞,存在一定的卡顿现象 response = requests.post(request_url, data=params, headers=headers) if response: data = response.json() print(data)
Parameter base64_image : image data at a certain moment passed from the window
A certain moment refers to the time to send the network, if a network request is sent every 1000ms, the parameters will only be passed every 1000ms
-
Modify the myWindow class
a.
The general flow of real-time transmission of picture data between the main window and the threadb.
Customize a signal and slotdetect_data_signal = pyqtSignal(str)
c.
Create a thread for sending face requests, and associate the custom signal with the detect_data functiondef create_thread(self): self.detect_thread = detectThread(self.access_token) self.detect_thread.start() self.detect_data_signal.connect(self.detect_thread.detect_data)
d.
Modify on_actionopen functiondef on_actionopen(self): # 启动摄像头 self.camera_data = camera() # 启动定时器,进行定时,每个多长时间获取摄像头信号进行显示 self.timeshow = QTimer(self) self.timeshow.start(50) # 计数 self.count = 0 # 每隔50毫秒将产生一个信号timeout self.timeshow.timeout.connect(self.show_camera) # 每8000ms调用一次人脸检测 # 创建检测线程 self.create_thread() # 获取要检测的数据 self.detection_time = QTimer(self) self.detection_time.start(8000) self.detection_time.timeout.connect(self.get_cameradata)
When you click "Start sign-in", a new thread is created and camera data is obtained every 8000ms (call the get_cameradata function)
e.
Create a new get_cameradata function to obtain camera information and send a custom signaldef get_cameradata(self): # 摄像头获取画面 camera_img = self.camera_data.read_camera() # 把摄像头画面转换成图片 _, enc = cv2.imencode('.jpg', camera_img) # 设置为base64编码格式 base64_image = base64.b64encode(enc.tobytes()) # 产生信号,传递数据 self.detect_data_signal.emit(str(base64_image))
Three, the effect is as follows
It can be found that there are still some lags in the middle. The reason is that a signal is generated by the window to directly call the network request detection function ( detect_data ) in the thread.
The results returned are as follows:
{
'error_code': 222203, 'error_msg': 'image check fail', 'log_id': 1545256500165, 'timestamp': 1603251891, 'cached': 0, 'result': None}
There is a return result, indicating that there is no problem with network communication in this way. The error in the returned result is due to an error in the base64_image format. At the same time, there are still some small problems, save it for the next summary!
Summary 5 Portal: Face Classroom Sign-in Management System (Summary 5) Solve the problem of screen freeze