#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <vector>
#include <F:/work/c/rapidjson/include/rapidjson/document.h>
#include <F:/work/c/rapidjson/include/rapidjson/writer.h>
#include <F:/work/c/rapidjson/include/rapidjson/stringbuffer.h>
#include "F:/work/c/cpp-httplib/httplib.h"
#include <F:/work/c/cppcodec/cppcodec/base64_rfc4648.hpp>
using namespace std;
using namespace cv;
using namespace cv::dnn;
class DetectionObject {
public:
int index;
int class_id;
double confidence;
// cv::Rect rect;
};
class ModelManager {
private:
std::vector<Net> nets;
static ModelManager* instance;
ModelManager() {
// 加载模型的代码
vector<string> model_paths = { "F:/work/c/1.onnx" };
for (string model_path : model_paths) {
Net net = readNetFromONNX(model_path);
bool is_cuda = false;
if (is_cuda) {
cout << "Running to use CUDA\n" << endl;
net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
}
else {
net.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
net.setPreferableTarget(dnn::DNN_TARGET_CPU);
cout << "Running to use CPU\n" << endl;
}
nets.push_back(net);
}
}
public:
static ModelManager* getInstance() {
if (instance == nullptr) {
instance = new ModelManager();
}
return instance;
}
Net& getModel(int index) {
return nets[index];
}
int getNumModels() {
return nets.size();
}
};
ModelManager* ModelManager::instance = nullptr;
int main() {
//imshow("output", n640);
//waitKey(0);
//HTTPS
//httplib::SSLServer svr;
httplib::Server svr;
svr.Post("/v1/api/aiMedia/recognition", [](const httplib::Request& req, httplib::Response& res) {
//std::string base64EncodedImage = req.body;
std::string base64EncodedImage = req.get_param_value("file");
// 解码Base64字符串,并存储在vector中
std::vector<unsigned char> decodedImage = cppcodec::base64_rfc4648::decode(base64EncodedImage);
// 使用OpenCV的imdecode函数将图像数据解码为cv::Mat对象
cv::Mat image = cv::imdecode(decodedImage, cv::IMREAD_COLOR);
// 检查图像是否成功加载
if (image.empty()) {
std::cerr << "Failed to decode image." << std::endl;
return -1;
}
rapidjson::Document document;
document.SetObject();
rapidjson::Value results(rapidjson::kArrayType);
std::vector<DetectionObject> detectionObjects;
//vector<string> model_paths = { "F:/work/c/1.onnx" };
//vector<Net> nets;
//for (string model_path : model_paths) {
// Net net = readNetFromONNX(model_path);
// bool is_cuda = false;
// if (is_cuda)
// {
// cout << "Running to use CUDA\n" << endl;
// net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
// net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
// //result.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA__FP16);
// }
// else {
// net.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
// net.setPreferableTarget(dnn::DNN_TARGET_CPU);
// cout << "Running to use CPU\n" << endl;
// }
// nets.push_back(net);
//}
//string image_path = "F:/work/c/6.jpg";
int h00 = 736;
int w00 = 1280;
//Mat image = imread(image_path);
// cout << "start" << endl;
Mat input_blob;
cv::dnn::blobFromImage(image, input_blob, 1.0 / 255.0, Size(w00, h00), Scalar(), true, false);
// cout << " input_blob" << endl;
vector<Mat> detections;
//for (Net net : nets) {
// net.setInput(input_blob);
// // cout << "setInput" << endl;
// Mat detection = net.forward();
// detections.push_back(detection);
//}
//cout << "forward" << endl;
ModelManager* manager = ModelManager::getInstance();
int numModels = manager->getNumModels();
for (int i = 0; i < numModels; i++) {
Net& net = manager->getModel(i);
net.setInput(input_blob);
Mat detection = net.forward();
detections.push_back(detection);
}
cv::Mat n640;
for (int i = 0; i < detections.size(); i++) {
Mat detection = detections[i];
float* data = (float*)detection.data;
float x_factor = image.cols / w00;
float y_factor = image.rows / h00;
for (int aa = 0; aa < 20; aa++) {
// cout << "aa" << data[aa] << endl;
}
std::vector<int> class_idsss;
std::vector<float> confidencesss;
std::vector<cv::Rect> boxesss;
for (int i = 0; i < detection.total(); i += 21) {
float confidence = data[i + 4];
int class_id = (int)(data[i + 5]);
float x, y, w, h, left, top, width, height;
x = data[i + 0];
y = data[i + 1];
w = data[i + 2];
h = data[i + 3];
left = (x - 0.5 * w);
top = (y - 0.5 * h);
width = w;
height = h;
int idx = -1;
double conf = 0;
float confide = data[i + 4];
if (confidence >= 0.5) {
float* classes_sco = data + i + 5;
cv::Mat scores(1, 16, CV_32FC1, classes_sco);
cv::Point cla_id;
double max_class_score;
minMaxLoc(scores, 0, &max_class_score, 0, &cla_id);
if (max_class_score > 0.5) {
conf = max_class_score;
idx = cla_id.x;
}
}
if (conf > 0.5) {
cv::Rect aaaa = cv::Rect(left, top, w, h);
boxesss.push_back(aaaa);
class_idsss.push_back(idx);
confidencesss.push_back((float)conf);
}
}
std::vector<int> nms_result;
// cout << " boxesss() " << boxesss.size() << endl;
cv::dnn::NMSBoxes(boxesss, confidencesss, 0.4, 0.5, nms_result);
//cout << " nms_result.size() " << nms_result.size() << endl;
cv::resize(image, n640, Size(w00, h00));
for (int i = 0; i < nms_result.size(); i++) {
int id = nms_result[i];
int class_id = class_idsss[id];
double cf = confidencesss[id];
cv::Rect rec = boxesss[id];
rapidjson::Value result(rapidjson::kObjectType);
result.AddMember("index", i, document.GetAllocator());
result.AddMember("class_id", class_id, document.GetAllocator());
result.AddMember("confidence", cf, document.GetAllocator());
rapidjson::Value position(rapidjson::kObjectType);
position.AddMember("x", rec.x, document.GetAllocator());
position.AddMember("y", rec.y, document.GetAllocator());
position.AddMember("width", rec.width, document.GetAllocator());
position.AddMember("height", rec.height, document.GetAllocator());
result.AddMember("position", position, document.GetAllocator());
results.PushBack(result, document.GetAllocator());
// DetectionObject detectionObject;
// detectionObject.index = i;
// detectionObject.class_id = class_id;
// detectionObject.confidence = cf;
// detectionObject.rect = rec;
// detectionObjects.push_back(detectionObject);
// rectangle(n640, rec, Scalar(0, 255, 0), 1);
// cout << i << " " << "class" << class_id << " " << cf << " x:" << rec.x << " y:" << rec.y << " w:" << rec.width << " h:" << rec.height << endl;
//putText(n640, to_string(class_id), Point(rec.x, rec.y - 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0), 1);
}
}
document.AddMember("results", results, document.GetAllocator());
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
std::string json = buffer.GetString();
std::cout << json << std::endl; // 输出 JSON 字符串
cout << "detection.total()" << detections[0].total() << endl;
cout << "RESULT" << json << endl;
res.set_content( json , "text/plain");
});
cout << " run " << 0 << endl;
svr.listen("0.0.0.0", 8087);
return 0;
}
评论区