|
C++调用WITIAI模型:
(1)WITIAI软件训练好AI模型后,采用C++调用AI模型,得到分割分类图。
(2)halcon、opencv的接口都预留了,满足工业需求;
(3)Demo代码如下:
- // CplusDemos.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
- #include <torch/script.h>
- #include <torch/utils.h>
- #include <iostream>
- #include <string>
- #include <time.h>
- #include <vector>
- #include <memory.h>
- #include <windows.h>
- #include <atlstr.h>
- #include <initguid.h>
- #include <setupapi.h>
- #include <opencv2/opencv.hpp>
- using namespace std;
- using namespace cv;
- typedef void(*pload_module)(char* module_path, int GPUindex);
- typedef void(*pPredictRGBImage)(char* input_image_path, char* output_image_path, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef unsigned char*(*pPredictRGBImagePath2Intptr)(char* input_image_path, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef unsigned char*(*pPredictRGBImageMat2Intptr)(int aWidth, int aHeight, int aChannel, unsigned char* aBytes, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef void(*pPredictRGBImageMat2Path)(int aWidth, int aHeight, int aChannel, unsigned char* aBytes, char* output_image_path, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef void(*pPredictGrayImage)(char* input_image_path, char* output_image_path, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef unsigned char*(*pPredictGrayImagePath2Intptr)(char* input_image_path, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef unsigned char*(*pPredictGrayImageMat2Intptr)(int aWidth, int aHeight, int aChannel, unsigned char* aBytes, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- typedef void(*pPredictGrayImageMat2Path)(int aWidth, int aHeight, int aChannel, unsigned char* aBytes, char* output_image_path, int sliceImageSize, double overRate, double image_threshold, int classnum, int GPUindex);
- void fun1()
- {
- HINSTANCE HDLL = LoadLibrary("PytorchApi.dll");//加载动态链接库MyDll.dll文件;
- if (HDLL != NULL)
- {
- ///////////////////////// RGB /////////////////////////////////////
- //
- char* model_path = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_model.WITIAI";
- //char* input_image_path = "D:/Release/rgb.jpg";
- char* input_image_path = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb.jpg";
- char* output_image_path = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_1.png";
- int sliceImageSize = 256;
- double overRate = 0;
- double image_threshold = 0.5;
- int classnum = 4;
- int GPUindex = 0;
- // Load model
- const char* FunctionName0 = "load_module";
- pload_module Callload_module = (pload_module)GetProcAddress(HDLL, FunctionName0);
- (*Callload_module)(model_path, GPUindex);
- // method1
- const char* FunctionName1 = "PredictRGBImage";
- pPredictRGBImage CallPredictRGBImage = (pPredictRGBImage)GetProcAddress(HDLL, FunctionName1);
- (*CallPredictRGBImage)(input_image_path, output_image_path, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- // method2
- Mat image = cv::imread(input_image_path, 1); // RGB
- int aHeight = image.rows;
- int aWidth = image.cols;
- int aChannel = image.channels();
- const char* FunctionName2 = "PredictRGBImagePath2Intptr";
- pPredictRGBImagePath2Intptr CallPredictRGBImagePath2Intptr = (pPredictRGBImagePath2Intptr)GetProcAddress(HDLL, FunctionName2);
- unsigned char* aBytes = new unsigned char[aHeight * aWidth * 1];
- aBytes = (*CallPredictRGBImagePath2Intptr)(input_image_path, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- Mat frame(aHeight, aWidth, CV_MAKETYPE(CV_8U, 1), aBytes);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_2.png", frame);
- // method3
- Mat image3 = cv::imread(input_image_path, 1); // RGB
- int aHeight3 = image3.rows;
- int aWidth3 = image3.cols;
- int aChannel3 = image3.channels();
- unsigned char* ucImg3 = new unsigned char[aHeight3 * aWidth3 * aChannel3];
- std::memcpy(ucImg3, image3.data, aHeight3 * aWidth3 * aChannel3 * sizeof(unsigned char));
- const char* FunctionName3 = "PredictRGBImageMat2Intptr";
- pPredictRGBImageMat2Intptr CallPredictRGBImageMat2Intptr = (pPredictRGBImageMat2Intptr)GetProcAddress(HDLL, FunctionName3);
- unsigned char* aBytes1 = new unsigned char[aHeight3 * aWidth3 * 1];
- aBytes1 = (*CallPredictRGBImageMat2Intptr)(aWidth3, aHeight3, aChannel3, ucImg3, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- Mat frame1(aHeight3, aWidth3, CV_MAKETYPE(CV_8U, 1), aBytes1);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_3.png", frame1);
- // method4
- Mat image4 = cv::imread(input_image_path, 1); // RGB
- int aHeight4 = image4.rows;
- int aWidth4 = image4.cols;
- int aChannel4 = image4.channels();
- unsigned char* ucImg4 = new unsigned char[aHeight4 * aWidth4 * aChannel4];
- std::memcpy(ucImg4, image4.data, aHeight4 * aWidth4 * aChannel4 * sizeof(unsigned char));
- const char* FunctionName4 = "PredictRGBImageMat2Path";
- pPredictRGBImageMat2Path CallPredictRGBImageMat2Path = (pPredictRGBImageMat2Path)GetProcAddress(HDLL, FunctionName4);
- char* output_image_path4 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_4.png";
- (*CallPredictRGBImageMat2Path)(aWidth4, aHeight4, aChannel4, ucImg4, output_image_path4, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- //
- ///////////////////////// RGB /////////////////////////////////////
- //
- //
- ///////////////////////// Gray /////////////////////////////////////
- //
- char* model_path0 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_model.WITIAI";
- char* input_image_path0 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray.jpg";
- char* output_image_path0 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_1.png";
- int sliceImageSize0 = 512;
- //double overRate = 0;
- //double image_threshold = 0.5;
- //int classnum = 4;
- //int GPUindex = 0;
- // Load model
- const char* FunctionName5 = "load_module";
- pload_module Callload_module_gray = (pload_module)GetProcAddress(HDLL, FunctionName5);
- (*Callload_module_gray)(model_path0, GPUindex);
- // method1
- const char* FunctionName6 = "PredictGrayImage";
- pPredictGrayImage CallPredictGrayImage = (pPredictGrayImage)GetProcAddress(HDLL, FunctionName6);
- (*CallPredictGrayImage)(input_image_path0, output_image_path0, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- // method2
- Mat image_gray = cv::imread(input_image_path0, 0); // Gray
- int aHeight_gray = image_gray.rows;
- int aWidth_gray = image_gray.cols;
- int aChannel_gray = image_gray.channels();
- const char* FunctionName7 = "PredictGrayImagePath2Intptr";
- pPredictGrayImagePath2Intptr CallPredictGrayImagePath2Intptr = (pPredictGrayImagePath2Intptr)GetProcAddress(HDLL, FunctionName7);
- unsigned char* aBytes_gray = new unsigned char[aHeight_gray * aWidth_gray * 1];
- aBytes_gray = (*CallPredictRGBImagePath2Intptr)(input_image_path0, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- Mat frame_gray(aHeight_gray, aWidth_gray, CV_MAKETYPE(CV_8U, 1), aBytes_gray);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_2.png", frame_gray);
- // method3
- Mat image3_gray = cv::imread(input_image_path0, 0); // Gray
- int aHeight3_gray = image3_gray.rows;
- int aWidth3_gray = image3_gray.cols;
- int aChannel3_gray = image3_gray.channels();
- unsigned char* ucImg3_gray = new unsigned char[aHeight3_gray * aWidth3_gray * aChannel3_gray];
- std::memcpy(ucImg3_gray, image3_gray.data, aHeight3_gray * aWidth3_gray * aChannel3_gray * sizeof(unsigned char));
- const char* FunctionName8 = "PredictGrayImageMat2Intptr";
- pPredictGrayImageMat2Intptr CallPredictGrayImageMat2Intptr = (pPredictGrayImageMat2Intptr)GetProcAddress(HDLL, FunctionName8);
- unsigned char* aBytes1_gray = new unsigned char[aHeight3 * aWidth3 * 1];
- aBytes1_gray = (*CallPredictGrayImageMat2Intptr)(aWidth3_gray, aHeight3_gray, aChannel3_gray, ucImg3_gray, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- Mat frame1_gray(aHeight3_gray, aWidth3_gray, CV_MAKETYPE(CV_8U, 1), aBytes1_gray);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_3.png", frame1_gray);
- // method4
- Mat image4_gray = cv::imread(input_image_path0, 0); // Gray
- int aHeight4_gray = image4_gray.rows;
- int aWidth4_gray = image4_gray.cols;
- int aChannel4_gray = image4_gray.channels();
- unsigned char* ucImg4_gray = new unsigned char[aHeight4_gray * aWidth4_gray * aChannel4_gray];
- std::memcpy(ucImg4_gray, image4_gray.data, aHeight4_gray * aWidth4_gray * aChannel4_gray * sizeof(unsigned char));
- const char* FunctionName9 = "PredictGrayImageMat2Path";
- pPredictGrayImageMat2Path CallPredictGrayImageMat2Path = (pPredictGrayImageMat2Path)GetProcAddress(HDLL, FunctionName9);
- char* output_image_path4_gray = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_4.png";
- (*CallPredictGrayImageMat2Path)(aWidth4_gray, aHeight4_gray, aChannel4_gray, ucImg4_gray, output_image_path4_gray, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- //
- ///////////////////////// Gray /////////////////////////////////////
- //try
- //{
- // FreeLibrary(HDLL); //卸载MyDll.dll文件;
- //}
- //catch(exception ex)
- //{
- // std::cout << "catch(FreeLibrary(HDLL))\n";
- //}
- }
- }
- void fun2()
- {
- HINSTANCE HDLL = LoadLibrary("PytorchApi1.dll");//加载动态链接库MyDll.dll文件;
- if (HDLL != NULL)
- {
- ///////////////////////// RGB /////////////////////////////////////
- //
- char* model_path = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_model.WITIAI";
- //char* input_image_path = "D:/Release/rgb.jpg";
- char* input_image_path = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb.jpg";
- char* output_image_path = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_1.png";
- int sliceImageSize = 256;
- double overRate = 0;
- double image_threshold = 0.5;
- int classnum = 4;
- int GPUindex = 0;
- // Load model
- const char* FunctionName0 = "load_module";
- pload_module Callload_module = (pload_module)GetProcAddress(HDLL, FunctionName0);
- (*Callload_module)(model_path, GPUindex);
- // method1
- const char* FunctionName1 = "PredictRGBImage";
- pPredictRGBImage CallPredictRGBImage = (pPredictRGBImage)GetProcAddress(HDLL, FunctionName1);
- (*CallPredictRGBImage)(input_image_path, output_image_path, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- // method2
- Mat image = cv::imread(input_image_path, 1); // RGB
- int aHeight = image.rows;
- int aWidth = image.cols;
- int aChannel = image.channels();
- const char* FunctionName2 = "PredictRGBImagePath2Intptr";
- pPredictRGBImagePath2Intptr CallPredictRGBImagePath2Intptr = (pPredictRGBImagePath2Intptr)GetProcAddress(HDLL, FunctionName2);
- unsigned char* aBytes = new unsigned char[aHeight * aWidth * 1];
- aBytes = (*CallPredictRGBImagePath2Intptr)(input_image_path, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- Mat frame(aHeight, aWidth, CV_MAKETYPE(CV_8U, 1), aBytes);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_2.png", frame);
- // method3
- Mat image3 = cv::imread(input_image_path, 1); // RGB
- int aHeight3 = image3.rows;
- int aWidth3 = image3.cols;
- int aChannel3 = image3.channels();
- unsigned char* ucImg3 = new unsigned char[aHeight3 * aWidth3 * aChannel3];
- std::memcpy(ucImg3, image3.data, aHeight3 * aWidth3 * aChannel3 * sizeof(unsigned char));
- const char* FunctionName3 = "PredictRGBImageMat2Intptr";
- pPredictRGBImageMat2Intptr CallPredictRGBImageMat2Intptr = (pPredictRGBImageMat2Intptr)GetProcAddress(HDLL, FunctionName3);
- unsigned char* aBytes1 = new unsigned char[aHeight3 * aWidth3 * 1];
- aBytes1 = (*CallPredictRGBImageMat2Intptr)(aWidth3, aHeight3, aChannel3, ucImg3, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- Mat frame1(aHeight3, aWidth3, CV_MAKETYPE(CV_8U, 1), aBytes1);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_3.png", frame1);
- // method4
- Mat image4 = cv::imread(input_image_path, 1); // RGB
- int aHeight4 = image4.rows;
- int aWidth4 = image4.cols;
- int aChannel4 = image4.channels();
- unsigned char* ucImg4 = new unsigned char[aHeight4 * aWidth4 * aChannel4];
- std::memcpy(ucImg4, image4.data, aHeight4 * aWidth4 * aChannel4 * sizeof(unsigned char));
- const char* FunctionName4 = "PredictRGBImageMat2Path";
- pPredictRGBImageMat2Path CallPredictRGBImageMat2Path = (pPredictRGBImageMat2Path)GetProcAddress(HDLL, FunctionName4);
- char* output_image_path4 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/rgb_4.png";
- (*CallPredictRGBImageMat2Path)(aWidth4, aHeight4, aChannel4, ucImg4, output_image_path4, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
- //
- ///////////////////////// RGB /////////////////////////////////////
- //
- //
- ///////////////////////// Gray /////////////////////////////////////
- //
- char* model_path0 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_model.WITIAI";
- char* input_image_path0 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray.jpg";
- char* output_image_path0 = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_1.png";
- int sliceImageSize0 = 512;
- //double overRate = 0;
- //double image_threshold = 0.5;
- //int classnum = 4;
- //int GPUindex = 0;
- // Load model
- const char* FunctionName5 = "load_module";
- pload_module Callload_module_gray = (pload_module)GetProcAddress(HDLL, FunctionName5);
- (*Callload_module_gray)(model_path0, GPUindex);
- // method1
- const char* FunctionName6 = "PredictGrayImage";
- pPredictGrayImage CallPredictGrayImage = (pPredictGrayImage)GetProcAddress(HDLL, FunctionName6);
- (*CallPredictGrayImage)(input_image_path0, output_image_path0, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- // method2
- Mat image_gray = cv::imread(input_image_path0, 0); // Gray
- int aHeight_gray = image_gray.rows;
- int aWidth_gray = image_gray.cols;
- int aChannel_gray = image_gray.channels();
- const char* FunctionName7 = "PredictGrayImagePath2Intptr";
- pPredictGrayImagePath2Intptr CallPredictGrayImagePath2Intptr = (pPredictGrayImagePath2Intptr)GetProcAddress(HDLL, FunctionName7);
- unsigned char* aBytes_gray = new unsigned char[aHeight_gray * aWidth_gray * 1];
- aBytes_gray = (*CallPredictRGBImagePath2Intptr)(input_image_path0, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- Mat frame_gray(aHeight_gray, aWidth_gray, CV_MAKETYPE(CV_8U, 1), aBytes_gray);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_2.png", frame_gray);
- // method3
- Mat image3_gray = cv::imread(input_image_path0, 0); // Gray
- int aHeight3_gray = image3_gray.rows;
- int aWidth3_gray = image3_gray.cols;
- int aChannel3_gray = image3_gray.channels();
- unsigned char* ucImg3_gray = new unsigned char[aHeight3_gray * aWidth3_gray * aChannel3_gray];
- std::memcpy(ucImg3_gray, image3_gray.data, aHeight3_gray * aWidth3_gray * aChannel3_gray * sizeof(unsigned char));
- const char* FunctionName8 = "PredictGrayImageMat2Intptr";
- pPredictGrayImageMat2Intptr CallPredictGrayImageMat2Intptr = (pPredictGrayImageMat2Intptr)GetProcAddress(HDLL, FunctionName8);
- unsigned char* aBytes1_gray = new unsigned char[aHeight3 * aWidth3 * 1];
- aBytes1_gray = (*CallPredictGrayImageMat2Intptr)(aWidth3_gray, aHeight3_gray, aChannel3_gray, ucImg3_gray, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- Mat frame1_gray(aHeight3_gray, aWidth3_gray, CV_MAKETYPE(CV_8U, 1), aBytes1_gray);
- cv::imwrite("D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_3.png", frame1_gray);
- // method4
- Mat image4_gray = cv::imread(input_image_path0, 0); // Gray
- int aHeight4_gray = image4_gray.rows;
- int aWidth4_gray = image4_gray.cols;
- int aChannel4_gray = image4_gray.channels();
- unsigned char* ucImg4_gray = new unsigned char[aHeight4_gray * aWidth4_gray * aChannel4_gray];
- std::memcpy(ucImg4_gray, image4_gray.data, aHeight4_gray * aWidth4_gray * aChannel4_gray * sizeof(unsigned char));
- const char* FunctionName9 = "PredictGrayImageMat2Path";
- pPredictGrayImageMat2Path CallPredictGrayImageMat2Path = (pPredictGrayImageMat2Path)GetProcAddress(HDLL, FunctionName9);
- char* output_image_path4_gray = "D:/2-LearningCode/994-WiTiAi/5_Demos/CplusDemos/CplusDemos/x64/Release/gray_4.png";
- (*CallPredictGrayImageMat2Path)(aWidth4_gray, aHeight4_gray, aChannel4_gray, ucImg4_gray, output_image_path4_gray, sliceImageSize0, overRate, image_threshold, classnum, GPUindex);
- //
- ///////////////////////// Gray /////////////////////////////////////
- //try
- //{
- // FreeLibrary(HDLL); //卸载MyDll.dll文件;
- //}
- //catch(exception ex)
- //{
- // std::cout << "catch(FreeLibrary(HDLL))\n";
- //}
- }
- }
- int main()
- {
- std::cout << "Hello World!\n";
- // 注释:当有2个AI模型参与调度时, PytorchApi.dll,加载第一个模型,PytorchApi1.dll,加载第二个模型
- fun1();
- //fun2();
- system("pause");
- return 0;
- }
复制代码
|
|