Example #1
0
        private void InferenceEmotion(int shotsTaken, NDArray img_buffer_)
        {
            var         imgArr    = ReadTensorFromDetected(img_buffer_, img_size: 60);
            ConfigProto config    = new ConfigProto();
            GPUOptions  gpuConfig = new GPUOptions();

            gpuConfig.AllowGrowth = true;
            gpuConfig.PerProcessGpuMemoryFraction = 0.3;
            config.GpuOptions = gpuConfig;

            using (var sess = tf.Session(emotionGraph, config))
            {
                Tensor   tensorClasses = emotionGraph.OperationByName("Identity");
                Tensor   imgTensor     = emotionGraph.OperationByName("x");
                Tensor[] outTensorArr  = new Tensor[] { tensorClasses };

                var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr));

                var emotions = results[0].ToArray <float>();
                //var records = new List<object>
                //{
                //    new { Frame = shotsTaken, Results = results[0] },
                //};
                //csv.WriteRecord(new { Frame = shotsTaken, Results = results[0] });
                //csv.Flush();
                var record = new CSVRecord();
                record.Neutral   = (int)(Math.Round(emotions[0], 2) * 100);
                record.Happy     = (int)(Math.Round(emotions[1], 2) * 100);
                record.Sad       = (int)(Math.Round(emotions[2], 2) * 100);
                record.Angry     = (int)(Math.Round(emotions[3], 2) * 100);
                record.Surprised = (int)(Math.Round(emotions[4], 2) * 100);
                record.Date      = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                using (var stream = File.Open("output.csv", FileMode.Append))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            // Don't write the header again.
                            csv.Configuration.HasHeaderRecord = false;
                            csv.WriteRecord <CSVRecord>(record);
                            csv.NextRecord();
                        }

                Console.WriteLine($"Results: {results[0].ToString()}");
                //PreProcessEmotion(img_buffer, results[0]);
            }
        }
Example #2
0
        private NDArray ReadTensorFromDetected(NDArray img_buffer_, int img_size = 60)
        {
            var         graph     = tf.Graph().as_default();
            ConfigProto config    = new ConfigProto();
            GPUOptions  gpuConfig = new GPUOptions();

            gpuConfig.AllowGrowth = true;
            gpuConfig.PerProcessGpuMemoryFraction = 0.3;
            config.GpuOptions = gpuConfig;

            var t3 = tf.constant(img_buffer_, dtype: TF_DataType.TF_UINT8);
            //var inp = tf.reshape(t3, (height, width, 3));
            var casted        = tf.cast(t3, tf.float32);
            var dims_expander = tf.expand_dims(casted, 0);
            var resize        = tf.constant(new int[] { img_size, img_size });
            var bilinear      = tf.image.resize_bilinear(dims_expander, resize);

            using (var sess = tf.Session(graph, config))
                return(sess.run(bilinear));
        }
Example #3
0
        private NDArray InferenceDetector(NDArray img_buffer_)
        {
            var         imgArr    = ReadTensorFromImageFile(img_buffer_);
            ConfigProto config    = new ConfigProto();
            GPUOptions  gpuConfig = new GPUOptions();

            gpuConfig.AllowGrowth = true;
            gpuConfig.PerProcessGpuMemoryFraction = 0.3;
            config.GpuOptions = gpuConfig;

            using (var sess = tf.Session(detectorGraph, config))
            {
                Tensor   tensorClasses = detectorGraph.OperationByName("Identity");
                Tensor   imgTensor     = detectorGraph.OperationByName("x");
                Tensor[] outTensorArr  = new Tensor[] { tensorClasses };

                var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr));

                //Console.WriteLine($"Results: {results[0].ToString()}");
                return(PreProcessEmotion(img_buffer_, results[0]));
            }
        }