diff --git a/YOLOPv2.cpp b/YOLOPv2.cpp index 33f325b..38f6359 100644 --- a/YOLOPv2.cpp +++ b/YOLOPv2.cpp @@ -1,5 +1,21 @@ #include "YOLOPv2.h" #include +#include "tcp_utility.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +/*#include +#include +#include // 需要安装nlohmann/json库 +#include +#pragma comment(lib, "ws2_32.lib") + +using json = nlohmann::json;*/ YOLOPv2::YOLOPv2(Net_config config) { @@ -67,6 +83,37 @@ YOLOPv2::YOLOPv2(Net_config config) while (getline(ifs, line)) this->class_names.push_back(line); this->num_class = class_names.size(); + + //initial tcp 开始 + //tcp::TcpClient client; + + // 设置消息回调 + this->tcp_client.setMessageCallback([](tcp::TcpSocket& socket, const std::vector& data) { + std::string message(data.begin(), data.end()); + std::cout << "Received from server: " << message << std::endl; + }); + + // 设置错误回调 + this->tcp_client.setErrorCallback([](tcp::TcpSocket& socket, tcp::ErrorCode error) { + std::cout << "Error: " << tcp::errorCodeToString(error) << std::endl; + }); + + // 连接服务器 + auto result = this->tcp_client.connect("192.168.124.135", 6000); + if (result == tcp::ErrorCode::Success) { + std::cout << "Connected to server" << std::endl; + + // 发送消息 + //this->tcp_client.send("Hello, server!"); + + // 让客户端运行一段时间 + std::cout << "Press Enter to disconnect..." << std::endl; + std::cin.get(); + } else { + std::cout << "Failed to connect to server: " + << tcp::errorCodeToString(result) << std::endl; + } + } void YOLOPv2::normalize_(Mat img) @@ -130,6 +177,106 @@ inline float sigmoid(float x) { return 1.0 / (1 + exp(-x)); } +/* +void SendMessage(const char* ip, int port, map data) { + // 初始化Winsock + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + std::cerr << "WSAStartup failed: " << WSAGetLastError() << std::endl; + return; + } + + // 创建套接字 + SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (hSocket == INVALID_SOCKET) { + std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl; + WSACleanup(); + return; + } + + // 设置服务器地址 + sockaddr_in servAddr; + servAddr.sin_family = AF_INET; + servAddr.sin_port = htons(port); + if (inet_pton(AF_INET, ip, &servAddr.sin_addr) <= 0) { + std::cerr << "Invalid address/Address not supported" << std::endl; + closesocket(hSocket); + WSACleanup(); + return; + } + + // 连接服务器 + if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) { + std::cerr << "Connection failed: " << WSAGetLastError() << std::endl; + closesocket(hSocket); + WSACleanup(); + return; + } + + // 构造JSON消息 + json message; + message["receiver"] = "ANGLE"; + message["sender"] = "ANGLE"; + message["type"] = 5; + + json params; + for (const auto& pair: data) { + params[pair.first] = pair.second; + } + params["status"] = "OK"; + message["params"] = params; + + std::string jsonStr = message.dump(); + + // 发送消息 + int bytesSent = send(hSocket, jsonStr.c_str(), (int)jsonStr.size(), 0); + if (bytesSent == SOCKET_ERROR) { + std::cerr << "Send failed: " << WSAGetLastError() << std::endl; + } else { + std::cout << "Sent " << bytesSent << " bytes: " << jsonStr << std::endl; + + // 接收响应(可选) + char buffer[1024]; + int bytesRecv = recv(hSocket, buffer, sizeof(buffer), 0); + if (bytesRecv > 0) { + std::cout << "Received response: " << std::string(buffer, bytesRecv) << std::endl; + } + } + + // 清理资源 + closesocket(hSocket); + WSACleanup(); +}*/ +std::vector YOLOPv2::mapToByteArray(const std::map& param) { + std::vector byteArray; + + // 写入map大小 + size_t mapSize = param.size(); + byteArray.insert(byteArray.end(), + reinterpret_cast(&mapSize), + reinterpret_cast(&mapSize) + sizeof(mapSize)); + + // 遍历map中的每个元素 + for (const auto& pair : param) { + // 写入键的长度 + size_t keyLength = pair.first.size(); + byteArray.insert(byteArray.end(), + reinterpret_cast(&keyLength), + reinterpret_cast(&keyLength) + sizeof(keyLength)); + + // 写入键的内容 + byteArray.insert(byteArray.end(), + pair.first.begin(), + pair.first.end()); + + // 写入值(double类型) + byteArray.insert(byteArray.end(), + reinterpret_cast(&pair.second), + reinterpret_cast(&pair.second) + sizeof(double)); + } + + return byteArray; +} Mat YOLOPv2::detect(Mat& frame) { @@ -251,12 +398,12 @@ Mat YOLOPv2::detect(Mat& frame) outimg.at(i, j)[0] = 255; outimg.at(i, j)[1] = 0; outimg.at(i, j)[2] = 0; - if (!flg && j >= frame.cols / 2 && right == -1) { // 记录图像右半部分最靠左的车道线的左边缘坐标 + if (!flg && j >= center && right == -1) { // 记录图像右半部分最靠左的车道线的左边缘坐标 right = j; } flg = true; } else { - if (flg && j - 1 < frame.cols / 2) { //记录图像左半部分最靠右的车道线的右边缘坐标 + if (flg && j - 1 < center) { //记录图像左半部分最靠右的车道线的右边缘坐标 left = j - 1; } flg = false; @@ -280,9 +427,10 @@ Mat YOLOPv2::detect(Mat& frame) outimg.at(i, mid+k)[2] = 255; } //提取偏移量 - for (int k = 0; k < N; k++) { + for (int k = 1; k < N-1; k++) { if (ypos[k] == i) { deviation[dis[k]] = (mid - center) * rate[k]; + cerr< params; + //params["dev_1m"] = deviation[dis[0]]; + //params["dev_2m"] = deviation[dis[1]]; + //params["dev_3m"] = deviation[dis[2]]; + //params["dev_4m"] = deviation[dis[3]]; + //params["dev_5m"] = deviation[dis[4]]; + //params["dev_6m"] = deviation[dis[5]]; + //params["angle"] = dev_angle; + //SendMessage("127.0.0.1", 6000, params); + //std::vector byteVector = mapToByteArray(params); + // 发送消息 + QJsonObject json; + json["receiver"]="DRIVER"; + json["sender"]="FAST"; + json["type"]=22; + QJsonObject params; + params["angle"]=dev_angle; + json["params"]=params; + QJsonDocument doc(json); + QByteArray data=QJsonDocument(json).toJson(); + std::string jsonstr=data.toStdString(); + std::cout << jsonstr<< std::endl; + + this->tcp_client.send(jsonstr); + return outimg; } + + + + +