SqueezeBrains SDK 1.13
RetinaMultiProject.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using sb_cs;
7using Tutorials_Common;
8
10{
29 {
30 static SbError execute()
31 {
32 SbProject slice = null;
33 SbProject hole = null;
34 SbRoi roi = null;
35 SbImage img = null;
36 SbError err = SbError.SB_ERR_NONE;
37
39 Console.WriteLine("Sb Init");
40 err = Sb.Init("../../../sb.lic");
41 if (err != SbError.SB_ERR_NONE)
42 {
43 Console.WriteLine("Sb.Init failed with error " + err);
44 return err;
45 }
46
48 Console.WriteLine("Wait for license...");
49 err = Common.WaitLicense();
50 if (err != SbError.SB_ERR_NONE)
51 {
52 Console.WriteLine("SbLicense.WaitLicense failed with error " + err);
53 goto FnExit;
54 }
55
58 String solution_file = "../../solution/tutorial_7_retina_multi_project." + SbDefines.SbSolutionExt;
59 SbSolutionInfo solutionInfo = SbSolution.GetInfo(solution_file);
60 if (solutionInfo == null || solutionInfo.Error() != SbError.SB_ERR_NONE)
61 {
62 err = solutionInfo == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : solutionInfo.Error();
63 Console.WriteLine("SbSolution.GetInfo failed");
64 goto FnExit;
65 }
66 else if (solutionInfo.projects.Length == 0)
67 {
68 err = SbError.SB_ERR_PROJECT_NOT_FOUND;
69 Console.WriteLine("SbSolution.GetInfo no projects found");
70 goto FnExit;
71 }
72 Console.WriteLine("SbSolutionInfo.GetInfo, found " + solutionInfo.projects.Length + " projects");
73
75 slice = SbProject.Load(solution_file, solutionInfo.projects[0].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
76 if (slice == null || slice.Error() != SbError.SB_ERR_NONE)
77 {
78 err = slice == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : slice.Error();
79 Console.WriteLine("SbProject.Load failed with error " + err);
80 goto FnExit;
81 }
82
84 hole = SbProject.Load(solution_file, solutionInfo.projects[1].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
85 if (hole == null || hole.Error() != SbError.SB_ERR_NONE)
86 {
87 err = hole == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : hole.Error();
88 Console.WriteLine("SbProject.Load failed with error " + err);
89 goto FnExit;
90 }
91
92 SbFolder folder = SbFolder.Load("../../dataset", "png", true, 0);
93 if (folder != null)
94 {
95 for (int i = 0; i < folder.files.Length; i++)
96 {
97 // Load the image from file.
98 img = SbImage.Load(folder.files[i]);
99 if (img == null || img.Error() != SbError.SB_ERR_NONE)
100 {
101 Console.WriteLine("SbImage.Load " + folder.files[i] + " failed");
102 err = img == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : img.Error();
103 break;
104 }
105
106 // Create the ROI
107 roi = SbRoi.Create(img.Width(), img.Height());
108 if (roi == null || roi.Error() != SbError.SB_ERR_NONE)
109 {
110 Console.WriteLine("SbRoi.Create failed");
111 err = roi == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : roi.Error();
112 break;
113 }
114
115 // Set a rectangular ROI equal to image dimensions.
116 err = roi.SetRect(255, new SbRect(0, 0, img.Width(), img.Height()), true);
117 if (err != SbError.SB_ERR_NONE)
118 {
119 Console.WriteLine("SbRoi.SetRect failed with error " + err);
120 break;
121 }
122
124 err = slice.Detection(img, roi);
125 if (err != SbError.SB_ERR_NONE)
126 {
127 Console.WriteLine("SbProject.Detection failed with error " + err);
128 break;
129 }
130
131 // Get the results.
132 SbRes res_slice = slice.GetRes(false);
133 if (res_slice == null || slice.Error() != SbError.SB_ERR_NONE)
134 {
135 err = res_slice == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : slice.Error();
136 Console.WriteLine("SbProject.GetRes failed with error " + err);
137 break;
138 }
139
140 Console.WriteLine(" slice detection time=" + (res_slice.timeUs / 1000.0f) + "ms, " + res_slice.samples.Length + " slices found");
141
142 for (int j = 0; j < res_slice.samples.Length; ++j)
143 {
145 err = roi.SetRect(255, new SbRect(res_slice.samples[j].centre.x - 115, res_slice.samples[j].centre.y - 65, 230, 130), true);
146 if (err != SbError.SB_ERR_NONE)
147 {
148 Console.WriteLine("SbRoi.SetRect failed with error " + err);
149 break;
150 }
151
153 err = hole.Detection(img, roi);
154 if (err != SbError.SB_ERR_NONE)
155 {
156 Console.WriteLine("SbProject.Detection failed with error " + err);
157 break;
158 }
159
160 // Get the results.
161 SbRes res_hole = hole.GetRes(false);
162 if (res_hole == null || hole.Error() != SbError.SB_ERR_NONE)
163 {
164 err = res_hole == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : hole.Error();
165 Console.WriteLine("SbProject.GetRes failed with error " + err);
166 break;
167 }
168 Console.WriteLine(" slice " + j + ": hole detection time=" + (res_hole.timeUs / 1000.0f) + ", " + res_hole.samples.Length + " holes found");
169 // Print the results
170 for (int k = 0; k < res_hole.samples.Length; ++k)
171 Console.WriteLine(" centre=(" + res_hole.samples[k].centre.x + "," + res_hole.samples[k].centre.y + ") weight=" + res_hole.samples[k].weight);
172
173 }
174 if (img != null)
175 {
176 img.Dispose();
177 img = null;
178 }
179 if (roi != null)
180 {
181 roi.Dispose();
182 roi = null;
183 }
184 }
185 }
186
187 FnExit:
188
190 if (slice != null)
191 slice.Dispose();
192 if (hole != null)
193 hole.Dispose();
194 if (img != null)
195 img.Dispose();
196 if (roi != null)
197 roi.Dispose();
198
199 Console.WriteLine("Release SqueezeBrains library");
200 Sb.Release();
201
202 Console.WriteLine("Press ENTER to terminate");
203 Console.ReadKey();
204
205 return err;
206 }
207
208 static void Main(string[] args)
209 {
210 execute();
211 }
212 }
213}
Tutorial 7 - Retina - How to use more than one project to detect objects.
SbError Error()
Returns the error code of the last operation. If no error SbError.SB_ERR_NONE is returned.
Defines class
Definition: cs_common.h:258
static const String SbSolutionExt
Solution file extension
Definition: cs_common.h:264
Folder Class that wraps the sb_t_folder structure
Definition: cs_folder.h:16
static SbFolder Load(String^ path, String ^ext, bool sort, int verbosity)
Creates the list of the name of the files in a specified folder.
array< String^> files
Array of files.
Definition: cs_folder.h:22
Sb Main Class
Definition: cs_sb.h:374
static SbError Release()
Releases all the resources allocated in the library
static SbError Init(String ^ license_file)
Init the SB Library. The function initializes all the functionalities of the library including the li...
SbImage class that wraps the sb_t_image structure. You must call the Dispose() method to free all the...
Definition: cs_image.h:66
int Height()
Height, in pixel, of the image.
static SbImage Load(String^ filename)
Loads an image from file.
int Width()
Width, in pixel, of the image.
Project Class You must call the Dispose() method to free all the resources of the returned instance.
Definition: cs_project.h:44
static SbProject Load(String^ solution_file, String^ project_uuid, SbProjectMode mode)
Loads an existing project from file.
SbRes GetRes(bool details)
Retrieves the results of the last processed image
SbError Detection(SbImage ^img, SbRoi ^roi)
The function elaborates the image inside the ROI. For Surface projects the function computes also the...
Rectangle class that wraps the sb_t_rect structure
Definition: cs_sb.h:224
Class of the results of the image elaboration with the SbProject::Detection method....
Definition: cs_res.h:225
long long timeUs
Detection time
Definition: cs_res.h:261
array< SbSample^> samples
Samples results. Only Retina projects
Definition: cs_res.h:246
ROI Class that wraps the sb_t_roi structure. You must call the Dispose() method to free all the resou...
Definition: cs_roi.h:18
static SbRoi Create(int width, int height)
Creates a ROI.
SbError SetRect(int gl, SbRect^ rect, bool reset_roi)
Sets a rectangular ROI.
Solution class
Definition: cs_solution.h:44
static SbSolutionInfo GetInfo(String^ solution_file)
Returns the information contained in the solution_file.
Solution Info Class that wraps the sb_t_solution_info structure
Definition: cs_solution.h:17
array< SbProjectInfo^> projects
Array of the projects information of the solution. sb_t_solution_info.projects
Definition: cs_solution.h:23
SbError
Enum error codes
Definition: cs_common.h:13
SbProjectMode
Project loading or saving mode that wraps the sb_t_project_mode enum
Definition: cs_project.h:18
SB Namespace
Definition: cs_common.h:3