コード例 #1
0
    private async Task MachineLearningTask()
    {
        var ModelInput  = new NumberModelInput();
        var ModelGen    = new NumberModel();
        var ModelOutput = new NumberModelOutput();

        try
        {
            StorageFile modelFile = await ApplicationData.Current.LocalFolder.GetFileAsync(onnxName);

            ModelGen = await NumberModel.CreateNumberModel(modelFile);
        }
        catch (Exception e)
        {
            UnityEngine.WSA.Application.InvokeOnAppThread(() =>
            {
                TextData.text = e.ToString();
            }, true);
        }

        while (true)
        {
            if (bytes != null)
            {
                // Unityのカメラは上下反転しているので入れ替え処理
                var buf = new byte[bytes.Length];
                for (int i = 0; i < TexHeight; i++)
                {
                    for (int j = 0; j < TexWidth; j++)
                    {
                        buf[(TexWidth * (TexHeight - 1 - i) + j) * 4 + 0] = bytes[(TexWidth * i + j) * 4 + 0];
                        buf[(TexWidth * (TexHeight - 1 - i) + j) * 4 + 1] = bytes[(TexWidth * i + j) * 4 + 1];
                        buf[(TexWidth * (TexHeight - 1 - i) + j) * 4 + 2] = bytes[(TexWidth * i + j) * 4 + 2];
                        buf[(TexWidth * (TexHeight - 1 - i) + j) * 4 + 3] = bytes[(TexWidth * i + j) * 4 + 3];
                    }
                }
                try
                {
                    var softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba8, TexWidth, TexHeight, BitmapAlphaMode.Premultiplied);
                    softwareBitmap.CopyFromBuffer(buf.AsBuffer());
                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

                    ModelInput.data = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
                    ModelOutput     = await ModelGen.EvaluateAsync(ModelInput);

                    float  maxProb      = 0;
                    string maxIndexName = "";
                    foreach (var item in ModelOutput.loss)
                    {
                        if (item.Value > maxProb)
                        {
                            maxIndexName = item.Key;
                            maxProb      = item.Value;
                        }
                    }
                    softwareBitmap.Dispose();
                    bytes = null;
                    buf   = null;
                    UnityEngine.WSA.Application.InvokeOnAppThread(() =>
                    {
                        TextData.text = maxIndexName + ":" + maxProb;
                    }, true);
                }
                catch (Exception e)
                {
                    UnityEngine.WSA.Application.InvokeOnAppThread(() =>
                    {
                        TextData.text = e.ToString();
                    }, true);
                }
            }
        }
    }