在VisionPro中,常见的操作是:
将产品多种型号分别定义成多个QuickBuild。(PS: 产品A调用QuickBuild1,产品B调用QuickBuild2)
一个QuickBuild(JobManager)文件下可以包括若干个作业(Job),如果一个项目连接多个工业相机,那么可以将相机单独绑定到Job上。
Job中可以包含采像工具、ToolBlock、ToolGroup和各种算法工具。
结果输出,可以通过添加已发送项的方式,将单个Job执行结果输出。
这里主要实现模拟四个相机从分别读取OCR字符,然后将字符的个数打印到CogRecordDisplay的左上角。效果如下:
接下来,上代码:
实例化CogManager(作业管理器)、CogJob(作业)、CogJobIndependent(类似于TaskHelper的存在,欢迎纠错)对象。
1 private CogJobManager myJobManager;
2 private List<CogJob> myJobs = new List<CogJob>();
3 private List<CogJobIndependent> myIndependentJobs = new List<CogJobIndependent>();
初始化对象,并订阅结果可用事件(UserResultAvailable)
1 myJobManager = CogSerializer.LoadObjectFromFile(Application.StartupPath + @"\QuickBuild\QuickBuild1.vpp") as CogJobManager;
2 //将作业管理器中的作业遍历到作业集合中
3 for (int i = 0; i < myJobManager.JobCount; i++)
4 {
5 myJobs.Add(myJobManager.Job(i));
6 myIndependentJobs.Add(myJobs[i].OwnedIndependent);
7 }
8 myJobManager.UserQueueFlush(); //用户队列刷新
9 myJobManager.FailureQueueFlush(); //故障队列刷新
10 for (int i = 0; i < myJobManager.JobCount; i++)
11 {
12 myJobs[i].ImageQueueFlush();
13 myIndependentJobs[i].RealTimeQueueFlush();
14 }
15 this.Invoke(new Action(() =>
16 {
17 cogJobManagerEdit1.Subject = myJobManager; //将工程文件显示到JobManager控件中
18 }));
19
20 //订阅完成事件
21 myJobManager.Stopped += new CogJobManager.CogJobManagerStoppedEventHandler((sender, e) => { });
22 //订阅结果可用事件
23 myJobManager.UserResultAvailable += new CogJobManager.CogUserResultAvailableEventHandler(OnUserResultAvailable);
在OnUserResultAvailable事件的回调函数中,添加显示、获取结果代码
1 private void OnUserResultAvailable(object sender, CogJobManagerActionEventArgs e)
2 {
3 //由于CogManager对象是在异步线程中创建的,所以需要使用主线程的Invoke()方法调用
4 this.Invoke(new Action(() =>
5 {
6 ICogRecord record = myJobManager.UserResult();
7 ICogRecord tmpRecord;
8 switch (record.SubRecords["JobName"].Content.ToString())
9 {
10 case "Camera_1":
11 //获取当前Job的已发送项结果
12 tmpRecord = record.SubRecords["StringLength"];
13 lbl_ResultStrLength1.Text = tmpRecord.Content.ToString();
14
15 tmpRecord = record.SubRecords["ShowLastRunRecordForUserQueue"];
16 tmpRecord = tmpRecord.SubRecords["LastRun"];
17 tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"];
18 cogRecordDisplay4.InteractiveGraphics.Clear();
19 cogRecordDisplay4.Record = tmpRecord;
20 cogRecordDisplay4.Fit(true);
21 break;
22 case "Camera_2":
23 tmpRecord = record.SubRecords["StringLength"];
24 lbl_ResultStrLength2.Text = tmpRecord.Content.ToString();
25
26 tmpRecord = record.SubRecords["ShowLastRunRecordForUserQueue"];
27 tmpRecord = tmpRecord.SubRecords["LastRun"];
28 tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"]; cogRecordDisplay5.InteractiveGraphics.Clear();
29 cogRecordDisplay5.Record = tmpRecord;
30 cogRecordDisplay5.Fit(true);
31 break;
32 case "Camera_3":
33 tmpRecord = record.SubRecords["StringLength"];
34 lbl_ResultStrLength3.Text = tmpRecord.Content.ToString();
35
36 tmpRecord = record.SubRecords["ShowLastRunRecordForUserQueue"];
37 tmpRecord = tmpRecord.SubRecords["LastRun"];
38 tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"];
39 cogRecordDisplay7.InteractiveGraphics.Clear();
40 cogRecordDisplay7.Record = tmpRecord;
41 cogRecordDisplay7.Fit(true);
42 break;
43 case "Camera_4":
44 tmpRecord = record.SubRecords["StringLength"];
45 lbl_ResultStrLength4.Text = tmpRecord.Content.ToString();
46
47 tmpRecord = record.SubRecords["ShowLastRunRecordForUserQueue"];
48 tmpRecord = tmpRecord.SubRecords["LastRun"];
49 tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"];
50 cogRecordDisplay6.InteractiveGraphics.Clear();
51 cogRecordDisplay6.Record = tmpRecord;
52 cogRecordDisplay6.Fit(true);
53 break;
54 }
55 }));
56 }
执行CogManager作业管理器
1 private void btn_QuickBuildRun_Click(object sender, EventArgs e)
2 {
3 myJobManager.Run();
4 }
如何使用ToolGroup?
以上面的例子为例,可以在每个Job中都通过引入CogOCRMaxTool工具LineResult.Count引脚的方式获取字符总数。也可以通过在ToolGroup中添加脚本的方式获取。
在初始化函数 Initialize()中,添加输入输出引脚
1 public override void Initialize(CogToolGroup host)
2 {
3 // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
4 base.Initialize(host);
5
6 this.toolGroup.DefineScriptTerminal( input,"Input", true);
7 this.toolGroup.DefineScriptTerminal( output,"Output", false);
8 }
定义输入输出变量,在GroupRun()方法中获取输入值,并判断字符个数后输出
1 public string input = "";
2 public int output;
3 public override bool GroupRun(ref string message, ref CogToolResultConstants result)
4 {
5 string str = this.toolGroup.GetScriptTerminalData("Input").ToString();
6 output = str.Length;
7 this.toolGroup.SetScriptTerminalData("Output",output);
8
9 return false;
10 }
连接输入、输出 (可以传递给其他工具,也可以直接添加到已发送项) 引脚
关于ToolGroup和ToolBlock的使用和区别,后面会更新详细的文档。