请选择 进入手机版 | 继续访问电脑版

AOI

 找回密码
 立即注册
查看: 16689|回复: 2

C#调用WITIAI模型

[复制链接]

83

主题

111

帖子

653

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
653
发表于 2021-11-22 22:54:28 | 显示全部楼层 |阅读模式
C#调用WITIAI模型
(1)WITIAI软件训练好AI模型后,采用C#调用AI模型,得到分割分类图。
(2)halcon、opencv的接口都预留了,满足工业需求;
(3)Demo代码如下:
  1.             string ImagePath = this.textBox1.Text.ToString();
  2.             int sliceImageSize = 512;
  3.             double overRate = 0.0;
  4.             double image_threshold = 0.5;
  5.             int classnum = 4;
  6.             int GPUindex = 0;
  7.             int ImageChannel = 1;
  8.             string savepath1 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_1.png";
  9.             // 初始化AI模型
  10.             string AImodelpt = @"gray_model.WITIAI";
  11.             // method1
  12.             CallAIDLL.load_module(AImodelpt, GPUindex);
  13.             CallAIDLL.PredictGrayImage(ImagePath, savepath1, sliceImageSize, overRate, image_threshold, classnum, GPUindex);

  14.             // method2 -- halcon12进行调试
  15.             HObject ho_Image = null;
  16.             HTuple Width = 1200, Height = 1200;
  17.             HOperatorSet.GenEmptyObj(out ho_Image);
  18.             ho_Image.Dispose();
  19.             HOperatorSet.ReadImage(out ho_Image, ImagePath);
  20.             HOperatorSet.GetImageSize(ho_Image, out Width, out Height);
  21.             IntPtr p = Marshal.AllocHGlobal(Width.I * Height.I * ImageChannel);
  22.             p = CallAIDLL.PredictGrayImagePath2Intptr(ImagePath, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
  23.             HObject outputImage = null;
  24.             HOperatorSet.GenEmptyObj(out outputImage);
  25.             outputImage.Dispose();
  26.             HOperatorSet.GenImage1Extern(out outputImage, "byte", Width, Height, p, Marshal.GetFunctionPointerForDelegate(callback));
  27.             string savepath2 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_2.png";
  28.             HOperatorSet.WriteImage(outputImage, "png", -1, savepath2);

  29.             // method3 -- halcon12进行调试
  30.             HObject ho_Image1 = null;
  31.             HTuple aWidth = 1200, aHeight = 1200;
  32.             HOperatorSet.GenEmptyObj(out ho_Image1);
  33.             ho_Image1.Dispose();
  34.             HOperatorSet.ReadImage(out ho_Image1, ImagePath);
  35.             HOperatorSet.GetImageSize(ho_Image1, out aWidth, out aHeight);
  36.             // 将图像转化为bytes传入
  37.             HTuple hred, hgreen, hblue, type0, width0, height0;
  38.             HOperatorSet.GetImagePointer1(ho_Image1, out hred, out type0, out width0, out height0);
  39.             //HOperatorSet.GetImagePointer3(ho_Image1, out hred, out hgreen, out hblue, out type0, out width0, out height0);
  40.             byte[] bptr = new byte[aWidth.I * aHeight.I * ImageChannel];
  41.             for (int i = 0; i < (width0.I * height0.I); i++)
  42.             {
  43.                 Marshal.Copy((IntPtr)(hred + i), bptr, i * 1 + 0, 1);
  44.             }
  45.             IntPtr p0 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
  46.             Marshal.Copy(bptr, 0, p0, aWidth.I * aHeight.I * ImageChannel);
  47.             // call
  48.             IntPtr p1 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
  49.             p1 = CallAIDLL.PredictGrayImageMat2Intptr(aWidth.I, aHeight.I, ImageChannel, p0, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
  50.             HObject outputImage1 = null;
  51.             HOperatorSet.GenEmptyObj(out outputImage1);
  52.             outputImage1.Dispose();
  53.             HOperatorSet.GenImage1Extern(out outputImage1, "byte", aWidth.I, aHeight.I, p1, Marshal.GetFunctionPointerForDelegate(callback));
  54.             string savepath3 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_3.png";
  55.             HOperatorSet.WriteImage(outputImage1, "png", -1, savepath3);

  56.             // method4 -- halcon12进行调试
  57.             string savepath4 = System.Windows.Forms.Application.StartupPath + @"/" + "gray_4.png";
  58.             HObject ho_Image2 = null;
  59.             HTuple aWidth1 = 1200, aHeight1 = 1200;
  60.             HOperatorSet.GenEmptyObj(out ho_Image2);
  61.             ho_Image2.Dispose();
  62.             HOperatorSet.ReadImage(out ho_Image2, ImagePath);
  63.             HOperatorSet.GetImageSize(ho_Image2, out aWidth1, out aHeight1);
  64.             // 将图像转化为bytes传入
  65.             HTuple hred1, hgreen1, hblue1, type1, width1, height1;
  66.             HOperatorSet.GetImagePointer1(ho_Image2, out hred1, out type1, out width1, out height1);
  67.             //HOperatorSet.GetImagePointer3(ho_Image2, out hred1, out hgreen1, out hblue1, out type1, out width1, out height1);
  68.             byte[] bptr1 = new byte[aWidth1.I * aHeight1.I * ImageChannel];
  69.             for (int i = 0; i < (aWidth1.I * aHeight1.I); i++)
  70.             {
  71.                 Marshal.Copy((IntPtr)(hred1 + i), bptr1, i * 1 + 0, 1);
  72.                 //Marshal.Copy((IntPtr)(hgreen1 + i), bptr1, i * 3 + 1, 1);
  73.                 //Marshal.Copy((IntPtr)(hred1 + i), bptr1, i * 3 + 2, 1);
  74.             }
  75.             IntPtr p2 = Marshal.AllocHGlobal(aWidth1.I * aHeight1.I * ImageChannel);
  76.             Marshal.Copy(bptr1, 0, p2, aWidth1.I * aHeight1.I * ImageChannel);
  77.             // call
  78.             CallAIDLL.PredictGrayImageMat2Path(aWidth1, aHeight1, ImageChannel, p2, savepath4, sliceImageSize, overRate, image_threshold, classnum, GPUindex);

  79.             //
  80.             MessageBox.Show("测试完成!!!");
复制代码








回复

使用道具 举报

83

主题

111

帖子

653

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
653
 楼主| 发表于 2021-11-22 22:56:29 | 显示全部楼层
彩色图像:
  1.             string ImagePath = this.textBox2.Text.ToString();
  2.             int sliceImageSize = 256;
  3.             double overRate = 0.0;
  4.             double image_threshold = 0.5;
  5.             int classnum = 4;
  6.             int GPUindex = 0;
  7.             int ImageChannel = 3;
  8.             string savepath1 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_1.png";
  9.             // 初始化AI模型
  10.             string AImodelpt = @"rgb_model.WITIAI";
  11.             // method1
  12.             CallAIDLL.load_module(AImodelpt, GPUindex);
  13.             CallAIDLL.PredictRGBImage(ImagePath, savepath1, sliceImageSize, overRate, image_threshold, classnum, GPUindex);

  14.             // method2 -- halcon12进行调试
  15.             HObject ho_Image = null;
  16.             HTuple Width = 1200, Height = 1200;
  17.             HOperatorSet.GenEmptyObj(out ho_Image);
  18.             ho_Image.Dispose();
  19.             HOperatorSet.ReadImage(out ho_Image, ImagePath);
  20.             HOperatorSet.GetImageSize(ho_Image, out Width, out Height);
  21.             IntPtr p = Marshal.AllocHGlobal(Width.I * Height.I * ImageChannel);
  22.             p = CallAIDLL.PredictRGBImagePath2Intptr(ImagePath, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
  23.             HObject outputImage = null;
  24.             HOperatorSet.GenEmptyObj(out outputImage);
  25.             outputImage.Dispose();
  26.             HOperatorSet.GenImage1Extern(out outputImage, "byte", Width, Height, p, Marshal.GetFunctionPointerForDelegate(callback));
  27.             string savepath2 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_2.png";
  28.             HOperatorSet.WriteImage(outputImage, "png", -1, savepath2);

  29.             // method3 -- halcon12进行调试
  30.             HObject ho_Image1 = null;
  31.             HTuple aWidth = 1200, aHeight = 1200;
  32.             HOperatorSet.GenEmptyObj(out ho_Image1);
  33.             ho_Image1.Dispose();
  34.             HOperatorSet.ReadImage(out ho_Image1, ImagePath);
  35.             HOperatorSet.GetImageSize(ho_Image1, out aWidth, out aHeight);
  36.             // 将图像转化为bytes传入
  37.             HTuple hred, hgreen, hblue, type0, width0, height0;
  38.             HOperatorSet.GetImagePointer3(ho_Image1, out hred, out hgreen, out hblue, out type0, out width0, out height0);
  39.             byte[] bptr = new byte[aWidth.I * aHeight.I * ImageChannel];
  40.             for (int i = 0; i < (width0.I * height0.I); i++)
  41.             {
  42.                 Marshal.Copy((IntPtr)(hblue + i), bptr, i * 3 + 0, 1);
  43.                 Marshal.Copy((IntPtr)(hgreen + i), bptr, i * 3 + 1, 1);
  44.                 Marshal.Copy((IntPtr)(hred + i), bptr, i * 3 + 2, 1);
  45.             }
  46.             IntPtr p0 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
  47.             Marshal.Copy(bptr, 0, p0, aWidth.I * aHeight.I * ImageChannel);
  48.             // call
  49.             IntPtr p1 = Marshal.AllocHGlobal(aWidth.I * aHeight.I * ImageChannel);
  50.             p1 = CallAIDLL.PredictRGBImageMat2Intptr(aWidth.I, aHeight.I, ImageChannel, p0, sliceImageSize, overRate, image_threshold, classnum, GPUindex);
  51.             HObject outputImage1 = null;
  52.             HOperatorSet.GenEmptyObj(out outputImage1);
  53.             outputImage1.Dispose();
  54.             HOperatorSet.GenImage1Extern(out outputImage1, "byte", aWidth.I, aHeight.I, p1, Marshal.GetFunctionPointerForDelegate(callback));
  55.             string savepath3 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_3.png";
  56.             HOperatorSet.WriteImage(outputImage1, "png", -1, savepath3);

  57.             // method4 -- halcon12进行调试
  58.             string savepath4 = System.Windows.Forms.Application.StartupPath + @"/" + "rgb_4.png";
  59.             HObject ho_Image2 = null;
  60.             HTuple aWidth1 = 1200, aHeight1 = 1200;
  61.             HOperatorSet.GenEmptyObj(out ho_Image2);
  62.             ho_Image2.Dispose();
  63.             HOperatorSet.ReadImage(out ho_Image2, ImagePath);
  64.             HOperatorSet.GetImageSize(ho_Image2, out aWidth1, out aHeight1);
  65.             // 将图像转化为bytes传入
  66.             HTuple hred1, hgreen1, hblue1, type1, width1, height1;
  67.             HOperatorSet.GetImagePointer3(ho_Image2, out hred1, out hgreen1, out hblue1, out type1, out width1, out height1);
  68.             byte[] bptr1 = new byte[aWidth1.I * aHeight1.I * ImageChannel];
  69.             for (int i = 0; i < (aWidth1.I * aHeight1.I); i++)
  70.             {
  71.                 Marshal.Copy((IntPtr)(hblue1 + i), bptr1, i * 3 + 0, 1);
  72.                 Marshal.Copy((IntPtr)(hgreen1 + i), bptr1, i * 3 + 1, 1);
  73.                 Marshal.Copy((IntPtr)(hred1 + i), bptr1, i * 3 + 2, 1);
  74.             }
  75.             IntPtr p2 = Marshal.AllocHGlobal(aWidth1.I * aHeight1.I * ImageChannel);
  76.             Marshal.Copy(bptr1, 0, p2, aWidth1.I * aHeight1.I * ImageChannel);
  77.             // call
  78.             CallAIDLL.PredictRGBImageMat2Path(aWidth1, aHeight1, ImageChannel, p2, savepath4, sliceImageSize, overRate, image_threshold, classnum, GPUindex);

  79.             //
  80.             MessageBox.Show("OK");
复制代码




回复

使用道具 举报

83

主题

111

帖子

653

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
653
 楼主| 发表于 2021-11-22 22:57:43 | 显示全部楼层
模型释放:
  1.             string AImodelpt = @"rgb_model.WITIAI";
  2.             CallAIDLL1.Free_module(AImodelpt, 0);
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Opencv|MATLAB|Python|WiTiAi ( 蜀ICP备2021019707号 )

GMT+8, 2024-4-16 21:10 , Processed in 0.089017 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表