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}");
46 default:
throw new NotImplementedException($
"unknwon model-category={model.ModelCategory}");
49 static void UseWithWIL(String modelPath, String imagePath)
54 if (
false == has_license) {
throw new Exception(
"no license"); }
60 CFviImage image =
new CFviImage(imagePath);
64 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
67 Console.WriteLine(
"original image");
68 _predict(model, image);
71 var filtered_image =
new CFviImage(image,
false);
72 var parser =
new FVIL.Filter.CFviAverageFilter();
73 parser.SrcImages[0] = image;
74 parser.DstImages[0] = filtered_image;
75 if (!parser.IsValid())
82 Console.WriteLine(
"filtered image");
83 _predict(model, filtered_image);
85 catch (CFviException ex)
87 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
88 Console.WriteLine(ex.StackTrace);
92 Console.WriteLine($
"Message={ex.Message}");
93 Console.WriteLine(ex.StackTrace);
97 static void _predictMultiView(
Model model, IEnumerable<CFviImage> targetImages)
108 for (var i_score = 0; i_score < scores.Length; i_score++)
110 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
118 var threshold = 10.0;
122 var str_is_anomaly = tuple.Item1 ?
"anomaly" :
"normal";
123 Console.WriteLine($
"{str_is_anomaly}, max_anomaly={tuple.Item2}");
132 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
136 static void UseWithWILMultiImage(String modelPath, String imageFolder)
141 if (
false == has_license) {
throw new Exception(
"no license"); }
147 List<CFviImage> original_images =
new List<CFviImage>();
148 for (var i_img = 0; i_img < model.
NumViews; i_img++)
151 var img_path = System.IO.Path.Combine(imageFolder, String.Format(
"{0}.bmp", i_img));
152 var image =
new CFviImage(img_path);
156 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
159 original_images.Add(image);
163 Console.WriteLine(
"original image");
164 _predictMultiView(model, original_images);
167 List<CFviImage> filtered_images =
new List<CFviImage>();
168 using (var parser =
new FVIL.Filter.CFviAverageFilter())
170 foreach (var src_image
in original_images)
172 var dst_image =
new CFviImage(src_image.HorzSize, src_image.VertSize, src_image.ImageType, src_image.Channel);
174 parser.SrcImages[0] = src_image;
175 parser.DstImages[0] = dst_image;
176 if (!parser.IsValid())
182 filtered_images.Add(dst_image);
187 Console.WriteLine(
"filtered image");
188 _predictMultiView(model, filtered_images);
190 catch (CFviException ex)
192 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
193 Console.WriteLine(ex.StackTrace);
197 Console.WriteLine($
"Message={ex.Message}");
198 Console.WriteLine(ex.StackTrace);
202 static void UseWithWILSegmentation(String modelPath, String imagePath, Double blobAreaMinThreshold, Double blobAreaMaxThreshold)
207 if (
false == has_license) {
throw new Exception(
"no license"); }
213 CFviImage image =
new CFviImage(imagePath);
217 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
228 var max_posi = segm_result_image.FindMaxValue(0, 0);
229 var max_value = segm_result_image.GetPixelI32(0, (Int32)max_posi.X, (Int32)max_posi.Y);
232 Console.WriteLine(
"all pixels are background");
239 for (var category_id = 1; category_id <= n_category; category_id++)
243 var bin_mask =
new CFviImage(segm_result_image.HorzSize, segm_result_image.VertSize, ImageType.BIN, 1, 1);
244 bin_mask.Window = segm_result_image.Window;
245 var ret = fvalgcli.api.fnFIE_band_threshold(segm_result_image.GetFIEChild(), bin_mask.GetFIEChild(), category_id, category_id);
246 if ((Int32)fvalgcli.f_err.F_ERR_NONE != ret) {
throw new fvalgcli.FvException(ret); }
249 var blob_result =
new FVIL.Blob.CFviBlobResult();
250 var blob_param =
new FVIL.Blob.CFviBlobParam();
251 blob_param.ColorMode =
FVIL.Blob.ObjectColor.WhiteFG_BlackBG;
252 blob_param.Neighborhood =
FVIL.Blob.Neighborhood.Eight;
253 var blob_parser =
new FVIL.Blob.CFviBlob(bin_mask, blob_result, blob_param);
254 if (!blob_parser.IsValid()) { blob_parser.Validate(); }
255 blob_parser.Execute();
258 var filters =
new List<
FVIL.Blob.CFviBlobFilterRange>();
259 filters.Add(
new FVIL.Blob.CFviBlobFilterRange(
FVIL.Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold));
260 var blob_list = blob_result.GetBlobList(filters);
261 Console.WriteLine($
"category_id={category_id}: blob count={blob_list.Count}");
262 foreach(var kv
in blob_list)
264 var blob_data =
new FVIL.Blob.CFviBlobData(kv);
269 var region_mask =
new CFviRegion(bin_mask);
281 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);
282 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);
283 category_map.Window = segm_result_image.Window;
284 instance_map.Window = segm_result_image.Window;
287 var max_posi = instance_map.FindMaxValue(0, 0);
288 var max_value = instance_map.GetPixelI32(0, (Int32)max_posi.X, (Int32)max_posi.Y);
291 Console.WriteLine(
"all pixels are background");
295 for (var instance_id = 1; instance_id <= max_value; instance_id++)
298 var bin_mask =
new CFviImage(instance_map.HorzSize, instance_map.VertSize, ImageType.BIN, 1, 1);
299 bin_mask.Window = instance_map.Window;
300 var ret = fvalgcli.api.fnFIE_band_threshold(instance_map.GetFIEChild(), bin_mask.GetFIEChild(), instance_id, instance_id);
301 if ((Int32)fvalgcli.f_err.F_ERR_NONE != ret) {
throw new fvalgcli.FvException(ret); }
304 var blob_result =
new FVIL.Blob.CFviBlobResult();
305 var blob_param =
new FVIL.Blob.CFviBlobParam();
306 blob_param.ColorMode =
FVIL.Blob.ObjectColor.WhiteFG_BlackBG;
307 blob_param.Neighborhood =
FVIL.Blob.Neighborhood.Eight;
308 var blob_parser =
new FVIL.Blob.CFviBlob(bin_mask, blob_result, blob_param);
309 if (!blob_parser.IsValid()) { blob_parser.Validate(); }
310 blob_parser.Execute();
313 var filters =
new List<
FVIL.Blob.CFviBlobFilterRange>();
314 filters.Add(
new FVIL.Blob.CFviBlobFilterRange(
FVIL.Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold));
315 var blob_list = blob_result.GetBlobList(filters);
316 Console.WriteLine($
"instance_id={instance_id}: blob count={blob_list.Count}");
317 foreach (var kv
in blob_list)
319 var blob_data =
new FVIL.Blob.CFviBlobData(kv);
321 var region = blob_data.GetRegion();
322 var category_id = (UInt32)
FVIL.Region.Measure.MaxPixel(category_map, region, OriginMode.Window);
324 Console.WriteLine($
"instance_id={instance_id}: category_id={category_id}, blob no={blob_data.BlobNo}");
330 var region_mask =
new CFviRegion(bin_mask);
341 default:
throw new NotImplementedException($
"unknwon model-category={model.ModelCategory}");
344 catch (CFviException ex)
346 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
347 Console.WriteLine(ex.StackTrace);
351 Console.WriteLine($
"Message={ex.Message}");
352 Console.WriteLine(ex.StackTrace);
推論するモデルを扱うクラス
Definition: PredictionCS.cs:146
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:167
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:888
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:571
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:321
IEnumerable< String > GetLabelList()
ラべル一覧の取得
Definition: PredictionCS.cs:507
CFviImage PredictSemanticSegmentation(CFviImage targetImage)
推論の実行(セマンティックセグメンテーション)
Definition: PredictionCS.cs:1336
Tuple< Boolean, float > PredictMultiViewAD(IEnumerable< CFviImage > targetImages, float threshold)
推論の実行(多視点アノマリー検出)
Definition: PredictionCS.cs:1201
Int32 NumViews
読み込んだモデルが期待する視点数
Definition: PredictionCS.cs:210
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:979
CFviImage PredictPanopticSegmentation(CFviImage targetImage)
推論の実行(パノプティックセグメンテーション)
Definition: PredictionCS.cs:1436
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:1109
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:20
ModelCategory
モデルの種別
Definition: PredictionCS.cs:31
FVILの最上位ネームスペース
Definition: PredictionCS.cs:15
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}")
30 Throw New NotImplementedException($"unknwon model-category={model.ModelCategory}")
33 Private Shared Sub UseWithWIL(modelPath As String, imagePath As String)
35 Dim has_license = PDL.Model.CheckLicense()
36 If False = has_license Then
37 Throw New Exception("no license")
40 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
41 Dim model As Model = New Model(modelPath)
44 Dim image As CFviImage = New CFviImage(imagePath)
47 Dim is_valid_img = model.IsValidImage(image)
48 If False = is_valid_img Then
49 Throw New Exception("invalid image")
53 Console.WriteLine("original image")
54 _predict(model, image)
56 ' 任意のフィルタ処理 ( ここでは例として単純な average フィルタ )
57 Dim filtered_image = New CFviImage(image, False)
58 Dim parser = New Filter.CFviAverageFilter()
59 parser.SrcImages(0) = image
60 parser.DstImages(0) = filtered_image
61 If Not parser.IsValid() Then
67 Console.WriteLine("filtered image")
68 _predict(model, filtered_image)
69 Catch ex As CFviException
70 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
71 Console.WriteLine(ex.StackTrace)
73 Console.WriteLine($"Message={ex.Message}")
74 Console.WriteLine(ex.StackTrace)
78 Private Shared Sub _predictMultiView(model As Model, targetImages As IEnumerable(Of CFviImage))
80 Select Case model.ModelCategory
81 Case ModelCategory.MultiViewCNN
83 Dim scores = model.PredictMultiViewCNN(targetImages)
86 For i_score = 0 To scores.Length - 1
87 Console.WriteLine($"label{i_score}: {scores(i_score)}")
89 Case ModelCategory.MultiViewAD
91 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
93 Dim tuple = model.PredictMultiViewAD(targetImages, threshold)
96 Dim str_is_anomaly = If(tuple.Item1, "anomaly", "normal")
97 Console.WriteLine($"{str_is_anomaly}, max_anomaly={tuple.Item2}")
101 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
105 Private Shared Sub UseWithWILMultiImage(modelPath As String, imageFolder As String)
107 Dim has_license = PDL.Model.CheckLicense()
108 If False = has_license Then
109 Throw New Exception("no license")
112 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
113 Dim model As Model = New Model(modelPath)
116 Dim original_images As List(Of CFviImage) = New List(Of CFviImage)()
117 For i_img = 0 To model.NumViews - 1
118 ' imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
119 Dim img_path = Path.Combine(imageFolder, String.Format("{0}.bmp", i_img))
120 Dim image = New CFviImage(img_path)
123 Dim is_valid_img = model.IsValidImage(image)
124 If False = is_valid_img Then
125 Throw New Exception("invalid image")
129 original_images.Add(image)
133 Console.WriteLine("original image")
134 _predictMultiView(model, original_images)
136 ' 任意のフィルタ処理 ( ここでは例として単純な average フィルタ )
137 Dim filtered_images As List(Of CFviImage) = New List(Of CFviImage)()
138 Using parser = New Filter.CFviAverageFilter()
139 For Each src_image In original_images
140 Dim dst_image = New CFviImage(src_image.HorzSize, src_image.VertSize, src_image.ImageType, src_image.Channel)
142 parser.SrcImages(0) = src_image
143 parser.DstImages(0) = dst_image
144 If Not parser.IsValid() Then
149 filtered_images.Add(dst_image)
154 Console.WriteLine("filtered image")
155 _predictMultiView(model, filtered_images)
156 Catch ex As CFviException
157 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
158 Console.WriteLine(ex.StackTrace)
159 Catch ex As Exception
160 Console.WriteLine($"Message={ex.Message}")
161 Console.WriteLine(ex.StackTrace)
165 Private Shared Sub UseWithWILSegmentation(modelPath As String, imagePath As String, blobAreaMinThreshold As Double, blobAreaMaxThreshold As Double)
167 Dim has_license = PDL.Model.CheckLicense()
168 If False = has_license Then
169 Throw New Exception("no license")
172 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
173 Dim model As Model = New Model(modelPath)
176 Dim image As CFviImage = New CFviImage(imagePath)
179 Dim is_valid_img = model.IsValidImage(image)
180 If False = is_valid_img Then
181 Throw New Exception("invalid image")
185 Select Case model.ModelCategory
186 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
188 Dim segm_result_image = model.PredictSemanticSegmentation(image)
190 ' 濃度値の最大が領域を検出できたカテゴリたちの中での最大の番号に相当
191 Dim max_posi = segm_result_image.FindMaxValue(0, 0)
192 Dim max_value = segm_result_image.GetPixelI32(0, max_posi.X, max_posi.Y)
193 If 0 = max_value Then ' 0 が最大値ということは 1 以上のピクセルが無い。つまり画像の全てが背景と判定されたとき
194 Console.WriteLine("all pixels are background")
197 ' カテゴリの個数: 既知であればここで取得する必要は無い
198 Dim n_category = model.GetLabelList().Count()
200 ' 各カテゴリ ( ラベル ) の情報を取得: 0 番は背景に対応し、カテゴリ番号は 1-index であることに注意
201 For category_id = 1 To n_category
202 ' カテゴリ番号に一致するピクセルのみを 1 に、その他を 0 にすることで、そのカテゴリとして検出された領域の二値画像を取得
203 ' ※ PredictSemanticSegmentation(CFviImage targetImage, Dictionary<UInt32, CFviImage> categoryMaskImages) にて categoryMaskImages で取得できる二値画像に相当
204 Dim bin_mask = New CFviImage(segm_result_image.HorzSize, segm_result_image.VertSize, ImageType.BIN, 1, 1)
205 bin_mask.Window = segm_result_image.Window
206 Dim ret = fvalgcli.api.fnFIE_band_threshold(segm_result_image.GetFIEChild(), bin_mask.GetFIEChild(), category_id, category_id)
207 If fvalgcli.f_err.F_ERR_NONE <> ret Then
208 Throw New fvalgcli.FvException(ret)
211 ' 検出した領域の二値画像に対してブローブ解析を実行
212 Dim blob_result = New Blob.CFviBlobResult()
213 Dim blob_param = New Blob.CFviBlobParam()
214 blob_param.ColorMode = Blob.ObjectColor.WhiteFG_BlackBG
215 blob_param.Neighborhood = Blob.Neighborhood.Eight
216 Dim blob_parser = New Blob.CFviBlob(bin_mask, blob_result, blob_param)
217 If Not blob_parser.IsValid() Then
218 blob_parser.Validate()
220 blob_parser.Execute()
222 ' ブローブ解析の結果に対して、さらにフィルタリング ( ここでは面積 )
223 Dim filters = New List(Of Blob.CFviBlobFilterRange)()
224 filters.Add(New Blob.CFviBlobFilterRange(Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold))
225 Dim blob_list = blob_result.GetBlobList(filters)
226 Console.WriteLine($"category_id={category_id}: blob count={blob_list.Count}")
227 For Each kv In blob_list
228 Dim blob_data = New Blob.CFviBlobData(kv)
229 ' 以下、ブローブ特徴量に基づく解析も可能
232 ' 検出した領域の二値画像をリージョンに変換
233 Dim region_mask = New CFviRegion(bin_mask)
234 ' リージョン特徴量に基づく解析や、画像に対するリージョン内の特徴量の取得が可能
235 'var mean = FVIL.Region.Measure.MeanPixel(image, region_mask);// 例: 元画像のリージョン内の平均濃度値の取得
237 Case ModelCategory.PanopticSegmentation
239 Dim segm_result_image = model.PredictPanopticSegmentation(image)
241 ' カテゴリ番号の格納された画像とインスタンス番号の格納された画像に分離
242 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)
243 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)
244 category_map.Window = segm_result_image.Window
245 instance_map.Window = segm_result_image.Window
247 ' 濃度値の最大が検出した領域の個数に相当
248 Dim max_posi = instance_map.FindMaxValue(0, 0)
249 Dim max_value = instance_map.GetPixelI32(0, max_posi.X, max_posi.Y)
250 If 0 = max_value Then ' 0 が最大値ということは 1 以上のピクセルが無い。つまり画像の全てが背景と判定されたとき
251 Console.WriteLine("all pixels are background")
254 ' 各検出領域の取得と、その領域に対する画像処理
255 For instance_id = 1 To max_value
256 ' 検出した領域の通し番号に一致するピクセルのみを 1 に、その他を 0 にすることで、その領域の二値画像を取得
257 Dim bin_mask = New CFviImage(instance_map.HorzSize, instance_map.VertSize, ImageType.BIN, 1, 1)
258 bin_mask.Window = instance_map.Window
259 Dim ret = fvalgcli.api.fnFIE_band_threshold(instance_map.GetFIEChild(), bin_mask.GetFIEChild(), instance_id, instance_id)
260 If fvalgcli.f_err.F_ERR_NONE <> ret Then
261 Throw New fvalgcli.FvException(ret)
264 ' 検出した領域の二値画像に対してブローブ解析を実行 ( 物体毎に分離できているはずなので、各インスタンスの領域がそれ以上に分割されることはほとんど無いはず )
265 Dim blob_result = New Blob.CFviBlobResult()
266 Dim blob_param = New Blob.CFviBlobParam()
267 blob_param.ColorMode = Blob.ObjectColor.WhiteFG_BlackBG
268 blob_param.Neighborhood = Blob.Neighborhood.Eight
269 Dim blob_parser = New Blob.CFviBlob(bin_mask, blob_result, blob_param)
270 If Not blob_parser.IsValid() Then
271 blob_parser.Validate()
273 blob_parser.Execute()
275 ' ブローブ解析の結果に対して、さらにフィルタリング ( ここでは面積 )
276 Dim filters = New List(Of Blob.CFviBlobFilterRange)()
277 filters.Add(New Blob.CFviBlobFilterRange(Blob.FeatureType.AREA, blobAreaMinThreshold, blobAreaMaxThreshold))
278 Dim blob_list = blob_result.GetBlobList(filters)
279 Console.WriteLine($"instance_id={instance_id}: blob count={blob_list.Count}")
280 For Each kv In blob_list
281 Dim blob_data = New Blob.CFviBlobData(kv)
283 Dim region = blob_data.GetRegion()
284 Dim category_id = CUInt(FVIL.Region.Measure.MaxPixel(category_map, region, OriginMode.Window)) ' max=min のはずで、領域をすべて見なくても 1 ピクセルを確認するのみでも良い
286 Console.WriteLine($"instance_id={instance_id}: category_id={category_id}, blob no={blob_data.BlobNo}")
288 ' 以下、ブローブ特徴量に基づく解析も可能
291 ' 検出した領域の二値画像を直接リージョンに変換
292 Dim region_mask = New CFviRegion(bin_mask)
293 ' リージョン特徴量に基づく解析や、画像に対するリージョン内の特徴量の取得が可能
294 'var mean = FVIL.Region.Measure.MeanPixel(image, region_mask);// 例: 元画像のリージョン内の平均濃度値の取得
298 Throw New NotImplementedException($"unknwon model-category={model.ModelCategory}")
300 Catch ex As CFviException
301 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
302 Console.WriteLine(ex.StackTrace)
303 Catch ex As Exception
304 Console.WriteLine($"Message={ex.Message}")
305 Console.WriteLine(ex.StackTrace)