visionpro脚本

  1 #region namespace imports
  2 using System;
  3 using System.Collections;
  4 using System.Drawing;
  5 using System.IO;
  6 using System.Windows.Forms;
  7 using Cognex.VisionPro;
  8 using Cognex.VisionPro.ToolBlock;
  9 using Cognex.VisionPro3D;
 10 using System.Collections.Generic;
 11 using Cognex.GC.DeepLearning;
 12 using ViDi2;
 13 #endregion
 14 
 15 public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
 16 {
 17   #region Private Member Variables
 18   private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
 19   #endregion
 20 
 21   /// <summary>
 22   /// Called when the parent tool is run.
 23   /// Add code here to customize or replace the normal run behavior.
 24   /// </summary>
 25   /// <param name="message">Sets the Message in the tool's RunStatus.</param>
 26   /// <param name="result">Sets the Result in the tool's RunStatus</param>
 27   /// <returns>True if the tool should run normally,
 28   ///          False if GroupRun customizes run behavior</returns>
 29   public override bool GroupRun(ref string message, ref CogToolResultConstants result)
 30   {
 31     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
 32     #if DEBUG
 33     if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
 34     #endif
 35 
 36     // Run each tool using the RunTool function
 37     foreach (ICogTool tool in mToolBlock.Tools)
 38     {
 39       //bool isInitializeWork
 40       if (tool.Name == "InitializeTb")
 41       {
 42         CogToolBlock tb_Initial = (CogToolBlock) tool;
 43         if((bool)tb_Initial.Inputs["isDispose"].Value)
 44         {
 45           if(Cognex.GC.DeepLearning.GlobalParameter.control != null)
 46           {
 47               Cognex.GC.DeepLearning.Dispose.disposed();
 48               GlobalParameter.control = null;
 49           }
 50 
 51           return false;
 52         }
 53         if ((bool) tb_Initial.Inputs["isInitializeWorkSpace"].Value)
 54         {
 55           //Initialize
 56           string WorkspacePath = (string) tb_Initial.Inputs["WorkspacePath"].Value;
 57           string StreamName = (string) tb_Initial.Inputs["StreamName"].Value;
 58           string ToolName = (string) tb_Initial.Inputs["ToolName"].Value;
 59           int Gpu_Index = (int) tb_Initial.Inputs["Gpu_Index"].Value;
 60           Cognex.GC.DeepLearning.Initialize.Initial(WorkspacePath, StreamName, Gpu_Index);
 61           
 62           //update parameters
 63           CogToolBlock tb_Evaluate = mToolBlock.Tools["EvaluateTB"] as CogToolBlock;
 64           while (tb_Evaluate.Inputs.Count > 0)
 65           {
 66             tb_Evaluate.Inputs.RemoveAt(0);
 67           }
 68           System.Type type = typeof(Cognex.VisionPro.ICogImage);
 69           tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("Image", type));
 70 
 71           if (GlobalParameter.stream.Tools[ToolName].Type == ToolType.Blue)
 72           {
 73             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("tool_name", ToolName));
 74             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicScore", true));
 75             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("sampling_Density", -1));
 76             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("threshold", -1.0));
 77             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("precision", -1.0));
 78             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("thickness", 2));
 79             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("fontScale", 1));
 80             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveCsvData", true));
 81             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("CSVPath", ""));
 82             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveImage", true));
 83             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageSaveFolder", ""));
 84             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageName", ""));
 85 
 86           }
 87           if (GlobalParameter.stream.Tools[ToolName].Type == ToolType.BlueRead)
 88           {
 89             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("tool_name", ToolName));
 90             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("thickness", 2));
 91             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("sampling_Density", -1));
 92             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("threshold", -1.0));
 93             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicScore", true));
 94             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("fontScale", 1));
 95             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveCsvData", true));
 96             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("CSVPath", ""));
 97             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveImage", true));
 98             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageSaveFolder", ""));
 99             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageName", ""));
