public async Task <ModelResponse> SendRequest(MRCnnResponse lastPrediction, List <Rect> positions, string activeUrl) { var client = httpClientFactory.CreateClient(); var url = BuildUrl(positions, activeUrl); var response = await client.GetAsync(url); if (!response.IsSuccessStatusCode) { lastPrediction.Online = false; throw new ModelUnreachable(); } else { lastPrediction.Online = true; } var content = await response.Content.ReadAsStringAsync(); try { return(JsonConvert.DeserializeObject <ModelResponse>(content)); } catch { throw new InvalidModelResponse(); } }
public MRCnnResponse GetLastPrediction(int source) { if (LastPredictions.ContainsKey(source)) { return(LastPredictions[source]); } return(LastPredictions[source] = new MRCnnResponse()); }
private async Task PredictBySource(MRCnnResponse lastPrediction, StreamSource streamSource) { try { lastPrediction.Working = true; Timer.Restart(); List <Rect> positions = await controls.AllSelected(streamSource.Id); var currentSourceId = streamSource.Id; var activeUrl = Enviroment.SourceImageRemote(streamSource); if (positions.Count == 0) { lastPrediction.Free = 0; lastPrediction.Total = 0; lastPrediction.Rects = new List <DrawRects>(); return; } var model = await SendRequest(lastPrediction, positions, activeUrl); var carDetections = model.Data .Where(x => x[1] == "car") .ToList(); // Save to history lastPrediction.AddPrediction(model); lastPrediction.Result = positions .Select(x => new DrawRects(x.x1, x.y1, x.x2, x.y2)) .ToList(); lastPrediction.Detected = model.Detected; // Format newest data lastPrediction.Total = positions.Count; lastPrediction.Free = model.Detected .Where(x => x < 0.4) .Count(); lastPrediction.Width = model.Width; lastPrediction.Height = model.Height; lastPrediction.SourceId = currentSourceId; lastPrediction.Miliseconds = (int)Timer.ElapsedMilliseconds; lastPrediction.Online = true; lastPrediction.ParseRects(model.Rects); } catch (Exception e) { lastPrediction.Online = false; } finally { lastPrediction.Working = false; } }