Beispiel #1
0
        public async Task <bool> Load()
        {
            var status = await _client.GetStatusAsync();

            if (status != null && status.Contains("server is running"))
            {
                return(true);
            }

            _mlSharpPython = new MlSharpPython(_pythonExecName);
            if (await Task.Run(() => _mlSharpPython.Run(_fileNameParameter)))
            {
                var      startTime   = DateTime.Now;
                TimeSpan waitingTime = new TimeSpan(0, 0, 0, 2);
                while (DateTime.Now - startTime < waitingTime)
                {
                    Thread.Sleep(100);
                    status = await _client.GetStatusAsync();

                    if (status != null && status.Contains("server is running"))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #2
0
        public async Task <List <BoundBox> > Predict(Frame frame)
        {
            var list = new List <BoundBox>();

            // Instantiate Machine Learning C# - Python class object
            IMlSharpPython mlSharpPython = new MlSharpPython("python3");

            // Test image
            string imagePathName = "";
            var    appPath       = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            var    modelPatch    = $"{appPath}/python/snapshots/resnet50_liza_alert_v1_interface.h5";

            // Define Python script file and input parameter name
            string fileNameParameter = $"{appPath}/python/inference.py --model {modelPatch} --image {frame.Patch}";

            // Execute the python script file
            var outputText = await Task.Run(() => mlSharpPython.ExecutePythonScript(fileNameParameter));

            if (!string.IsNullOrEmpty(outputText))
            {
                foreach (var line in outputText.Split(Environment.NewLine))
                {
                    if (!line.StartsWith("output="))
                    {
                        continue;
                    }
                    var output = line.Split(' ');
                    var x1     = int.Parse(output[1]);
                    var y1     = int.Parse(output[2]);
                    var x2     = int.Parse(output[3]);
                    var y2     = int.Parse(output[4]);
                    var score  = output[6];
                    var label  = output[5];
                    var rect   = new BoundBox(
                        x1,
                        y1,
                        y2 - y1,
                        x2 - x1);
                    list.Add(rect);
                    Console.WriteLine($"{label}: {score}");
                }
            }
            return(list);
        }