100           }
101           if (GlobalParameter.stream.Tools[ToolName].Type == ToolType.Green || GlobalParameter.stream.Tools[ToolName].Type == ToolType.GreenHighDetail)
102           {
103             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("tool_name", ToolName));
104             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("threshold", -1.0));
105             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("fontScale", 1));
106             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("thickness", 2));
107             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicScore", true));
108             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicHeatmap", true));
109             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("CSVPath", ""));
110             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveCsvData", true));
111             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveImage", true));
112             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageSaveFolder", ""));
113             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageName", ""));
114           }
115           if (GlobalParameter.stream.Tools[ToolName].Type == ToolType.Red || GlobalParameter.stream.Tools[ToolName].Type == ToolType.RedHighDetail)
116           {
117             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("tool_name", ToolName));
118             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("sampling_Density", -1));
119             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("thresholdLow", -1.0));
120             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("thresholdUp", -1.0));
121             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("thickness", 2));
122             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("fontScale", 1));
123             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicArea", true));
124             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicBoundingRect", true));
125             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicContours", true));
126             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isGraphicOverlayImage", true));
127             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveCsvData", true));
128             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("CSVPath", ""));
129             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("isSaveImage", true));
130             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageSaveFolder", ""));
131             tb_Evaluate.Inputs.Add(new CogToolBlockTerminal("AnnotationImageName", ""));
132           }
133         }
134       }
135       if (tool.Name == "EvaluateTB")
136       {
137         CogToolBlock tb_Initial = (CogToolBlock) (mToolBlock.Tools["InitializeTb"]);
138         ((CogToolBlock) (mToolBlock.Tools["EvaluateTB"])).Inputs["Image"].Value = mToolBlock.Inputs[0].Value;
139         if (!(bool) tb_Initial.Inputs["isInitializeWorkSpace"].Value)
140         {
141           mToolBlock.RunTool(tool, ref message, ref result);
142         }
143       }
144     }
145     return false;
146   }
147 
148   #region When the Current Run Record is Created
149   /// <summary>
150   /// Called when the current record may have changed and is being reconstructed
151   /// </summary>
152   /// <param name="currentRecord">
153   /// The new currentRecord is available to be initialized or customized.</param>
154   public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
155   {
156   }
157   #endregion
158 
159   #region When the Last Run Record is Created
160   /// <summary>
161   /// Called when the last run record may have changed and is being reconstructed
162   /// </summary>
163   /// <param name="lastRecord">
164   /// The new last run record is available to be initialized or customized.</param>
165   public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
166   {
167   }
168   #endregion
169 
170   #region When the Script is Initialized
171   /// <summary>
172   /// Perform any initialization required by your script here
173   /// </summary>
174   /// <param name="host">The host tool</param>
175   public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
176   {
177     // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
178     base.Initialize(host);
179 
180 
181     // Store a local copy of the script host
182     this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock) (host));
183   }
184   #endregion
185 
186 }
  1 #region namespace imports
  2 using System;
  3 using System.Collections;
  4 using System.Drawing;
  5 using System.IO;
  6 using System.Windows.Forms;
  7 using Cognex.VisionPro;
  8 using Cognex.VisionPro.ToolBlock;
  9 using Cognex.VisionPro3D;
 10 using ViDi2;
 11 using Cognex.GC.DeepLearning;
 12 using System.Collections.Generic;
 13 using Cognex.VisionPro.ImageProcessing;
 14 #endregion
 15 
 16 public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
 17 {
 18   #region Private Member Variables
 19   private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
 20   BlueRead read = new BlueRead();
 21   BlueLocate locate = new BlueLocate();
 22   GreenClassify green = new GreenClassify();
 23   RedAnalyze red = new RedAnalyze();
 24   #endregion
 25 
 26   /// <summary>
 27   /// Called when the parent tool is run.
 28   /// Add code here to customize or replace the normal run behavior.
 29   /// </summary>
 30   /// <param name="message">Sets the Message in the tool's RunStatus.</param>
 31   /// <param name="result">Sets the Result in the tool's RunStatus</param>
 32   /// <returns>True if the tool should run normally,
 33   ///          False if GroupRun customizes run behavior</returns>
 34   public override bool GroupRun(ref string message, ref CogToolResultConstants result)
 35   {
 36     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
 37     #if DEBUG
 38     if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
 39     #endif
 40 
 41 
 42     // Run each tool using the RunTool function
 43     //    foreach(ICogTool tool in mToolBlock.Tools)
 44     //      mToolBlock.RunTool(tool, ref message, ref result);
 45 
 46     //Init tool
 47     BlueRead read = new BlueRead();
 48     BlueLocate locate = new BlueLocate();
 49     GreenClassify green = new GreenClassify();
 50     RedAnalyze red = new RedAnalyze();
 51     
 52     //image convert
 53     ICogImage cogImage = mToolBlock.Inputs["Image"].Value as ICogImage;
 54     IImage iImage = new ViDi2.VisionPro.Image(cogImage);
 55     Bitmap bitmap = iImage.Bitmap;
 56     
 57     //remove allResult
 58     while(mToolBlock.Outputs.Count > 0)
 59       mToolBlock.Outputs.RemoveAt(0);
 60     
 61 
 62     
 63     //run tools
 64     if (GlobalParameter.stream.Tools[(string) mToolBlock.Inputs["tool_name"].Value].Type == ToolType.Blue)
 65     {
 66       List<Tuple<List<double>, List<ViDi2.Point>, List<ViDi2.Rect>, IImage>> listResult = new List<Tuple<List<double>, List<ViDi2.Point>, List<ViDi2.Rect>, IImage>>();
 67       locate.EvaluateWithGraphic(iImage,
 68         (string) mToolBlock.Inputs["tool_name"].Value,
 69         ref listResult, 
 70         ref bitmap,
 71         (bool) mToolBlock.Inputs["isGraphicScore"].Value,
 72         (int)mToolBlock.Inputs["sampling_Density"].Value,
 73         (float) mToolBlock.Inputs["threshold"].Value,
 74         (float) mToolBlock.Inputs["precision"].Value,
 75         (int)mToolBlock.Inputs["thickness"].Value,
 76         (int)mToolBlock.Inputs["fontScale"].Value,
 77         (bool)mToolBlock.Inputs["isSaveCsvData"].Value,
 78         (string)mToolBlock.Inputs["CSVPath"].Value,
 79         (bool)mToolBlock.Inputs["isSaveImage"].Value,
 80         (string)mToolBlock.Inputs["AnnotationImageSaveFolder"].Value,
 81         (string)mToolBlock.Inputs["AnnotationImageName"].Value
 82         );
 83       mToolBlock.Outputs.Add(new CogToolBlockTerminal("ResultData", listResult));
 84 
 85     }
 86     if (GlobalParameter.stream.Tools[(string) mToolBlock.Inputs["tool_name"].Value].Type == ToolType.BlueRead)
 87     {
 88 
 89       List<List<Tuple<double, string, List<ViDi2.Point>>>> listResult = new List<List<Tuple<double, string, List<ViDi2.Point>>>>();
 90       read.EvaluateWithGraphic(iImage,
 91         (string) mToolBlock.Inputs["tool_name"].Value,
 92         ref listResult,
 93         ref bitmap,
 94         (int)mToolBlock.Inputs["thickness"].Value,
 95         (int)mToolBlock.Inputs["sampling_Density"].Value,
 96         (double) mToolBlock.Inputs["threshold"].Value,
 97         (bool) mToolBlock.Inputs["isGraphicScore"].Value,
 98         (int)mToolBlock.Inputs["fontScale"].Value,
 99         (bool)mToolBlock.Inputs["isSaveCsvData"].Value,
100        (string)mToolBlock.Inputs["CSVPath"].Value,
101        (bool)mToolBlock.Inputs["isSaveImage"].Value,
102         (string)mToolBlock.Inputs["AnnotationImageSaveFolder"].Value,
103        (string)mToolBlock.Inputs["AnnotationImageName"].Value
104         );
105       mToolBlock.Outputs.Add(new CogToolBlockTerminal("ResultData", listResult));
106 
107             
108     }
109     if (GlobalParameter.stream.Tools[(string) mToolBlock.Inputs["tool_name"].Value].Type == ToolType.Green || GlobalParameter.stream.Tools[(string) mToolBlock.Inputs["tool_name"].Value].Type == ToolType.GreenHighDetail)
110     {
111 
112       List<Tuple<double, string, IImage>> listResult = new List<Tuple<double, string, IImage>>();
113       green.EvaluateWithGraphicAndResult(iImage,
114         (string) mToolBlock.Inputs["tool_name"].Value,
115         ref bitmap,
116         ref listResult,
117         (double) mToolBlock.Inputs["threshold"].Value,
118         (int)mToolBlock.Inputs["thickness"].Value,
119         (int)mToolBlock.Inputs["fontScale"].Value,
120         (bool) mToolBlock.Inputs["isGraphicScore"].Value,
121         (bool) mToolBlock.Inputs["isGraphicHeatmap"].Value,
122         (string)mToolBlock.Inputs["CSVPath"].Value,
123         (bool)mToolBlock.Inputs["isSaveCsvData"].Value,
124         (bool)mToolBlock.Inputs["isSaveImage"].Value,
125         (string)mToolBlock.Inputs["AnnotationImageSaveFolder"].Value,
126         (string)mToolBlock.Inputs["AnnotationImageName"].Value
127         );
128       mToolBlock.Outputs.Add(new CogToolBlockTerminal("ResultData", listResult));
129 
130    
131     }
132     if (GlobalParameter.stream.Tools[(string) mToolBlock.Inputs["tool_name"].Value].Type == ToolType.Red || GlobalParameter.stream.Tools[(string) mToolBlock.Inputs["tool_name"].Value].Type == ToolType.RedHighDetail)
133     {
134       //1.view的分数;2.缺陷的中心点;3.缺陷最小外接矩形的四个角点;4.缺陷的面积;5.缺陷的周长;6.缺陷的轮廓点 7.view image; 8.overlay image;
135       List<Tuple<double, List<ViDi2.Point>, List<ViDi2.Point[]>, List<double>, List<double>, List<ViDi2.Point[]>, Tuple<IImage, IRedView>>> listResult = new List<Tuple<double, List<ViDi2.Point>, List<ViDi2.Point[]>, List<double>, List<double>, List<ViDi2.Point[]>, Tuple<IImage, IRedView>>>();
136       red.EvaluateWithGraphicAndResult(iImage,
137         (string) mToolBlock.Inputs["tool_name"].Value,
138         ref listResult,
139         ref bitmap,
140         (int)mToolBlock.Inputs["sampling_Density"].Value,
141         (double) mToolBlock.Inputs["thresholdLow"].Value,
142         (double) mToolBlock.Inputs["thresholdUp"].Value,
143         (int)mToolBlock.Inputs["thickness"].Value,
144         (int)mToolBlock.Inputs["fontScale"].Value,
145         (bool) mToolBlock.Inputs["isGraphicArea"].Value,
146         (bool) mToolBlock.Inputs["isGraphicBoundingRect"].Value,
147         (bool) mToolBlock.Inputs["isGraphicContours"].Value,
148         (bool) mToolBlock.Inputs["isGraphicOverlayImage"].Value,
149         (bool)mToolBlock.Inputs["isSaveCsvData"].Value,
150         (string)mToolBlock.Inputs["CSVPath"].Value,
151         (bool)mToolBlock.Inputs["isSaveImage"].Value,
152         (string)mToolBlock.Inputs["AnnotationImageSaveFolder"].Value,
153         (string)mToolBlock.Inputs["AnnotationImageName"].Value
154         );
155       mToolBlock.Outputs.Add(new CogToolBlockTerminal("ResultData", listResult));
156     }
157     
158     //add RawImage
159     mToolBlock.Outputs.Add(new CogToolBlockTerminal("RawImage", cogImage));
160     
161     //AddResultImg
162     Cognex.VisionPro.CogImage24PlanarColor resultImg = new CogImage24PlanarColor(bitmap);
163     mToolBlock.Outputs.Add(new CogToolBlockTerminal("ResultImage", resultImg));
164 
165     //Display
166     CogIPOneImageTool RawDisplay = mToolBlock.Tools["RawDisplay"] as CogIPOneImageTool;
167     RawDisplay.InputImage = cogImage;
168     RawDisplay.Run();
169     
170     CogIPOneImageTool ResultDisplay = mToolBlock.Tools["ResultDisplay"] as CogIPOneImageTool;
171     ResultDisplay.InputImage = resultImg;
172     ResultDisplay.Run();
173 
174 
175     return false;
176   }
177 
178   #region When the Current Run Record is Created
179   /// <summary>
180   /// Called when the current record may have changed and is being reconstructed
181   /// </summary>
182   /// <param name="currentRecord">
183   /// The new currentRecord is available to be initialized or customized.</param>
184   public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
185   {
186   }
187   #endregion
188 
189   #region When the Last Run Record is Created
190   /// <summary>
191   /// Called when the last run record may have changed and is being reconstructed
192   /// </summary>
193   /// <param name="lastRecord">
194   /// The new last run record is available to be initialized or customized.</param>
195   public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
196   {
197   }
198   #endregion
199 
200   #region When the Script is Initialized
201   /// <summary>
202   /// Perform any initialization required by your script here
203   /// </summary>
204   /// <param name="host">The host tool</param>
205   public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
206   {
207     // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
208     base.Initialize(host);
209 
210 
211     // Store a local copy of the script host
212     this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock) (host));
213     
214   }
215   #endregion
216 
217 }

 

posted @ 2021-02-02 13:34  荼离伤花  阅读(243)  评论(0编辑  收藏  举报