コード例 #1
0
ファイル: PetModel.cs プロジェクト: zy55769068/DogOrCat
        public async Task <PetModelOutput> EvaluateAsync(PetModelInput input)
        {
            binding.Bind("data", input.data);
            var result = await session.EvaluateAsync(binding, "0");

            var output = new PetModelOutput();

            output.classLabel = result.Outputs["classLabel"] as TensorString;
            output.loss       = result.Outputs["loss"] as IList <IDictionary <string, float> >;
            return(output);
        }
コード例 #2
0
        public async void Classification(byte[] bytes)
        {
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{modelFileName}"));

            petModel = await PetModelModel.CreateFromStreamAsync(file);

            try
            {
                var newBitmap = new WriteableBitmap(255, 255);

                using (var stream = new InMemoryRandomAccessStream())
                {
                    await stream.WriteAsync(bytes.AsBuffer());

                    stream.Seek(0);
                    await newBitmap.SetSourceAsync(stream);
                }

                var outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(
                    newBitmap.PixelBuffer,
                    BitmapPixelFormat.Bgra8,
                    newBitmap.PixelWidth,
                    newBitmap.PixelHeight
                    );


                var frame = VideoFrame.CreateWithSoftwareBitmap(outputBitmap);


                if (frame != null)
                {
                    try
                    {
                        var inputData = new PetModelInput();
                        inputData.data = ImageFeatureValue.CreateFromVideoFrame(frame);
                        var results = await petModel.EvaluateAsync(inputData);

                        var loss = results.loss.ToList();

                        var catValue = loss.FirstOrDefault()["cat"];
                        var dogValue = loss.FirstOrDefault()["dog"];

                        var result = new Dictionary <string, float>();
                        if (catValue > dogValue)
                        {
                            result.Add("cat", catValue);
                        }
                        else
                        {
                            result.Add("dog", dogValue);
                        }

                        ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"error: {ex.Message}");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }