public override async Task AzureModuleInitAsync <C>(C c) { AzureConnection c1 = c as AzureConnection; await base.AzureModuleInitAsync(c1); await _moduleClient.SetInputMessageHandlerAsync(Keys.ModuleLoadedInputRoute, ModuleLoadedMessageHandler, this); await _moduleClient.SetMethodHandlerAsync(Keys.SetModuleLoaded, SetModuleLoaded, this); await base.AzureModuleInitEndAsync(); }
static async Task CameraProcessingAsync(Model model, MediaFrameReader reader, EventWaitHandle evtframe, AzureConnection azure) { var fps_t0 = DateTime.Now; string prev_label = null; try { double fps = 0.0; for (UInt64 total_frame = 0, current_frame = 0; ; ++total_frame) { if (Model.Full) { evtframe.WaitOne(); evtframe.Reset(); } //auto input_feature{ImageFeatureValue::CreateFromVideoFrame(vf.get())}; var frame = reader.TryAcquireLatestFrame(); if (frame == null) { // assume 60 fps, wait about half a frame for more input. // in the unlikely event that eval is faster than capture this should be done differently Thread.Sleep(10); continue; } ++current_frame; //int oldFrame, double oldFps, DateTime oldFpsT0, string oldLabel) (current_frame, fps, fps_t0, prev_label) = await FrameProcessingAsync(model, frame, total_frame, current_frame, fps, fps_t0, prev_label, azure); } } catch (Exception e) { throw new ApplicationException("Camera Processing Failed", e); } }
static async Task <int> MainAsync(AppOptions options) { //Log.WriteLine("pause..."); //var x = Console.ReadLine(); Log.WriteLine("Starting async..."); Model model = null; AzureConnection connection = null; MediaFrameReader reader = null; EventWaitHandle evtFrame = null; if (options.List) { await EnumFrameSourcesAsync(); Environment.Exit(3); } await Task.WhenAll( Task.Run(async() => { try { model = await Model.CreateModelAsync( Directory.GetCurrentDirectory() + "\\resources\\office_fruit.onnx", options.Gpu); if (options.Test) { await Task.CompletedTask; } else { connection = await AzureConnection.CreateAzureConnectionAsync(); } } catch (Exception e) { Log.WriteLineError("failed to create model {0}", e.ToString()); Environment.Exit(2); } }), Task.Run(async() => { try { (reader, evtFrame) = await GetFrameReaderAsync(); await AsyncHelper.AsAsync(reader.StartAsync()); } catch (Exception e) { Log.WriteLineError("failed to start frame reader {0}", e.ToString()); Environment.Exit(2); } })); try { AzureModule m = null; EventHandler <string> ModuleLoadedHandler = async(Object sender, string moduleName) => { try { Log.WriteLine("module loaded. resending state"); await connection.NotifyNewModuleOfCurrentStateAsync(); } catch (Exception e) { Log.WriteLineError("failed to notify state {0}", e.ToString()); Environment.Exit(2); } }; if (connection != null) { m = (AzureModule)connection.Module; m.ModuleLoaded += ModuleLoadedHandler; } try { Log.WriteLine("Model loaded, Azure Connection created, and FrameReader Started\n\n\n\n"); await CameraProcessingAsync(model, reader, evtFrame, connection); } finally { if (connection != null) { m.ModuleLoaded -= ModuleLoadedHandler; } } } finally { if (connection != null) { connection.Dispose(); } reader.Dispose(); model.Dispose(); } return(0); }
static async Task <Tuple <UInt64, double, DateTime, string> > FrameProcessingAsync(Model model, MediaFrameReference frame, UInt64 totalFrame, UInt64 oldFrame, double oldFps, DateTime oldFpsT0, string oldLabel, AzureConnection azure) { UInt64 currentFrame = oldFrame; double fps = oldFps; DateTime fpsT0 = oldFpsT0; string prevLabel = oldLabel; string correlationId = String.Format("{0}", totalFrame); try { var r = await model.EvaluateAsync(frame, correlationId); if (!r._result.Succeeded) { Log.WriteLineUp3Home("eval failed {0}", r._result.ErrorStatus); } else { Log.WriteLineUp3Home("eval succeeded"); } var fps_interval = DateTime.Now - fpsT0; if (fps_interval.Seconds > 10) { fps = currentFrame * 1.0 / fps_interval.Seconds; currentFrame = 0; fpsT0 = DateTime.Now; } Log.WriteLineHome("fps {0}", fps.ToString("0.00")); string label = r._output.classLabel; var most = r.MostProbable; if (label != most.Key) { throw new ApplicationException(string.Format("Output Feature Inconsistency model output label 0 '{0}' and label 1 '{1}'", label, most.Key)); } Log.WriteLineSuccess("{0} with probability {1}", most.Key, most.Value.ToString("0.0000")); // we would like to apply a confidence threshold but with these models garbage always ends up high confidence something if (prevLabel == null || prevLabel != label) { prevLabel = label; if (azure != null) { azure.UpdateObject(new KeyValuePair <string, object>(Keys.FruitSeen, label)); } } model.Clear(correlationId); } catch (Exception e) { throw new ApplicationException("Frame Processing Failed", e); } return(new Tuple <UInt64, double, DateTime, string>(currentFrame, fps, fpsT0, prevLabel)); }