7. Windows ML APIを使うための環境
実行環境 開発環境
• Windows 10:バージョン1809 • Windows 10
• Windows SDK 17763以降
• Visual Studio 2017
開発環境は必ずしもOSバージョン1809でなくても開発は可能。
実行時には1809でないとだめ。
12. 4.自動生成されるラッパークラスを修正
public sealed class TinyYoloV1_2Input{
public TensorFloat image; // shape(-1,3,416,416)
}
public sealed class TinyYoloV1_2Output{
public TensorFloat grid; // shape(-1,125,13,13)
}
生成直後(抜粋)
ここはVideoFrame or ImageFeatureValueに変更!
public sealed class TinyYoloV1_2Input{
public VideoFrame image; // shape(-1,3,416,416)
}
public sealed class TinyYoloV1_2Output{
public TensorFloat grid; // shape(-1,125,13,13)
}
修正後(抜粋)
入力が画像データの場合は中身を確認して書換えを実施
13. 5.画像を取り込むための実装
UWPのMediaCaptureクラスを用いてカメラ画像をキャプチャ
キャプチャの際の画像フォーマットは「BGRA8」
var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var device = devices[0];
_capture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings {
VideoDeviceId = device.Id
};
await _capture.InitializeAsync(settings);
_lowLagPhotoCapture = await _capture.PrepareLowLagPhotoCaptureAsync(
ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
_capture.VideoDeviceController.Focus.TrySetAuto(true);
asyncOperation = await _lowLagPhotoCapture.CaptureAsync();
実装例(抜粋)
14. 6.ONNXを使って取得した画像を推論にかける
ラッパークラスを利用して推論処理を実行する
var input = new TinyYoloV1_2Input();
input.image = VideoFrame.CreateWithSoftwareBitmap(SoftwareBitmap.Convert(
softwareBitmap, BitmapPixelFormat.Bgra8));
var tinyYoloV10ModelOutput = await _model.EvaluateAsync(input);
実装例(抜粋)