Beispiel #1
0
        static async Task <int> Main(string[] args)
        {
            try
            {
                //
                // Parse options
                //

                Options = new AppOptions();
                Options.Parse(args);

                if (Options.ShowList)
                {
                }
                if (Options.Exit)
                {
                    return(-1);
                }
                if (string.IsNullOrEmpty(Options.FileName))
                {
                    throw new ApplicationException("Please use --file to specify which file to use");
                }


                //
                // Init module client
                //

                if (Options.UseEdge)
                {
                    Log.WriteLine($"{AppOptions.AppName} module starting.");
                    await BlockTimer("Initializing Azure IoT Edge", async() => await InitEdge());
                }

                cts = new CancellationTokenSource();
                AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
                Console.CancelKeyPress += (sender, cpe) => cts.Cancel();


                //
                // Load model
                //

                MLModel model = null;
                Console.WriteLine($"Loading model from: '{Options.ModelPath}', Exists: '{File.Exists(Options.ModelPath)}'");
                await BlockTimer($"Loading modelfile '{Options.ModelPath}' on the {(Options.UseGpu ? "GPU" : "CPU")}",
                                 async() =>
                {
                    var d    = Directory.GetCurrentDirectory();
                    var path = d + "\\" + Options.ModelPath;

                    StorageFile modelFile = await AsAsync(StorageFile.GetFileFromPathAsync(path));
                    model = await MLModel.CreateFromStreamAsync(modelFile);
                });


                do
                {
                    //
                    // Open file
                    //
                    var rows = new List <DataRow>();
                    try
                    {
                        using (var fs = new StreamReader(Options.FileName))
                        {
                            // I just need this one line to load the records from the file in my List<CsvLine>
                            rows = new CsvHelper.CsvReader(fs).GetRecords <DataRow>().ToList();
                            Console.WriteLine($"Loaded {rows.Count} row(s)");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    Console.WriteLine(rows);


                    //
                    // Main loop
                    //

                    foreach (var row in rows)
                    {
                        //
                        // Evaluate model
                        //

                        var inputShape = new long[2] {
                            1, 4
                        };
                        var inputFeatures = new float[4] {
                            row.Temperature, row.Pressure, row.Humidity, row.ExternalTemperature
                        };

                        MLModelVariable result    = null;
                        var             evalticks = await BlockTimer("Running the model",
                                                                     async() =>
                        {
                            result = await model.EvaluateAsync(new MLModelVariable()
                            {
                                Variable = TensorFloat.CreateFromArray(inputShape, inputFeatures)
                            });
                        });

                        //
                        // Print results
                        //

                        var message = new MessageBody
                        {
                            result = result.Variable.GetAsVectorView().First()
                        };
                        message.metrics.evaltimeinms = evalticks;
                        var json = JsonConvert.SerializeObject(message);
                        Log.WriteLineRaw($"Recognized {json}");

                        //
                        // Send results to Edge
                        //

                        if (Options.UseEdge)
                        {
                            var eventMessage = new Message(Encoding.UTF8.GetBytes(json));
                            await ioTHubModuleClient.SendEventAsync("resultsOutput", eventMessage);

                            // Let's not totally spam Edge :)
                            await Task.Delay(500);
                        }


                        Console.WriteLine("Waiting 1 second...");
                        Thread.Sleep(1000);
                    }
                }while (Options.RunForever && !cts.Token.IsCancellationRequested);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(-1);
            }

            return(0);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            try
            {
                //
                // Parse options
                //

                Options = new AppOptions();

                Options.Parse(args);

                //
                // Enumerate devices
                //

                var devicepaths = SerialPort.GetPortNames();
                if (Options.List || string.IsNullOrEmpty(Options.DeviceName))
                {
                    if (devicepaths.Length > 0)
                    {
                        Log.WriteLine("Available devices:");

                        foreach (var devicepath in devicepaths)
                        {
                            Console.WriteLine($"{devicepath}");
                        }
                    }
                    else
                    {
                        Log.WriteLine("No available devices");
                    }
                    return;
                }

                //
                // Open Device
                //

                var deviceid = devicepaths.Where(x => x.Contains(Options.DeviceName)).SingleOrDefault();
                if (null == deviceid)
                {
                    throw new ApplicationException($"Unable to find device containing {Options.DeviceName}");
                }

                Log.WriteLine($"Connecting to device {deviceid}...");

                using (var device = new SerialPort())
                {
                    //
                    // Configure Device
                    //

                    device.PortName = deviceid;
                    device.BaudRate = 115200;
                    device.Open();

                    //
                    // Dump device info
                    //

                    if (Options.ShowConfig)
                    {
                        Console.WriteLine("=====================================");

                        Console.WriteLine($"Parity: {device.Parity}");
                        Console.WriteLine($"Encoding: {device.Encoding}");
                        Console.WriteLine($"BaudRate: {device.BaudRate}");
                        Console.WriteLine($"DataBits: {device.DataBits}");
                        Console.WriteLine($"StopBits: {device.StopBits}");
                        Console.WriteLine($"Handshake: {device.Handshake}");

                        Console.WriteLine("=====================================");
                    }

                    //
                    // Init module client
                    //

                    if (Options.UseEdge)
                    {
                        Init().Wait();
                    }

                    //
                    // Set up a background thread to read from the device
                    //

                    if (Options.Receive)
                    {
                        var background = Task.Run(() => ReaderTask(device));
                    }

                    //
                    // Continuously write to serial device every second
                    //

                    if (Options.Transmit)
                    {
                        var background = Task.Run(() => TransmitTask(device));
                    }

                    // Wait until the app unloads or is cancelled
                    if (Options.Receive || Options.Transmit)
                    {
                        var cts = new CancellationTokenSource();
                        AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
                        Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
                        WhenCancelled(cts.Token).Wait();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLineException(ex);
            }
        }
Beispiel #3
0
        static async Task <int> Main(string[] args)
        {
            try
            {
                //
                // Parse options
                //

                Options = new AppOptions();

                Options.Parse(args);

                if (Options.ShowList)
                {
                    var devices = await FrameSource.GetSourceNamesAsync();

                    Log.WriteLine("Available cameras:");

                    foreach (var device in devices)
                    {
                        Log.WriteLine(device);
                    }
                }

                if (Options.Exit)
                {
                    return(-1);
                }

                if (string.IsNullOrEmpty(Options.DeviceId))
                {
                    throw new ApplicationException("Please use --device to specify which camera to use");
                }


                //
                // Init module client
                //

                if (Options.UseEdge)
                {
                    Log.WriteLine($"{AppOptions.AppName} module starting.");
                    await BlockTimer("Initializing Azure IoT Edge", async() => await InitEdge());
                }

                cts = new CancellationTokenSource();
                AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
                Console.CancelKeyPress += (sender, cpe) => cts.Cancel();

                //
                // Load model
                //

                ScoringModel model = null;
                await BlockTimer($"Loading modelfile '{Options.ModelPath}' on the {(Options.UseGpu ? "GPU" : "CPU")}",
                                 async() => {
                    var d    = Directory.GetCurrentDirectory();
                    var path = d + "\\" + Options.ModelPath;
                    StorageFile modelFile = await AsAsync(StorageFile.GetFileFromPathAsync(path));
                    model = await ScoringModel.CreateFromStreamAsync(modelFile, Options.UseGpu);
                });

                //
                // Open camera
                //

                using (var frameSource = new FrameSource())
                {
                    await frameSource.StartAsync(Options.DeviceId, Options.UseGpu);

                    //
                    // Main loop
                    //
                    do
                    {
                        Log.WriteLineVerbose("Getting frame...");
                        using (var frame = await frameSource.GetFrameAsync())
                        {
                            var inputImage = frame.VideoMediaFrame.GetVideoFrame();
                            ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);

                            //
                            // Evaluate model
                            //

                            ScoringOutput outcome   = null;
                            var           evalticks = await BlockTimer("Running the model",
                                                                       async() =>
                            {
                                var input = new ScoringInput()
                                {
                                    data = imageTensor
                                };
                                outcome = await model.EvaluateAsync(input);
                            });

                            //
                            // Print results
                            //

                            var message = ResultsToMessage(outcome);
                            message.metrics.evaltimeinms = evalticks;
                            var json = JsonConvert.SerializeObject(message);
                            Log.WriteLineRaw($"Recognized {json}");

                            //
                            // Send results to Edge
                            //

                            if (Options.UseEdge)
                            {
                                var eventMessage = new Message(Encoding.UTF8.GetBytes(json));
                                await ioTHubModuleClient.SendEventAsync("resultsOutput", eventMessage);

                                // Let's not totally spam Edge :)
                                await Task.Delay(500);
                            }
                        }
                    }while (Options.RunForever && !cts.Token.IsCancellationRequested);

                    await frameSource.StopAsync();
                }

                return(0);
            }
            catch (Exception ex)
            {
                Log.WriteLineException(ex);
                return(-1);
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            try
            {
                //
                // Parse options
                //

                Options = new AppOptions();

                Options.Parse(args);
                Log.Enabled = !Options.Quiet;
                Log.Verbose = Options.Verbose;

                //
                // Enumerate devices
                //

                var devicepaths = Win32Serial.Device.EnumerateDevices();
                if (Options.List || string.IsNullOrEmpty(Options.DeviceName))
                {
                    if (devicepaths.Length > 0)
                    {
                        Log.WriteLine("Available devices:");

                        foreach (var devicepath in devicepaths)
                        {
                            Log.WriteLine($"{devicepath}");
                        }
                    }
                    else
                    {
                        Log.WriteLine("No available devices");
                    }
                    return;
                }

                //
                // Open Device
                //

                var deviceid = devicepaths.Where(x => x.Contains(Options.DeviceName)).SingleOrDefault();
                if (null == deviceid)
                {
                    throw new ApplicationException($"Unable to find device containing {Options.DeviceName}");
                }

                Log.WriteLine($"{DateTime.Now.ToLocalTime()} Connecting to device {deviceid}...");

                using (var device = Win32Serial.Device.Create(deviceid))
                {
                    //
                    // Configure Device
                    //

                    var config = device.Config;
                    config.BaudRate = 115200;
                    device.Config   = config;

                    var timeouts = device.Timeouts;
                    timeouts.ReadIntervalTimeout        = 10;
                    timeouts.ReadTotalTimeoutConstant   = 0;
                    timeouts.ReadTotalTimeoutMultiplier = 0;
                    timeouts.WriteTotalTimeoutConstant  = 0;
                    timeouts.WriteTotalTimeoutConstant  = 0;
                    device.Timeouts = timeouts;

                    //
                    // Dump device info
                    //

                    if (Options.ShowConfig)
                    {
                        Log.WriteLine("=====================================");

                        foreach (var line in device.Info)
                        {
                            Log.WriteLine(line);
                        }

                        Log.WriteLine("=====================================");
                    }

                    //
                    // Init module client
                    //

                    if (Options.UseEdge)
                    {
                        Init().Wait();
                    }

                    //
                    // Set up a background thread to read from the device
                    //

                    if (Options.Receive)
                    {
                        var background = Task.Run(() => ReaderTask(device));
                    }

                    //
                    // Continuously write to serial device every second
                    //

                    if (Options.Transmit)
                    {
                        var background = Task.Run(() => TransmitTask(device));
                    }

                    // Wait until the app unloads or is cancelled
                    if (Options.Receive || Options.Transmit)
                    {
                        var cts = new CancellationTokenSource();
                        AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
                        Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
                        WhenCancelled(cts.Token).Wait();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLineException(ex);
            }
        }
        static async Task Main(string[] args)
        {
            try
            {
                //
                // Parse options
                //

                Options = new AppOptions();

                Options.Parse(args);

                if (Options.Exit)
                {
                    return;
                }

                //
                // Open Device
                //

                using (var device = await Si7021.Open())
                {
                    if (null == device)
                    {
                        throw new ApplicationException($"Unable to open sensor. Please ensure that no other applications are using this device.");
                    }

                    //
                    // Dump device info
                    //

                    Log.WriteLineRaw($"Model: {device.Model}");
                    Log.WriteLineRaw($"Serial Number: {device.SerialNumber}");
                    Log.WriteLineRaw($"Firmware Rev: {device.FirmwareRevision}");

                    //
                    // Init module client
                    //

                    if (Options.UseEdge)
                    {
                        Init().Wait();
                    }

                    //
                    // Launch background thread to obtain readings
                    //


                    var background = Task.Run(async() =>
                    {
                        while (true)
                        {
                            device.Update();

                            var message = new MessageBody();
                            message.Ambient.Temperature = device.Temperature;
                            message.Ambient.Humidity    = device.Humidity;
                            message.TimeCreated         = DateTime.Now;

                            string dataBuffer = JsonConvert.SerializeObject(message);
                            var eventMessage  = new Message(Encoding.UTF8.GetBytes(dataBuffer));
                            Log.WriteLineRaw($"SendEvent: [{dataBuffer}]");

                            if (Options.UseEdge)
                            {
                                await ioTHubModuleClient.SendEventAsync("temperatureOutput", eventMessage);
                            }

                            await Task.Delay(1000);
                        }
                    });

                    //
                    // Wait until the app unloads or is cancelled
                    //

                    var cts = new CancellationTokenSource();
                    AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
                    Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
                    WhenCancelled(cts.Token).Wait();
                }
            }
            catch (Exception ex)
            {
                Log.WriteLineException(ex);
            }
        }
Beispiel #6
0
        static async Task <int> Main(string[] args)
        {
            try
            {
                //
                // Parse options
                //

                Options = new AppOptions();

                Options.Parse(args);

                if (Options.ShowList)
                {
                    var devices = await FrameSource.GetSourceNamesAsync();

                    Log.WriteLine("Available cameras:");

                    foreach (var device in devices)
                    {
                        Log.WriteLine(device);
                    }
                }

                if (Options.Exit)
                {
                    return(-1);
                }

                if (!Options.UseImages)
                {
                    if (string.IsNullOrEmpty(Options.DeviceId))
                    {
                        throw new ApplicationException("Please use --device to specify which camera to use");
                    }
                }

                try
                {
                    string sv            = AnalyticsInfo.VersionInfo.DeviceFamilyVersion;
                    ulong  v             = ulong.Parse(sv);
                    ulong  v1            = (v & 0xFFFF000000000000L) >> 48;
                    ulong  v2            = (v & 0x0000FFFF00000000L) >> 32;
                    ulong  v3            = (v & 0x00000000FFFF0000L) >> 16;
                    ulong  v4            = (v & 0x000000000000FFFFL);
                    var    systemVersion = $"{v1}.{v2}.{v3}.{v4}";

                    _stats.CurrentVideoDeviceId = Options.DeviceId;
                    _stats.Platform             = $"{AnalyticsInfo.VersionInfo.DeviceFamily} - {System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") ?? "Unknown"} - {systemVersion}";
                }
                catch (Exception)
                {
                }


                //
                // Init module client
                //

                if (Options.UseEdge)
                {
                    Log.WriteLine($"{AppOptions.AppName} module starting.");
                    await BlockTimer("Initializing Azure IoT Edge", async() => await InitEdge());
                }

                cts = new CancellationTokenSource();
                AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
                Console.CancelKeyPress += (sender, cpe) => cts.Cancel();

                //
                // Load model
                //

                ScoringModel model = null;
                await BlockTimer($"Loading modelfile '{Options.ModelPath}' on the {(Options.UseGpu ? "GPU" : "CPU")}",
                                 async() =>
                {
                    var d    = Directory.GetCurrentDirectory();
                    var path = d + "\\" + Options.ModelPath;
                    StorageFile modelFile = await AsAsync(StorageFile.GetFileFromPathAsync(path));
                    model = await ScoringModel.CreateFromStreamAsync(modelFile, Options.UseGpu);
                });

                _stats.OnnxModelLoaded  = true;
                _stats.CurrentOnnxModel = Options.ModelPath;
                _stats.IsGpu            = Options.UseGpu;

                // WebServer Code


                HttpServer httpsv            = null;
                bool       HttpServerStarted = false;

                if (Options.RunForever)
                {
                    try
                    {
                        Log.WriteLine($"Start HTTP Server on port : " + Options.WebServerPort.ToString());
                        httpsv = new HttpServer(Options.WebServerPort);
                        httpsv.Start();
                        httpsv.OnGet += HttpsvOnOnGet;

                        HttpServerStarted = true;
                        Log.WriteLine($"- HTTP Server Started.");
                        Log.WriteLine($"");
                    }
                    catch (Exception e)
                    {
                        HttpServerStarted = false;
                        Log.WriteLine($"Exiting - Websockets Server Failed to start : " + e.Message);
                    }
                }


                //
                // Open camera
                //

                FrameSource     frameSource     = null;
                ImageFileSource imageFileSource = null;

                if (Options.UseImages)
                {
                    imageFileSource = new ImageFileSource();
                    imageFileSource.ScanUpdateQueue(Options.ImagePath);
                }
                else
                {
                    frameSource = new FrameSource();
                    await frameSource.StartAsync(Options.DeviceId, Options.UseGpu);
                }

                _stats.DeviceInitialized = true;
                SetLatestStatsPayload(JsonConvert.SerializeObject(_stats));

                //
                // Main loop
                //
                do
                {
                    ScoringOutput outcome   = null;
                    int           evalticks = 0;
                    Log.WriteLineVerbose("Getting frame...");

                    byte[]      data          = new byte[] { };
                    byte[]      annotatedData = new byte[] { };
                    MessageBody message       = null;

                    //
                    // Use Image File Source or fall back to Webcam Source if not specified
                    //
                    if (Options.UseImages)
                    {
                        var(fileName, sbmp) = await imageFileSource.GetNextImageAsync(Options.ImagePath, cts.Token);

                        using (var vf = VideoFrame.CreateWithSoftwareBitmap(sbmp))
                        {
                            ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(vf);


                            _stats.TotalFrames = _stats.TotalFrames + 1;

                            //
                            // Evaluate model
                            //

                            var ticksTaken = await BlockTimer($"Running the model",
                                                              async() =>
                            {
                                var input = new ScoringInput()
                                {
                                    data = imageTensor
                                };
                                outcome = await model.EvaluateAsync(input);
                            });

                            evalticks = ticksTaken;

                            message = ResultsToMessage(outcome);
                            message.metrics.evaltimeinms = evalticks;
                            _stats.TotalEvaluations      = _stats.TotalEvaluations + 1;
                            message.imgSrc = fileName;

                            string summaryText = "";

                            if (message.results.Length > 0)
                            {
                                summaryText = $"Matched : {message.results[0].label} - Confidence ={message.results[0].confidence.ToString("P")} - Eval Time {message.metrics.evaltimeinms} ms";
                            }
                            data = await ImageUtils.GetConvertedImage(sbmp);

                            annotatedData = await ImageUtils.AnnotateImage(sbmp, $"Current Image : {fileName ?? "-"}", summaryText);
                        }
                    }
                    else
                    {
                        using (var frame = await frameSource.GetFrameAsync())
                        {
                            var inputImage = frame.VideoMediaFrame.GetVideoFrame();
                            ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);

                            _stats.TotalFrames = _stats.TotalFrames + 1;

                            //
                            // Evaluate model
                            //
                            var ticksTaken = await BlockTimer("Running the model",
                                                              async() =>
                            {
                                var input = new ScoringInput()
                                {
                                    data = imageTensor
                                };
                                outcome = await model.EvaluateAsync(input);
                            });

                            evalticks = ticksTaken;

                            message = ResultsToMessage(outcome);
                            message.metrics.evaltimeinms = evalticks;
                            _stats.TotalEvaluations      = _stats.TotalEvaluations + 1;

                            string summaryText = "";

                            if (message.results.Length > 0)
                            {
                                summaryText = $"Matched : {message.results[0].label} - Confidence ={message.results[0].confidence.ToString("P")} - Eval Time {message.metrics.evaltimeinms} ms";
                            }
                            data = await ImageUtils.GetConvertedImage(inputImage.SoftwareBitmap);

                            annotatedData = await ImageUtils.AnnotateImage(inputImage.SoftwareBitmap, $"Current Webcam : {Options.DeviceId ?? "-"}", summaryText);
                        }
                    }

                    if (message != null)
                    {
                        //
                        // Print results
                        //
                        message.metrics.evaltimeinms = evalticks;
                        var json = JsonConvert.SerializeObject(message,
                                                               new JsonSerializerSettings
                        {
                            //won't print null imgSrc if null
                            NullValueHandling = NullValueHandling.Ignore
                        });
                        Log.WriteLineRaw($"Inferenced: {json}");

                        if (Options.UseWebServer)
                        {
                            //
                            // Update the latest webserver payload snapshots with data from inferencing
                            //
                            UpdateWebServerPayloads(message, data, annotatedData);
                        }

                        //
                        // Send results to Edge
                        //


                        if (Options.UseEdge)
                        {
                            var eventMessage = new Message(Encoding.UTF8.GetBytes(json));
                            await ioTHubModuleClient.SendEventAsync("resultsOutput", eventMessage);

                            // Let's not totally spam Edge :)
                            await Task.Delay(500);
                        }
                        else if (Options.UseImages)
                        {
                            //slow it down a little..
                            await Task.Delay(TimeSpan.FromSeconds(5));
                        }
                    }
                }while (Options.RunForever && !cts.Token.IsCancellationRequested);

                if (frameSource != null)
                {
                    await frameSource.StopAsync();
                }


                if (HttpServerStarted)
                {
                    try
                    {
                        Log.WriteLine($"- Stopping Web Server.");
                        httpsv.OnGet -= HttpsvOnOnGet;
                        httpsv.Stop();
                        httpsv = null;
                        Log.WriteLine($"- Web Server Stopped.");
                    }
                    catch (Exception)
                    {
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Log.WriteLineException(ex);
                return(-1);
            }
        }