C#调用WITIAI模型
C#调用WITIAI模型(1)WITIAI软件训练好AI模型后,采用C#调用AI模型,得到分割分类图。
(2)halcon、opencv的接口都预留了,满足工业需求;
(3)Demo代码如下:
string ImagePath = this.textBox1.Text.ToString();
int sliceImageSize = 512;
double overRate = 0.0;
double image_threshold = 0.5;
int classnum = 4;
int GPUindex = 0;
int ImageChannel = 1;
string savepath1 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_1.png";
// 初始化AI模型
string AImodelpt = @"gray_model.WITIAI";
// method1
CallAIDLL.load_module(AImodelpt, GPUindex);
CallAIDLL.PredictGrayImage(ImagePath, savepath1, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
// method2 -- halcon12进行调试
HObject ho_Image = null;
HTuple Width = 1200, Height = 1200;
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, ImagePath);
HOperatorSet.GetImageSize(ho_Image, out Width, out Height);
IntPtr p = Marshal.AllocHGlobal(Width.I * Height.I * ImageChannel);
p = CallAIDLL.PredictGrayImagePath2Intptr(ImagePath, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
HObject outputImage = null;
HOperatorSet.GenEmptyObj(out outputImage);
outputImage.Dispose();
HOperatorSet.GenImage1Extern(out outputImage, "byte", Width, Height, p, Marshal.GetFunctionPointerForDelegate(callback));
string savepath2 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_2.png";
HOperatorSet.WriteImage(outputImage, "png", -1, savepath2);
// method3 -- halcon12进行调试
HObject ho_Image1 = null;
HTuple aWidth = 1200, aHeight = 1200;
HOperatorSet.GenEmptyObj(out ho_Image1);
ho_Image1.Dispose();
HOperatorSet.ReadImage(out ho_Image1, ImagePath);
HOperatorSet.GetImageSize(ho_Image1, out aWidth, out aHeight);
// 将图像转化为bytes传入
HTuple hred, hgreen, hblue, type0, width0, height0;
HOperatorSet.GetImagePointer1(ho_Image1, out hred, out type0, out width0, out height0);
//HOperatorSet.GetImagePointer3(ho_Image1, out hred, out hgreen, out hblue, out type0, out width0, out height0);
byte[] bptr = new byte;
for (int i = 0; i < (width0.I * height0.I); i++)
{
Marshal.Copy((IntPtr)(hred + i), bptr, i * 1 + 0, 1);
}
IntPtr p0 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
Marshal.Copy(bptr, 0, p0, aWidth.I * aHeight.I * ImageChannel);
// call
IntPtr p1 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
p1 = CallAIDLL.PredictGrayImageMat2Intptr(aWidth.I, aHeight.I, ImageChannel, p0, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
HObject outputImage1 = null;
HOperatorSet.GenEmptyObj(out outputImage1);
outputImage1.Dispose();
HOperatorSet.GenImage1Extern(out outputImage1, "byte", aWidth.I, aHeight.I, p1, Marshal.GetFunctionPointerForDelegate(callback));
string savepath3 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_3.png";
HOperatorSet.WriteImage(outputImage1, "png", -1, savepath3);
// method4 -- halcon12进行调试
string savepath4 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_4.png";
HObject ho_Image2 = null;
HTuple aWidth1 = 1200, aHeight1 = 1200;
HOperatorSet.GenEmptyObj(out ho_Image2);
ho_Image2.Dispose();
HOperatorSet.ReadImage(out ho_Image2, ImagePath);
HOperatorSet.GetImageSize(ho_Image2, out aWidth1, out aHeight1);
// 将图像转化为bytes传入
HTuple hred1, hgreen1, hblue1, type1, width1, height1;
HOperatorSet.GetImagePointer1(ho_Image2, out hred1, out type1, out width1, out height1);
//HOperatorSet.GetImagePointer3(ho_Image2, out hred1, out hgreen1, out hblue1, out type1, out width1, out height1);
byte[] bptr1 = new byte;
for (int i = 0; i < (aWidth1.I * aHeight1.I); i++)
{
Marshal.Copy((IntPtr)(hred1 + i), bptr1, i * 1 + 0, 1);
//Marshal.Copy((IntPtr)(hgreen1 + i), bptr1, i * 3 + 1, 1);
//Marshal.Copy((IntPtr)(hred1 + i), bptr1, i * 3 + 2, 1);
}
IntPtr p2 = Marshal.AllocHGlobal(aWidth1.I * aHeight1.I * ImageChannel);
Marshal.Copy(bptr1, 0, p2, aWidth1.I * aHeight1.I * ImageChannel);
// call
CallAIDLL.PredictGrayImageMat2Path(aWidth1, aHeight1, ImageChannel, p2, savepath4, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
//
MessageBox.Show("测试完成!!!");
彩色图像:
string ImagePath = this.textBox2.Text.ToString();
int sliceImageSize = 256;
double overRate = 0.0;
double image_threshold = 0.5;
int classnum = 4;
int GPUindex = 0;
int ImageChannel = 3;
string savepath1 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_1.png";
// 初始化AI模型
string AImodelpt = @"rgb_model.WITIAI";
// method1
CallAIDLL.load_module(AImodelpt, GPUindex);
CallAIDLL.PredictRGBImage(ImagePath, savepath1, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
// method2 -- halcon12进行调试
HObject ho_Image = null;
HTuple Width = 1200, Height = 1200;
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, ImagePath);
HOperatorSet.GetImageSize(ho_Image, out Width, out Height);
IntPtr p = Marshal.AllocHGlobal(Width.I * Height.I * ImageChannel);
p = CallAIDLL.PredictRGBImagePath2Intptr(ImagePath, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
HObject outputImage = null;
HOperatorSet.GenEmptyObj(out outputImage);
outputImage.Dispose();
HOperatorSet.GenImage1Extern(out outputImage, "byte", Width, Height, p, Marshal.GetFunctionPointerForDelegate(callback));
string savepath2 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_2.png";
HOperatorSet.WriteImage(outputImage, "png", -1, savepath2);
// method3 -- halcon12进行调试
HObject ho_Image1 = null;
HTuple aWidth = 1200, aHeight = 1200;
HOperatorSet.GenEmptyObj(out ho_Image1);
ho_Image1.Dispose();
HOperatorSet.ReadImage(out ho_Image1, ImagePath);
HOperatorSet.GetImageSize(ho_Image1, out aWidth, out aHeight);
// 将图像转化为bytes传入
HTuple hred, hgreen, hblue, type0, width0, height0;
HOperatorSet.GetImagePointer3(ho_Image1, out hred, out hgreen, out hblue, out type0, out width0, out height0);
byte[] bptr = new byte;
for (int i = 0; i < (width0.I * height0.I); i++)
{
Marshal.Copy((IntPtr)(hblue + i), bptr, i * 3 + 0, 1);
Marshal.Copy((IntPtr)(hgreen + i), bptr, i * 3 + 1, 1);
Marshal.Copy((IntPtr)(hred + i), bptr, i * 3 + 2, 1);
}
IntPtr p0 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
Marshal.Copy(bptr, 0, p0, aWidth.I * aHeight.I * ImageChannel);
// call
IntPtr p1 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
p1 = CallAIDLL.PredictRGBImageMat2Intptr(aWidth.I, aHeight.I, ImageChannel, p0, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
HObject outputImage1 = null;
HOperatorSet.GenEmptyObj(out outputImage1);
outputImage1.Dispose();
HOperatorSet.GenImage1Extern(out outputImage1, "byte", aWidth.I, aHeight.I, p1, Marshal.GetFunctionPointerForDelegate(callback));
string savepath3 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_3.png";
HOperatorSet.WriteImage(outputImage1, "png", -1, savepath3);
// method4 -- halcon12进行调试
string savepath4 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_4.png";
HObject ho_Image2 = null;
HTuple aWidth1 = 1200, aHeight1 = 1200;
HOperatorSet.GenEmptyObj(out ho_Image2);
ho_Image2.Dispose();
HOperatorSet.ReadImage(out ho_Image2, ImagePath);
HOperatorSet.GetImageSize(ho_Image2, out aWidth1, out aHeight1);
// 将图像转化为bytes传入
HTuple hred1, hgreen1, hblue1, type1, width1, height1;
HOperatorSet.GetImagePointer3(ho_Image2, out hred1, out hgreen1, out hblue1, out type1, out width1, out height1);
byte[] bptr1 = new byte;
for (int i = 0; i < (aWidth1.I * aHeight1.I); i++)
{
Marshal.Copy((IntPtr)(hblue1 + i), bptr1, i * 3 + 0, 1);
Marshal.Copy((IntPtr)(hgreen1 + i), bptr1, i * 3 + 1, 1);
Marshal.Copy((IntPtr)(hred1 + i), bptr1, i * 3 + 2, 1);
}
IntPtr p2 = Marshal.AllocHGlobal(aWidth1.I * aHeight1.I * ImageChannel);
Marshal.Copy(bptr1, 0, p2, aWidth1.I * aHeight1.I * ImageChannel);
// call
CallAIDLL.PredictRGBImageMat2Path(aWidth1, aHeight1, ImageChannel, p2, savepath4, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
//
MessageBox.Show("OK");
模型释放:
string AImodelpt = @"rgb_model.WITIAI";
CallAIDLL1.Free_module(AImodelpt, 0);
页:
[1]