2using System.Collections.Generic;
5using System.Threading.Tasks;
14 static void _predict(
Model model, CFviImage targetImage)
24 for (var i_score = 0; i_score < scores.Length; i_score++)
26 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
37 var str_is_anomaly = tuple.Item1 ?
"anomaly" :
"normal";
38 Console.WriteLine($
"{str_is_anomaly}, max_anomaly={tuple.Item2}");
47 for (var i_score = 0; i_score < scores.Length; i_score++)
49 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
58 default:
throw new NotImplementedException($
"unknwon model-category={model.ModelCategory}");
61 static void UseWithWIL(String modelPath, String imagePath)
66 if (
false == has_license) {
throw new Exception(
"no license"); }
72 CFviImage image =
new CFviImage(imagePath);
76 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
79 Console.WriteLine(
"original image");
80 _predict(model, image);
83 var filtered_image =
new CFviImage(image,
false);
84 var parser =
new FVIL.Filter.CFviAverageFilter();
85 parser.SrcImages[0] = image;
86 parser.DstImages[0] = filtered_image;
87 if (!parser.IsValid())
94 Console.WriteLine(
"filtered image");
95 _predict(model, filtered_image);
97 catch (CFviException ex)
99 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
100 Console.WriteLine(ex.StackTrace);
104 Console.WriteLine($
"Message={ex.Message}");
105 Console.WriteLine(ex.StackTrace);
109 static void _predictMultiView(
Model model, IEnumerable<CFviImage> targetImages)
120 for (var i_score = 0; i_score < scores.Length; i_score++)
122 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
130 var threshold = 10.0;
134 var str_is_anomaly = tuple.Item1 ?
"anomaly" :
"normal";
135 Console.WriteLine($
"{str_is_anomaly}, max_anomaly={tuple.Item2}");
144 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
148 static void UseWithWILMultiImage(String modelPath, String imageFolder)
153 if (
false == has_license) {
throw new Exception(
"no license"); }
159 List<CFviImage> original_images =
new List<CFviImage>();
160 for (var i_img = 0; i_img < model.
NumViews; i_img++)
163 var img_path = System.IO.Path.Combine(imageFolder, String.Format(
"{0}.bmp", i_img));
164 var image =
new CFviImage(img_path);
168 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
171 original_images.Add(image);
175 Console.WriteLine(
"original image");
176 _predictMultiView(model, original_images);
179 List<CFviImage> filtered_images =
new List<CFviImage>();
180 using (var parser =
new FVIL.Filter.CFviAverageFilter())
182 foreach (var src_image
in original_images)
184 var dst_image =
new CFviImage(src_image.HorzSize, src_image.VertSize, src_image.ImageType, src_image.Channel);
186 parser.SrcImages[0] = src_image;
187 parser.DstImages[0] = dst_image;
188 if (!parser.IsValid())
194 filtered_images.Add(dst_image);
199 Console.WriteLine(
"filtered image");
200 _predictMultiView(model, filtered_images);
202 catch (CFviException ex)
204 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
205 Console.WriteLine(ex.StackTrace);
209 Console.WriteLine($
"Message={ex.Message}");
210 Console.WriteLine(ex.StackTrace);
214 static void UseWithWILSegmentation(String modelPath, String imagePath, Double blobAreaMinThreshold, Double blobAreaMaxThreshold)
219 if (
false == has_license) {
throw new Exception(
"no license"); }
225 CFviImage image =
new CFviImage(imagePath);
229 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
240 var max_posi = segm_result_image.FindMaxValue(0, 0);
241 var max_value = segm_result_image.GetPixelI32(0, (Int32)max_posi.X, (Int32)max_posi.Y);
244 Console.WriteLine(
"all pixels are background");
251 for (var category_id = 1; category_id <= n_category; category_id++)
255 var bin_mask =
new CFviImage(segm_result_image.HorzSize, segm_result_image.VertSize, ImageType.BIN, 1, 1);
256 bin_mask.Window = segm_result_image.Window;
257 var ret = fvalgcli.api.fnFIE_band_threshold(segm_result_image.GetFIEChild(), bin_mask.GetFIEChild(), category_id, category_id);
258 if ((Int32)fvalgcli.f_err.F_ERR_NONE != ret) {
throw new fvalgcli.FvException(ret); }
261 var blob_result =
new FVIL.Blob.CFviBlobResult();
262 var blob_param =
new FVIL.Blob.CFviBlobParam();
263 blob_param.ColorMode =
FVIL.Blob.ObjectColor.WhiteFG_BlackBG;
264 blob_param.Neighborhood =
FVIL.Blob.Neighborhood.Eight;
265 var blob_parser =
new FVIL.Blob.CFviBlob(bin_mask, blob_result, blob_param);
266 if (!blob_parser.IsValid()) { blob_parser.Validate(); }
267 blob_parser.Execute();
270 var filters =
new List<
FVIL.Blob.CFviBlobFilterRange>();
271 filters.Add(
new FVIL.Blob.CFviBlobFilterRange(
FVIL.Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold));
272 var blob_list = blob_result.GetBlobList(filters);
273 Console.WriteLine($
"category_id={category_id}: blob count={blob_list.Count}");
274 foreach(var kv
in blob_list)
276 var blob_data =
new FVIL.Blob.CFviBlobData(kv);
281 var region_mask =
new CFviRegion(bin_mask);
293 var category_map =
new CFviImage(fvalgcli.api.fnFIE_img_child_alloc_single_ch(segm_result_image.GetFIERoot(), 0, 0, 0, segm_result_image.HorzSize, segm_result_image.VertSize),
true);
294 var instance_map =
new CFviImage(fvalgcli.api.fnFIE_img_child_alloc_single_ch(segm_result_image.GetFIERoot(), 1, 0, 0, segm_result_image.HorzSize, segm_result_image.VertSize),
true);
295 category_map.Window = segm_result_image.Window;
296 instance_map.Window = segm_result_image.Window;
299 var max_posi = instance_map.FindMaxValue(0, 0);
300 var max_value = instance_map.GetPixelI32(0, (Int32)max_posi.X, (Int32)max_posi.Y);
303 Console.WriteLine(
"all pixels are background");
307 for (var instance_id = 1; instance_id <= max_value; instance_id++)
310 var bin_mask =
new CFviImage(instance_map.HorzSize, instance_map.VertSize, ImageType.BIN, 1, 1);
311 bin_mask.Window = instance_map.Window;
312 var ret = fvalgcli.api.fnFIE_band_threshold(instance_map.GetFIEChild(), bin_mask.GetFIEChild(), instance_id, instance_id);
313 if ((Int32)fvalgcli.f_err.F_ERR_NONE != ret) {
throw new fvalgcli.FvException(ret); }
316 var blob_result =
new FVIL.Blob.CFviBlobResult();
317 var blob_param =
new FVIL.Blob.CFviBlobParam();
318 blob_param.ColorMode =
FVIL.Blob.ObjectColor.WhiteFG_BlackBG;
319 blob_param.Neighborhood =
FVIL.Blob.Neighborhood.Eight;
320 var blob_parser =
new FVIL.Blob.CFviBlob(bin_mask, blob_result, blob_param);
321 if (!blob_parser.IsValid()) { blob_parser.Validate(); }
322 blob_parser.Execute();
325 var filters =
new List<
FVIL.Blob.CFviBlobFilterRange>();
326 filters.Add(
new FVIL.Blob.CFviBlobFilterRange(
FVIL.Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold));
327 var blob_list = blob_result.GetBlobList(filters);
328 Console.WriteLine($
"instance_id={instance_id}: blob count={blob_list.Count}");
329 foreach (var kv
in blob_list)
331 var blob_data =
new FVIL.Blob.CFviBlobData(kv);
333 var region = blob_data.GetRegion();
334 var category_id = (UInt32)
FVIL.Region.Measure.MaxPixel(category_map, region, OriginMode.Window);
336 Console.WriteLine($
"instance_id={instance_id}: category_id={category_id}, blob no={blob_data.BlobNo}");
342 var region_mask =
new CFviRegion(bin_mask);
353 default:
throw new NotImplementedException($
"unknwon model-category={model.ModelCategory}");
356 catch (CFviException ex)
358 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
359 Console.WriteLine(ex.StackTrace);
363 Console.WriteLine($
"Message={ex.Message}");
364 Console.WriteLine(ex.StackTrace);
368 static void _predictObjectDetection(
Model model, CFviImage targetImage)
378 Console.WriteLine($
"number of detections: {detections.Count()}");
381 default:
throw new NotImplementedException($
"unknwon model-category={model.ModelCategory}");
384 static void UseWithWILObjectDetection(String modelPath, String imagePath)
389 if (
false == has_license) {
throw new Exception(
"no license"); }
395 CFviImage image =
new CFviImage(imagePath);
399 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
402 Console.WriteLine(
"original image");
403 _predictObjectDetection(model, image);
406 var filtered_image =
new CFviImage(image,
false);
407 var parser =
new FVIL.Filter.CFviAverageFilter();
408 parser.SrcImages[0] = image;
409 parser.DstImages[0] = filtered_image;
410 if (!parser.IsValid())
417 Console.WriteLine(
"filtered image");
418 _predictObjectDetection(model, filtered_image);
420 catch (CFviException ex)
422 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
423 Console.WriteLine(ex.StackTrace);
427 Console.WriteLine($
"Message={ex.Message}");
428 Console.WriteLine(ex.StackTrace);
推論するモデルを扱うクラス
Definition: PredictionCS.cs:250
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:271
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:992
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:675
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:425
IEnumerable< ObjectDetectionData > PredictObjectDetection(CFviImage targetImage)
推論の実行(物体検出)
Definition: PredictionCS.cs:1729
IEnumerable< String > GetLabelList()
ラべル一覧の取得
Definition: PredictionCS.cs:611
CFviImage PredictSemanticSegmentation(CFviImage targetImage)
推論の実行(セマンティックセグメンテーション)
Definition: PredictionCS.cs:1440
Tuple< Boolean, float > PredictMultiViewAD(IEnumerable< CFviImage > targetImages, float threshold)
推論の実行(多視点アノマリー検出)
Definition: PredictionCS.cs:1305
float[] PredictMultiLabelClassification(CFviImage targetImage)
推論の実行(マルチラベル画像分類)
Definition: PredictionCS.cs:1760
Int32 NumViews
読み込んだモデルが期待する視点数
Definition: PredictionCS.cs:314
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:1083
CFviImage PredictPanopticSegmentation(CFviImage targetImage)
推論の実行(パノプティックセグメンテーション)
Definition: PredictionCS.cs:1540
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:1213
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:21
ModelCategory
モデルの種別
Definition: PredictionCS.cs:32
FVILの最上位ネームスペース
Definition: PredictionCS.cs:16
2Imports System.Collections.Generic
11 Private Shared Sub _predict(model As Model, targetImage As CFviImage)
12 Select Case model.ModelCategory
13 Case ModelCategory.Classification ' 画像分類
15 Dim scores = model.PredictClassification(targetImage)
18 For i_score = 0 To scores.Length - 1
19 Console.WriteLine($"label{i_score}: {scores(i_score)}")
21 Case ModelCategory.AnomalyDetection ' アノマリー検出
22 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
24 Dim tuple = model.PredictAnomaly(targetImage, threshold)
27 Dim str_is_anomaly = If(tuple.Item1, "anomaly", "normal")
28 Console.WriteLine($"{str_is_anomaly}, max_anomaly={tuple.Item2}")
29 Case ModelCategory.MultiLabelClassification ' マルチラベル画像分類
31 Dim scores = model.PredictMultiLabelClassification(targetImage)
34 For i_score = 0 To scores.Length - 1
35 Console.WriteLine($"label{i_score}: {scores(i_score)}")
39 Throw New NotImplementedException($"unknwon model-category={model.ModelCategory}")
42 Private Shared Sub UseWithWIL(modelPath As String, imagePath As String)
44 Dim has_license = PDL.Model.CheckLicense()
45 If False = has_license Then
46 Throw New Exception("no license")
49 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
50 Dim model As Model = New Model(modelPath)
53 Dim image As CFviImage = New CFviImage(imagePath)
56 Dim is_valid_img = model.IsValidImage(image)
57 If False = is_valid_img Then
58 Throw New Exception("invalid image")
62 Console.WriteLine("original image")
63 _predict(model, image)
65 ' 任意のフィルタ処理 ( ここでは例として単純な average フィルタ )
66 Dim filtered_image = New CFviImage(image, False)
67 Dim parser = New Filter.CFviAverageFilter()
68 parser.SrcImages(0) = image
69 parser.DstImages(0) = filtered_image
70 If Not parser.IsValid() Then
76 Console.WriteLine("filtered image")
77 _predict(model, filtered_image)
78 Catch ex As CFviException
79 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
80 Console.WriteLine(ex.StackTrace)
82 Console.WriteLine($"Message={ex.Message}")
83 Console.WriteLine(ex.StackTrace)
87 Private Shared Sub _predictMultiView(model As Model, targetImages As IEnumerable(Of CFviImage))
89 Select Case model.ModelCategory
90 Case ModelCategory.MultiViewCNN
92 Dim scores = model.PredictMultiViewCNN(targetImages)
95 For i_score = 0 To scores.Length - 1
96 Console.WriteLine($"label{i_score}: {scores(i_score)}")
98 Case ModelCategory.MultiViewAD
100 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
102 Dim tuple = model.PredictMultiViewAD(targetImages, threshold)
105 Dim str_is_anomaly = If(tuple.Item1, "anomaly", "normal")
106 Console.WriteLine($"{str_is_anomaly}, max_anomaly={tuple.Item2}")
110 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
114 Private Shared Sub UseWithWILMultiImage(modelPath As String, imageFolder As String)
116 Dim has_license = PDL.Model.CheckLicense()
117 If False = has_license Then
118 Throw New Exception("no license")
121 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
122 Dim model As Model = New Model(modelPath)
125 Dim original_images As List(Of CFviImage) = New List(Of CFviImage)()
126 For i_img = 0 To model.NumViews - 1
127 ' imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
128 Dim img_path = Path.Combine(imageFolder, String.Format("{0}.bmp", i_img))
129 Dim image = New CFviImage(img_path)
132 Dim is_valid_img = model.IsValidImage(image)
133 If False = is_valid_img Then
134 Throw New Exception("invalid image")
138 original_images.Add(image)
142 Console.WriteLine("original image")
143 _predictMultiView(model, original_images)
145 ' 任意のフィルタ処理 ( ここでは例として単純な average フィルタ )
146 Dim filtered_images As List(Of CFviImage) = New List(Of CFviImage)()
147 Using parser = New Filter.CFviAverageFilter()
148 For Each src_image In original_images
149 Dim dst_image = New CFviImage(src_image.HorzSize, src_image.VertSize, src_image.ImageType, src_image.Channel)
151 parser.SrcImages(0) = src_image
152 parser.DstImages(0) = dst_image
153 If Not parser.IsValid() Then
158 filtered_images.Add(dst_image)
163 Console.WriteLine("filtered image")
164 _predictMultiView(model, filtered_images)
165 Catch ex As CFviException
166 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
167 Console.WriteLine(ex.StackTrace)
168 Catch ex As Exception
169 Console.WriteLine($"Message={ex.Message}")
170 Console.WriteLine(ex.StackTrace)
174 Private Shared Sub UseWithWILSegmentation(modelPath As String, imagePath As String, blobAreaMinThreshold As Double, blobAreaMaxThreshold As Double)
176 Dim has_license = PDL.Model.CheckLicense()
177 If False = has_license Then
178 Throw New Exception("no license")
181 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
182 Dim model As Model = New Model(modelPath)
185 Dim image As CFviImage = New CFviImage(imagePath)
188 Dim is_valid_img = model.IsValidImage(image)
189 If False = is_valid_img Then
190 Throw New Exception("invalid image")
194 Select Case model.ModelCategory
195 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
197 Dim segm_result_image = model.PredictSemanticSegmentation(image)
199 ' 濃度値の最大が領域を検出できたカテゴリたちの中での最大の番号に相当
200 Dim max_posi = segm_result_image.FindMaxValue(0, 0)
201 Dim max_value = segm_result_image.GetPixelI32(0, max_posi.X, max_posi.Y)
202 If 0 = max_value Then ' 0 が最大値ということは 1 以上のピクセルが無い。つまり画像の全てが背景と判定されたとき
203 Console.WriteLine("all pixels are background")
206 ' カテゴリの個数: 既知であればここで取得する必要は無い
207 Dim n_category = model.GetLabelList().Count()
209 ' 各カテゴリ ( ラベル ) の情報を取得: 0 番は背景に対応し、カテゴリ番号は 1-index であることに注意
210 For category_id = 1 To n_category
211 ' カテゴリ番号に一致するピクセルのみを 1 に、その他を 0 にすることで、そのカテゴリとして検出された領域の二値画像を取得
212 ' ※ PredictSemanticSegmentation(CFviImage targetImage, Dictionary<UInt32, CFviImage> categoryMaskImages) にて categoryMaskImages で取得できる二値画像に相当
213 Dim bin_mask = New CFviImage(segm_result_image.HorzSize, segm_result_image.VertSize, ImageType.BIN, 1, 1)
214 bin_mask.Window = segm_result_image.Window
215 Dim ret = fvalgcli.api.fnFIE_band_threshold(segm_result_image.GetFIEChild(), bin_mask.GetFIEChild(), category_id, category_id)
216 If fvalgcli.f_err.F_ERR_NONE <> ret Then
217 Throw New fvalgcli.FvException(ret)
220 ' 検出した領域の二値画像に対してブローブ解析を実行
221 Dim blob_result = New Blob.CFviBlobResult()
222 Dim blob_param = New Blob.CFviBlobParam()
223 blob_param.ColorMode = Blob.ObjectColor.WhiteFG_BlackBG
224 blob_param.Neighborhood = Blob.Neighborhood.Eight
225 Dim blob_parser = New Blob.CFviBlob(bin_mask, blob_result, blob_param)
226 If Not blob_parser.IsValid() Then
227 blob_parser.Validate()
229 blob_parser.Execute()
231 ' ブローブ解析の結果に対して、さらにフィルタリング ( ここでは面積 )
232 Dim filters = New List(Of Blob.CFviBlobFilterRange)()
233 filters.Add(New Blob.CFviBlobFilterRange(Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold))
234 Dim blob_list = blob_result.GetBlobList(filters)
235 Console.WriteLine($"category_id={category_id}: blob count={blob_list.Count}")
236 For Each kv In blob_list
237 Dim blob_data = New Blob.CFviBlobData(kv)
238 ' 以下、ブローブ特徴量に基づく解析も可能
241 ' 検出した領域の二値画像をリージョンに変換
242 Dim region_mask = New CFviRegion(bin_mask)
243 ' リージョン特徴量に基づく解析や、画像に対するリージョン内の特徴量の取得が可能
244 'var mean = FVIL.Region.Measure.MeanPixel(image, region_mask);// 例: 元画像のリージョン内の平均濃度値の取得
246 Case ModelCategory.PanopticSegmentation
248 Dim segm_result_image = model.PredictPanopticSegmentation(image)
250 ' カテゴリ番号の格納された画像とインスタンス番号の格納された画像に分離
251 Dim category_map = New CFviImage(fvalgcli.api.fnFIE_img_child_alloc_single_ch(segm_result_image.GetFIERoot(), 0, 0, 0, segm_result_image.HorzSize, segm_result_image.VertSize), True)
252 Dim instance_map = New CFviImage(fvalgcli.api.fnFIE_img_child_alloc_single_ch(segm_result_image.GetFIERoot(), 1, 0, 0, segm_result_image.HorzSize, segm_result_image.VertSize), True)
253 category_map.Window = segm_result_image.Window
254 instance_map.Window = segm_result_image.Window
256 ' 濃度値の最大が検出した領域の個数に相当
257 Dim max_posi = instance_map.FindMaxValue(0, 0)
258 Dim max_value = instance_map.GetPixelI32(0, max_posi.X, max_posi.Y)
259 If 0 = max_value Then ' 0 が最大値ということは 1 以上のピクセルが無い。つまり画像の全てが背景と判定されたとき
260 Console.WriteLine("all pixels are background")
263 ' 各検出領域の取得と、その領域に対する画像処理
264 For instance_id = 1 To max_value
265 ' 検出した領域の通し番号に一致するピクセルのみを 1 に、その他を 0 にすることで、その領域の二値画像を取得
266 Dim bin_mask = New CFviImage(instance_map.HorzSize, instance_map.VertSize, ImageType.BIN, 1, 1)
267 bin_mask.Window = instance_map.Window
268 Dim ret = fvalgcli.api.fnFIE_band_threshold(instance_map.GetFIEChild(), bin_mask.GetFIEChild(), instance_id, instance_id)
269 If fvalgcli.f_err.F_ERR_NONE <> ret Then
270 Throw New fvalgcli.FvException(ret)
273 ' 検出した領域の二値画像に対してブローブ解析を実行 ( 物体毎に分離できているはずなので、各インスタンスの領域がそれ以上に分割されることはほとんど無いはず )
274 Dim blob_result = New Blob.CFviBlobResult()
275 Dim blob_param = New Blob.CFviBlobParam()
276 blob_param.ColorMode = Blob.ObjectColor.WhiteFG_BlackBG
277 blob_param.Neighborhood = Blob.Neighborhood.Eight
278 Dim blob_parser = New Blob.CFviBlob(bin_mask, blob_result, blob_param)
279 If Not blob_parser.IsValid() Then
280 blob_parser.Validate()
282 blob_parser.Execute()
284 ' ブローブ解析の結果に対して、さらにフィルタリング ( ここでは面積 )
285 Dim filters = New List(Of Blob.CFviBlobFilterRange)()
286 filters.Add(New Blob.CFviBlobFilterRange(Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold))
287 Dim blob_list = blob_result.GetBlobList(filters)
288 Console.WriteLine($"instance_id={instance_id}: blob count={blob_list.Count}")
289 For Each kv In blob_list
290 Dim blob_data = New Blob.CFviBlobData(kv)
292 Dim region = blob_data.GetRegion()
293 Dim category_id = CUInt(FVIL.Region.Measure.MaxPixel(category_map, region, OriginMode.Window)) ' max=min のはずで、領域をすべて見なくても 1 ピクセルを確認するのみでも良い
295 Console.WriteLine($"instance_id={instance_id}: category_id={category_id}, blob no={blob_data.BlobNo}")
297 ' 以下、ブローブ特徴量に基づく解析も可能
300 ' 検出した領域の二値画像を直接リージョンに変換
301 Dim region_mask = New CFviRegion(bin_mask)
302 ' リージョン特徴量に基づく解析や、画像に対するリージョン内の特徴量の取得が可能
303 'var mean = FVIL.Region.Measure.MeanPixel(image, region_mask);// 例: 元画像のリージョン内の平均濃度値の取得
307 Throw New NotImplementedException($"unknwon model-category={model.ModelCategory}")
309 Catch ex As CFviException
310 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
311 Console.WriteLine(ex.StackTrace)
312 Catch ex As Exception
313 Console.WriteLine($"Message={ex.Message}")
314 Console.WriteLine(ex.StackTrace)
318 Private Shared Sub _predictObjectDetection(model As Model, targetImage As CFviImage)
319 Select Case model.ModelCategory
320 Case ModelCategory.ObjectDetection ' 物体検出
322 Dim detections = model.PredictObjectDetection(targetImage)
325 Console.WriteLine($"number of detections: {detections.Count()}")
327 Throw New NotImplementedException($"unknwon model-category={model.ModelCategory}")
330 Private Shared Sub UseWithWILObjectDetection(modelPath As String, imagePath As String)
332 Dim has_license = PDL.Model.CheckLicense()
333 If False = has_license Then
334 Throw New Exception("no license")
337 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
338 Dim model As Model = New Model(modelPath)
341 Dim image As CFviImage = New CFviImage(imagePath)
344 Dim is_valid_img = model.IsValidImage(image)
345 If False = is_valid_img Then
346 Throw New Exception("invalid image")
350 Console.WriteLine("original image")
351 _predictObjectDetection(model, image)
353 ' 任意のフィルタ処理 ( ここでは例として単純な average フィルタ )
354 Dim filtered_image = New CFviImage(image, False)
355 Dim parser = New Filter.CFviAverageFilter()
356 parser.SrcImages(0) = image
357 parser.DstImages(0) = filtered_image
358 If Not parser.IsValid() Then
364 Console.WriteLine("filtered image")
365 _predictObjectDetection(model, filtered_image)
366 Catch ex As CFviException
367 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
368 Console.WriteLine(ex.StackTrace)
369 Catch ex As Exception
370 Console.WriteLine($"Message={ex.Message}")
371 Console.WriteLine(ex.StackTrace)