SqueezeBrains SDK 1.13
DeepSurfaceDetect.cs
1using System;
2using sb_cs;
3using Tutorials_Common;
4
6{
43 {
44 static SbError execute()
45 {
46 SbProject prj = null;
47 SbImage img = null;
48 SbRoi roi = null;
49 SbRes res = null;
50 SbError err = SbError.SB_ERR_NONE;
51
53 Console.WriteLine("Sb Init");
54 err = Sb.Init("../../../sb.lic");
55 if (err != SbError.SB_ERR_NONE)
56 {
57 Console.WriteLine("Sb.Init failed with error " + err);
58 return err;
59 }
60
62 Console.WriteLine("Wait for license...");
63 err = Common.WaitLicense();
64 if (err != SbError.SB_ERR_NONE)
65 {
66 Console.WriteLine("SbLicense.WaitLicense failed with error " + err);
67 goto FnExit;
68 }
69
71 err = Sb.InitDl("../../../../../win_x64/dl_framework");
72 if (err != SbError.SB_ERR_NONE)
73 {
74 Console.WriteLine("Sb.InitDl failed with error " + err);
75 return err;
76 }
77
79 String solution_file = "../../solution/deep_surface_tutorial." + SbDefines.SbSolutionExt;
80 SbSolutionInfo solutionInfo = SbSolution.GetInfo(solution_file);
81 if (solutionInfo == null || solutionInfo.Error() != SbError.SB_ERR_NONE)
82 {
83 err = solutionInfo == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : solutionInfo.Error();
84 Console.WriteLine("SbSolution.GetInfo failed");
85 goto FnExit;
86 }
87 else if (solutionInfo.projects.Length == 0)
88 {
89 err = SbError.SB_ERR_PROJECT_NOT_FOUND;
90 Console.WriteLine("SbSolution.GetInfo no projects found");
91 goto FnExit;
92 }
93 Console.WriteLine("GetInfo, found " + solutionInfo.projects.Length + " project/s");
94
96 prj = SbProject.Load(solution_file, solutionInfo.projects[0].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
97 if (prj == null || prj.Error() != SbError.SB_ERR_NONE)
98 {
99 err = prj == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
100 Console.WriteLine("SbProject.Load failed with error " + err);
101 goto FnExit;
102 }
103
107 SbPar par = prj.GetPar();
108 // blob parameters
109 par.surface_blob_analysis = 1;
110 par.blob_par.area.min = 10;
111 par.blob_par.merge_area.min = 50;
112 par.blob_par.blob_rle = 1;
113 par.blob_par.blob_contour = 1;
114 par.blob_par.merge_distance = 20;
115 //par.surface_blob_analysis = 1;
116 // devices parameters
117 par.devices.type = SbDeviceType.SB_DEVICE_GPU; // set GPU device
118 par.devices.id[0] = 0; // set the first GPU founded on the machine (with index 0)
119 err = prj.SetPar(par);
120 if (err != SbError.SB_ERR_NONE || prj.Error() != SbError.SB_ERR_NONE)
121 {
122 Console.WriteLine("SbProject.SetPar failed with error " + err);
123 goto FnExit;
124 }
125
126 SbFolder folder = SbFolder.Load("../../dataset", "png", true, 0);
127 if (folder != null)
128 {
129 for (int i = 0; i < folder.files.Length; i++)
130 {
132 img = SbImage.Load(folder.files[i]);
133 if (img == null || img.Error() != SbError.SB_ERR_NONE)
134 {
135 Console.WriteLine("SbImage.Load " + folder.files[i] + " failed");
136 err = img == null ? SbError.SB_ERR_INTERNAL : img.Error();
137 break;
138 }
139
141 roi = SbRoi.Create(img.Width(), img.Height());
142 if (roi == null || roi.Error() != SbError.SB_ERR_NONE)
143 {
144 Console.WriteLine("SbRoi.Create failed");
145 err = roi == null ? SbError.SB_ERR_INTERNAL : roi.Error();
146 goto FnExit;
147 }
148
150 err = roi.SetRect(255, new SbRect(0, 0, img.Width(), img.Height()), false);
151 if (err != SbError.SB_ERR_NONE)
152 {
153 Console.WriteLine("SbRoi.SetRect failed with error " + err);
154 goto FnExit;
155 }
156
158 err = prj.Detection(img, roi);
159 if (err != SbError.SB_ERR_NONE)
160 {
161 Console.WriteLine("SbProject.Detection failed with error " + err);
162 break;
163 }
164
166 res = prj.GetRes(false);
167 if (res == null || prj.Error() != SbError.SB_ERR_NONE)
168 {
169 err = res == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
170 Console.WriteLine("SbProject.GetRes failed with error " + err);
171 break;
172 }
173
175 Console.WriteLine("image: " + folder.files[i]);
176 Console.WriteLine("\t" + res.global.surface.blobs.Length + " defect blobs found:");
177 for(int j = 0; j < res.global.surface.blobs.Length; j++)
178 {
179 Console.WriteLine("\t\tul=(" + res.global.surface.blobs[j].rect.x + "," + res.global.surface.blobs[j].rect.y +
180 "), size=(" + res.global.surface.blobs[j].rect.width + "," + res.global.surface.blobs[j].rect.height +
181 "), area=" + res.global.surface.blobs[j].area);
182 }
183
184 if (img != null)
185 {
186 img.Dispose();
187 img = null;
188 }
189 if (roi != null)
190 {
191 roi.Dispose();
192 roi = null;
193 }
194 if (res != null)
195 {
196 res.Dispose();
197 res = null;
198 }
199 }
200 }
201
202 FnExit:
204 if (prj != null)
205 prj.Dispose();
206 if (img != null)
207 img.Dispose();
208 if (roi != null)
209 roi.Dispose();
210 if (res != null)
211 res.Dispose();
212
213 Console.WriteLine("Release SqueezeBrains library");
214 Sb.Release();
215
216 Console.WriteLine("Press ENTER to terminate");
217 Console.ReadKey();
218
219 return err;
220 }
221
222 static void Main(string[] args)
223 {
224 execute();
225 }
226 }
227}
Tutorial 3 - Deep Cortex - How to elaborate images
int blob_rle
Set to a value != 0 if you want the rle of the shape each blob.
Definition: cs_blob.h:51
int merge_distance
blobs with a distance inferior or equal to the value are merged together.
Definition: cs_blob.h:68
int blob_contour
Set to a value != 0 if you want the contour of each blob.
Definition: cs_blob.h:52
SbRange merge_area
Range of area after merge, value in pixel.
Definition: cs_blob.h:69
SbRange area
Range of area, value in pixel.
Definition: cs_blob.h:47
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
array< int > id
Identifier of the devices to be used
Definition: cs_par.h:254
SbDeviceType type
Device computational type
Definition: cs_par.h:248
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 InitDl(String^ search_path)
Init the Deep Learning SB Library. The function enables the SbProject::Detection and SVL functions fo...
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.
Parameters Class that wraps the sb_t_par structure. You must call the Dispose() method to free all th...
Definition: cs_par.h:657
SbBlobPar blob_par
Blob analysis parameters. Used only for Surface projects
Definition: cs_par.h:687
SbDevicesPar devices
Devices used for inference.
Definition: cs_par.h:693
int surface_blob_analysis
Enable the surface blob analysis
Definition: cs_par.h:717
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...
SbPar GetPar()
Retrieves the project parameters structure.
SbError SetPar(SbPar^ par)
Sets the project parameters.
int min
mininum value
Definition: cs_sb.h:138
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
SbResModel global
Global results
Definition: cs_res.h:231
SbSurfaceRes surface
: Surface specific results.
Definition: cs_res.h:132
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
array< SbBlob^> blobs
Array of the blobs found
Definition: cs_res.h:69
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
SbDeviceType
Device type that wraps the sb_t_device_type enum
Definition: cs_common.h:181
SB Namespace
Definition: cs_common.h:3