2024-05-24 00:21:03 +08:00
|
|
|
import toml
|
|
|
|
|
import threading
|
|
|
|
|
from loguru import logger
|
|
|
|
|
import zmq
|
|
|
|
|
from infer import Lane_model_infer
|
|
|
|
|
import numpy as np
|
2024-05-24 13:00:10 +08:00
|
|
|
import cv2
|
2024-06-07 20:19:04 +08:00
|
|
|
|
2024-05-24 00:21:03 +08:00
|
|
|
lock = threading.Lock()
|
|
|
|
|
response = {'code': 0, 'data': 0}
|
|
|
|
|
|
2024-06-07 20:19:04 +08:00
|
|
|
|
|
|
|
|
# context2 = zmq.Context()
|
|
|
|
|
# socket_server = context2.socket(zmq.PUB)
|
|
|
|
|
# socket_server.bind("tcp://*:7778")
|
|
|
|
|
|
|
|
|
|
# 处理 server 响应数据
|
2024-05-24 00:21:03 +08:00
|
|
|
def server_resp(lane_infer_port):
|
|
|
|
|
logger.info("lane server thread init success")
|
|
|
|
|
global response
|
|
|
|
|
|
|
|
|
|
context = zmq.Context()
|
2024-06-07 20:19:04 +08:00
|
|
|
# 启动 server
|
2024-05-24 00:21:03 +08:00
|
|
|
socket = context.socket(zmq.REP)
|
|
|
|
|
socket.bind(f"tcp://*:{lane_infer_port}")
|
|
|
|
|
logger.info("lane infer server init success")
|
|
|
|
|
while True:
|
|
|
|
|
message = socket.recv_string()
|
|
|
|
|
with lock:
|
|
|
|
|
socket.send_pyobj(response)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
cfg = toml.load('../cfg_infer_server.toml')
|
|
|
|
|
|
|
|
|
|
# 配置日志输出
|
|
|
|
|
logger.add(cfg['debug']['logger_filename'], format=cfg['debug']['logger_format'], retention = 5, level="INFO")
|
|
|
|
|
|
2024-06-07 20:19:04 +08:00
|
|
|
# 连接摄像头 server 巡线只需要连接前摄像头
|
2024-05-24 00:21:03 +08:00
|
|
|
context = zmq.Context()
|
2024-06-07 20:19:04 +08:00
|
|
|
camera_socket = context.socket(zmq.REQ)
|
2024-05-24 13:00:10 +08:00
|
|
|
camera_socket.connect(f"tcp://localhost:{cfg['camera']['front_camera_port']}")
|
2024-05-24 00:21:03 +08:00
|
|
|
logger.info("connect camera success")
|
2024-06-07 20:19:04 +08:00
|
|
|
|
|
|
|
|
# 初始化 paddle 推理器
|
2024-05-24 00:21:03 +08:00
|
|
|
predictor = Lane_model_infer()
|
|
|
|
|
logger.info("lane model load success")
|
2024-06-07 20:19:04 +08:00
|
|
|
# 启动 lane_infer_server 线程
|
2024-05-24 00:21:03 +08:00
|
|
|
mythread = threading.Thread(target=server_resp,
|
2024-05-24 13:00:10 +08:00
|
|
|
args=(cfg['server']['lane_infer_port'],),
|
2024-05-24 00:21:03 +08:00
|
|
|
daemon=True)
|
2024-06-07 20:19:04 +08:00
|
|
|
|
2024-05-24 00:21:03 +08:00
|
|
|
mythread.start()
|
|
|
|
|
|
|
|
|
|
while True:
|
2024-06-07 20:19:04 +08:00
|
|
|
camera_socket.send_string("")
|
2024-05-24 13:00:10 +08:00
|
|
|
message = camera_socket.recv()
|
|
|
|
|
np_array = np.frombuffer(message, dtype=np.uint8)
|
|
|
|
|
frame = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
2024-06-07 20:19:04 +08:00
|
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
2024-05-24 13:00:10 +08:00
|
|
|
result = predictor.infer(frame)
|
2024-05-24 00:21:03 +08:00
|
|
|
with lock:
|
|
|
|
|
response['data'] = result
|
2024-06-07 20:19:04 +08:00
|
|
|
# print(result)
|
|
|
|
|
# cv2.circle(frame,(int(result[0]),int(result[1])),5,(0,255,0),-1)
|
|
|
|
|
# socket_server.send_pyobj(frame)
|
|
|
|
|
if cv2.waitKey(1) == 27:
|
|
|
|
|
break
|
2024-05-24 00:21:03 +08:00
|
|
|
mythread.join()
|
|
|
|
|
logger.info("lane infer server exit")
|
|
|
|
|
|