Exemple #1
0
 public ExecuteProcessing(ILogger <ExecuteProcessing> logger, IProcessImage processImage, IPersistData persistData,
                          IHashService hashService)
 {
     _Logger       = logger;
     _ProcessImage = processImage;
     _PersistData  = persistData;
     _HashService  = hashService;
 }
        /// <summary>
        /// Process the image. The image is given to the model ML and if a
        /// valid recognition is found then the image is uploaded to storage.
        /// Processing times are tracked.
        /// </summary>
        /// <param name="body">
        /// An ImageBody object containing the image from the camera.
        /// </param>
        public static void Process(BlobStorageHelper blobHelper, ModuleClient client, IProcessImage imageProc, ImageBody body)
        {
            ImageProcessor proc = new ImageProcessor(blobHelper, client, imageProc.ProcessorType, body);

            // Perform and measure elapsed time for the ML model work
            DateTime startTime = DateTime.Now;

            proc.features            = imageProc.Process(body.SmallImage);
            proc.recognitionDuration = DateTime.Now - startTime;

            // Loop to the next recognition task without waiting for the report to process
            if (proc.features != null)
            {
                Task reportTask = new Task(() => proc.Report());
                reportTask.Start();
            }
        }
Exemple #3
0
        static Task OnDesiredPropertiesUpdate(TwinCollection desiredProperties, object userContext)
        {
            Console.WriteLine($"OnDesiredPropertiesUpdate() called on thread {Thread.CurrentThread.ManagedThreadId}");

            lock (s_twinUpdateLocker)
            {
                try
                {
                    Console.WriteLine("Desired property change:");
                    Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));
                    string fpgaRefLevelRGB = "104, 117, 123";

                    string mlModelType = CpuModel.CpuModelProcessorType;
                    if (desiredProperties.Contains("mlModelType"))
                    {
                        mlModelType = desiredProperties["mlModelType"];
                    }
                    if (desiredProperties.Contains("FPGA"))
                    {
                        JObject fpgaProps = desiredProperties["FPGA"];
                        if (fpgaProps["rgbRefLevel"] != null)
                        {
                            fpgaRefLevelRGB = (string)fpgaProps["rgbRefLevel"];
                        }
                    }
                    Console.WriteLine($"Model type: {mlModelType}");
                    // Default to "CPU" on malformed input
                    switch (mlModelType)
                    {
                    // Don't change valid non-"CPU" inputs
                    case GpuModel.GpuModelProcessorType:
                    case FpgaModel.FpgaModelProcessorType:
                    case CpuModel.CpuModelProcessorType:
                        break;

                    default:
                        Console.WriteLine($"Malformed mlModelType: {mlModelType} -- defaulting to 'CPU'");
                        mlModelType = CpuModel.CpuModelProcessorType;
                        break;
                    }
                    if (mlModelType != s_currentModel.ProcessorType)
                    {
                        try
                        {
                            s_currentModel.Disconnect();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Error disconnecting {s_currentModel.ProcessorType} model: {ex.Message}");
                        }
                        Console.WriteLine($"Switching to {mlModelType} model.");
                        switch (mlModelType)
                        {
                        // Don't change valid non-"CPU" inputs
                        case GpuModel.GpuModelProcessorType:
                            s_currentModel = new GpuModel();
                            break;

                        case FpgaModel.FpgaModelProcessorType:
                            s_currentModel = new FpgaModel("voiddetectionbrainwave", 50051, fpgaRefLevelRGB);
                            break;

                        case CpuModel.CpuModelProcessorType:
                        default:
                            s_currentModel = new CpuModel();
                            break;
                        }
                    }

                    if (desiredProperties.Contains("blobStorageSasUrl"))
                    {
                        string sasUrl = desiredProperties["blobStorageSasUrl"];
                        // TODO: it would be nice to be able to compare the URL to the one
                        // in the current s_blobHelper (if any).
                        Console.WriteLine("Creating new BlobStorageHelper");
                        s_blobHelper = new BlobStorageHelper("still-images", sasUrl);
                    }
                }
                catch (AggregateException ex)
                {
                    foreach (Exception exception in ex.InnerExceptions)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error when receiving desired property: {0}", exception);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error when receiving desired property: {0}", ex.Message);
                }
            }
            return(Task.CompletedTask);
        }
Exemple #4
0
        /// <summary>
        /// Process the image. The image is given to the model ML and if a
        /// valid recognition is found then the image is uploaded to storage.
        /// Processing times are tracked.
        /// </summary>
        /// <param name="body">
        /// An ImageBody object containing the image from the camera.
        /// </param>
        public static void Process(BlobStorageHelper blobHelper, ModuleClient client, IProcessImage imageProc, ImageBody body)
        {
            ImageProcessor proc = new ImageProcessor(blobHelper, client, imageProc.ProcessorType, body);

            if (!body.SkipMlProcessing)
            {
                // Perform and measure elapsed time for the ML model work
                DateTime startTime = DateTime.Now;
                proc.features            = imageProc.Process(body.SmallImageRGB);
                proc.recognitionDuration = DateTime.Now - startTime;

                // Loop to the next recognition task without waiting for the report to process
                if (proc.features != null)
                {
                    Task reportTask = new Task(() => proc.Report());
                    reportTask.Start();
                }
            }
            else
            {
                // Don't process ML or send any messages; just upload to BLOB
                try
                {
                    proc.UploadToBlob();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"  Failed to upload image to BLOB store: {ex.Message}");
                }
            }
        }
 public Logic(IProcessImage processor)
 {
     _processor = processor;
 }