SqueezeBrains SDK 1.18
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 SbPar par = null;
51 SbError err = SbError.SB_ERR_NONE;
52
54 Console.WriteLine("Sb Init");
55 err = Sb.Init("../../../sb.lic");
56 if (err != SbError.SB_ERR_NONE)
57 {
58 Console.WriteLine("Sb.Init failed with error " + err);
59 return err;
60 }
61
63 Console.WriteLine("Wait for license...");
64 err = Common.WaitLicense();
65 if (err != SbError.SB_ERR_NONE)
66 {
67 Console.WriteLine("SbLicense.WaitLicense failed with error " + err);
68 goto FnExit;
69 }
70
72 err = Sb.InitDl("../../../../../win_x64/dl_framework", SbDlLibraryType.SB_DL_LIBRARY_PYTORCH);
73 if (err != SbError.SB_ERR_NONE)
74 {
75 Console.WriteLine("Sb.InitDl failed with error " + err);
76 return err;
77 }
78
80 String solution_file = "../../solution/deep_surface_tutorial." + SbDefines.SbSolutionExt;
81 SbSolutionInfo solutionInfo = SbSolution.GetInfo(solution_file);
82 if (solutionInfo == null || solutionInfo.Error() != SbError.SB_ERR_NONE)
83 {
84 err = solutionInfo == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : solutionInfo.Error();
85 Console.WriteLine("SbSolution.GetInfo failed");
86 goto FnExit;
87 }
88 else if (solutionInfo.projects.Length == 0)
89 {
90 err = SbError.SB_ERR_PROJECT_NOT_FOUND;
91 Console.WriteLine("SbSolution.GetInfo no projects found");
92 goto FnExit;
93 }
94 Console.WriteLine("GetInfo, found " + solutionInfo.projects.Length + " project/s");
95
97 prj = SbProject.Load(solution_file, solutionInfo.projects[0].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
98 if (prj == null || prj.Error() != SbError.SB_ERR_NONE)
99 {
100 err = prj == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
101 Console.WriteLine("SbProject.Load failed with error " + err);
102 goto FnExit;
103 }
104
108 par = prj.GetPar();
109 // blob parameters
110 par.surface_blob_analysis = 1;
111 par.blob_par.area.min = 10;
112 par.blob_par.merge_area.min = 50;
113 par.blob_par.blob_rle = 1;
114 par.blob_par.blob_contour = 1;
115 par.blob_par.merge_distance = 20;
116 //par.surface_blob_analysis = 1;
117 // devices parameters
118 par.devices.type = SbDeviceType.SB_DEVICE_GPU_NVIDIA; // set GPU device
119 par.devices.id[0] = 0; // set the first GPU founded on the machine (with index 0)
120 err = prj.SetPar(par);
121 if (err != SbError.SB_ERR_NONE || prj.Error() != SbError.SB_ERR_NONE)
122 {
123 Console.WriteLine("SbProject.SetPar failed with error " + err);
124 goto FnExit;
125 }
126
127 SbFolder folder = SbFolder.Load("../../dataset", "png", true, 0);
128 if (folder != null)
129 {
130 for (int i = 0; i < folder.files.Length; i++)
131 {
133 img = SbImage.Load(folder.files[i]);
134 if (img == null || img.Error() != SbError.SB_ERR_NONE)
135 {
136 Console.WriteLine("SbImage.Load " + folder.files[i] + " failed");
137 err = img == null ? SbError.SB_ERR_INTERNAL : img.Error();
138 break;
139 }
140
142 roi = SbRoi.Create(img.Width(), img.Height());
143 if (roi == null || roi.Error() != SbError.SB_ERR_NONE)
144 {
145 Console.WriteLine("SbRoi.Create failed");
146 err = roi == null ? SbError.SB_ERR_INTERNAL : roi.Error();
147 goto FnExit;
148 }
149
151 err = roi.SetRect(255, new SbRect(0, 0, img.Width(), img.Height()), false);
152 if (err != SbError.SB_ERR_NONE)
153 {
154 Console.WriteLine("SbRoi.SetRect failed with error " + err);
155 goto FnExit;
156 }
157
159 err = prj.Detection(img, roi);
160 if (err != SbError.SB_ERR_NONE)
161 {
162 Console.WriteLine("SbProject.Detection failed with error " + err);
163 break;
164 }
165
167 res = prj.GetRes(false);
168 if (res == null || prj.Error() != SbError.SB_ERR_NONE)
169 {
170 err = res == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
171 Console.WriteLine("SbProject.GetRes failed with error " + err);
172 break;
173 }
174
176 Console.WriteLine("image: " + folder.files[i]);
177 Console.WriteLine("\t" + res.global.surface.blobs.Length + " defect blobs found:");
178 for(int j = 0; j < res.global.surface.blobs.Length; j++)
179 {
180 Console.WriteLine("\t\tul=(" + res.global.surface.blobs[j].rect.x + "," + res.global.surface.blobs[j].rect.y +
181 "), size=(" + res.global.surface.blobs[j].rect.width + "," + res.global.surface.blobs[j].rect.height +
182 "), area=" + res.global.surface.blobs[j].area);
183 }
184
185 if (img != null)
186 {
187 img.Dispose();
188 img = null;
189 }
190 if (roi != null)
191 {
192 roi.Dispose();
193 roi = null;
194 }
195 if (res != null)
196 {
197 res.Dispose();
198 res = null;
199 }
200 }
201 }
202
203 FnExit:
205 if (prj != null)
206 prj.Dispose();
207 if (img != null)
208 img.Dispose();
209 if (roi != null)
210 roi.Dispose();
211 if (par != null)
212 par.Dispose();
213 if (res != null)
214 res.Dispose();
215
216 Console.WriteLine("Release SqueezeBrains library");
217 Sb.Release();
218
219 Console.WriteLine("Press ENTER to terminate");
220 Console.ReadKey();
221
222 return err;
223 }
224
225 static void Main(string[] args)
226 {
227 execute();
228 }
229 }
230}
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:309
static const String SbSolutionExt
Solution file extension
Definition: cs_common.h:315
array< int > id
Identifier of the devices to be used
Definition: cs_par.h:266
SbDeviceType type
Device computational type
Definition: cs_par.h:254
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:401
static SbError Release()
Releases all the resources allocated in the library
static SbError InitDl(String^ search_path, SbDlLibraryType LibType)
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:751
SbBlobPar blob_par
Blob analysis parameters. Used only for Surface projects
Definition: cs_par.h:781
SbDevicesPar devices
Devices used for inference.
Definition: cs_par.h:787
int surface_blob_analysis
Enable the surface blob analysis
Definition: cs_par.h:811
Project Class You must call the Dispose() method to free all the resources of the returned instance.
Definition: cs_project.h:51
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:128
Rectangle class that wraps the sb_t_rect structure
Definition: cs_sb.h:214
Class of the results of the image elaboration with the SbProject::Detection method....
Definition: cs_res.h:229
SbResModel global
Global results
Definition: cs_res.h:235
SbSurfaceRes surface
: Surface specific results.
Definition: cs_res.h:136
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:73
SbDlLibraryType
Deep Learning library types
Definition: cs_sb.h:39
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:219
SB Namespace
Definition: cs_common.h:3