目录
4. 定义函数database_features_construct:
首先,发送一条消息,告知服务器端要进行人脸数据库特征构建
message_to_send = {'message': 'get_database_face', 'data': 0}
然后就等待接收 服务端发送的数据,并将数据存储在多进程队列result_queue 中,等待检测函数获取。
接收数据格式:
{”name“: 姓名, "image": 图片}
- def receive_send(client_socket, result_queue):
- """
- 接收服务器端 receive_data信息,若成功接收则发送success 否则发送 failure
- 并将信息存储在result_queue 中
- Args:
- client_socket:
- """
- receive_data = client_socket.recv(4096000)
- receive_data = receive_data.decode('utf-8')
- if len(receive_data):
- feedback_data = 'success'
- client_socket.sendall(feedback_data.encode('utf-8'))
- result_queue.put(receive_data)
- else:
- feedback_data = 'failure'
- client_socket.sendall(feedback_data.encode('utf-8'))
- def get_database(client_socket, result_queue):
- """
- 从服务器端接收数据(姓名,图片) {”name“: 姓名, "image": 图片}
- 放在result_queue队列中,然后让本地模型检测特征 用于人脸数据库的构建
- Args:
- client_socket:
- result_queue:
- """
- message_to_send = {'message': 'get_database_face', 'data': 0}
- send_receive(client_socket, message_to_send)
- while True:
- receive_send(client_socket, result_queue)
从 result_queue 队列中取出,从服务器获取的人脸数据库信息(姓名,图片) {”name“: 姓名, "image": 图片}
然后 将数据 进行 字节序列解码为字符串的操作,
再将 JSON 格式的字符串转换为 Python 对象 词典,
然后提取,姓名,人脸图片
然后将经过Base64编码的图像数据解码,并使用OpenCV库将解码后的字节数据转换为NumPy数组,以便在Python中进行图像处理和分析
使用app.get 检测人脸特征 并存放在 result_queue1 以待发送
存放在 result_queue1 队列的 数据格式为词典形式
{”name“: 姓名, "feature": 人脸特征}
姓名:str
人脸特征:[ 1,2,3..............]
- def features_construct(app, result_queue, result_queue1):
- """
- 从 result_queue 队列中取出,从服务器获取的人脸数据库信息(姓名,图片) {”name“: 姓名, "image": 图片}
- 使用app.get 检测人脸特征 并存放如result_queue1 以待发送
- 发送数据格式 {”name“: 姓名, "feature": 人脸特征}
- Args:
- app:
- result_queue:
- result_queue1:
- """
- while True:
- while not result_queue.empty():
- face_data = result_queue.get()
- face_data = json.loads(face_data.decode('utf-8'))
- name = face_data['name']
- image = face_data['image']
- # 解码图像
- img_bytes = base64.b64decode(image)
- # 将字节数据转换为NumPy数组
- image = cv2.imdecode(np.frombuffer(img_bytes, np.uint8), cv2.IMREAD_COLOR)
- features = []
- data_dict = {}
- for i in range(image):
- img = image[i]
- face_all = app.get(img)
- for face_single in face_all: # 遍历每个人脸
- features.append(face_single.normed_embedding) # 将人脸的嵌入特征加入features列表
- feature = mean_feature_fusion(features)
- data_dict['name'] = name
- data_dict['feature'] = feature
- result_queue1.put(data_dict)
从多进程队列result_queue1 中 读取数据 feature_data,
添加消息头message:feature
数据格式:
{'message': 'feature', "name": '姓名', 'feature': "人脸特征"}
将词典数据 转换为JSON格式的字符串
然后对字符串进行UTF-8编码 进行传输
- def send_features_data(client_socket, result_queue1):
- """
- 进行数据发送,将多进程队列result_queue1 中 数据读取并发送回服务器端 :人脸数据可特征构建
- 人脸特征数据(姓名,特征数据)
- Args:
- client_socket:
- result_queue: 多进程队列
- """
- while True:
- while not result_queue1.empty():
- feature_data = result_queue1.get()
- feature_data['message'] = 'feature'
- json_data = json.dumps(feature_data)
- send_receive(client_socket, json_data)
- def send_receive(client_socket, data):
- """
- 发送数据 data,并接收服务器端 feedback信息
- Args:
- client_socket:
- data: 要发送的数据
- """
- client_socket.sendall(data.encode('utf-8'))
- feedback_data = client_socket.recv(1024)
- feedback_data = feedback_data.decode('utf-8')
- print("已发送数据,对方已接收,反馈信息为:", feedback_data)
使用多进程 将上述函数串联起来
- def database_features_construct(app, result_queue, result_queue1):
- # 服务器地址和端口
- server_address = ('192.168.2.4', 12345)
-
- # 创建一个TCP socket
- client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
- # 连接到服务器
- client_socket.connect(server_address)
- print(f"Connected to server at {server_address}")
-
- # 发送消息给服务器
- message_to_send = "Hello, server! This is the client."
- client_socket.send(message_to_send.encode('utf-8'))
-
- # 接收服务器消息
- data = client_socket.recv(1024)
- print(f"Received data from server: {data.decode('utf-8')}")
-
- get_database_process = multiprocessing.Process(target=get_database, args=(client_socket, result_queue))
- features_construct_process = multiprocessing.Process(target=features_construct, args=(app, result_queue, result_queue1))
- send_features_data_process = multiprocessing.Process(target=send_features_data, args=(client_socket, result_queue1))
-
- # 启动进程
- get_database_process.start()
- features_construct_process.start()
- send_features_data_process.start()
- # 等待两个进程结束
- get_database_process.join()
- features_construct_process.join()
- send_features_data_process.join()
- # 关闭连接
- client_socket.close()
- if __name__ == "__main__":
- parser2 = argparse.ArgumentParser(description='insightface app test') # 创建参数解析器,设置描述为'insightface app test'
- # 通用设置
- parser2.add_argument('--ctx', default=0, type=int,
- help='ctx id, <0 means using cpu') # 添加参数'--ctx',默认值为0,类型为整数,帮助信息为'ctx id, <0 means using cpu'
- parser2.add_argument('--det-size', default=640, type=int,
- help='detection size') # 添加参数'--det-size',默认值为640,类型为整数,帮助信息为'detection size'
- face_args = parser2.parse_args() # 解析参数
- face_app = FaceAnalysis() # 创建FaceAnalysis实例
- face_app.prepare(ctx_id=face_args.ctx, det_size=(face_args.det_size, face_args.det_size)) # 准备分析器,设置ctx_id和det_size
- result_queue = multiprocessing.Queue() # 多进程队列
- result_queue1 = multiprocessing.Queue()
-
- database_features_construct(face_app, result_queue, result_queue1)