Example #1
0
 public Frame(PendingItem item, List <InterestingObject> interesting)
 {
     Timestamp   = item.TimeEnqueued; // handy to have it
     Item        = item;
     Interesting = interesting;
 }
Example #2
0
        // This function is used by the (semi) live data, not the UI
        public async Task <AIResult> DetectObjectsAsync(Stream stream, PendingItem pending)
        {
            List <ImageObject> objects  = null;
            AIResult           aiResult = new AIResult
            {
                ObjectsFound = objects,
                Item         = pending
            };


            using (HttpClient client = new HttpClient())
            {
                using (StreamContent content = new StreamContent(stream))
                {
                    using (var request = new MultipartFormDataContent
                    {
                        { content, "image", pending.PendingFile }
                    })
                    {
                        string url = string.Format("http://{0}:{1}/v1/vision/detection", _AILocation, _AIPort);

                        HttpResponseMessage output = null;
                        try
                        {
                            DateTime startPost = DateTime.Now;
                            pending.TimeDispatched = startPost;

                            output = await client.PostAsync(new Uri(url), request).ConfigureAwait(false);

                            pending.TimeProcessingByAI();
                            TimeSpan postTime = DateTime.Now - startPost;
                        }
                        catch (AggregateException ex)
                        {
                            throw new AiNotFoundException(url);
                        }
                        catch (Exception ex)
                        {
                            throw new AiNotFoundException(url);
                        }

                        if (!output.IsSuccessStatusCode)
                        {
                            throw new AiNotFoundException(url);
                        }

                        var jsonString = await output.Content.ReadAsStringAsync().ConfigureAwait(false);

                        output.Dispose();

                        TimeSpan processTime = pending.TimeProcessingByAI();
                        // Console.WriteLine("Process Time: " + processTime.TotalMilliseconds.ToString());
                        Response response = JsonConvert.DeserializeObject <Response>(jsonString);

                        if (response.Predictions != null && response.Predictions.Length > 0)
                        {
                            foreach (var result in response.Predictions)
                            {
                                if (objects == null)
                                {
                                    objects = new List <ImageObject>();
                                }

                                result.Success = true;

                                // Windows likes Rectangles, so it is easier to create one now
                                result.ObjectRectangle = Rectangle.FromLTRB(result.X_min, result.Y_min, result.X_max, result.Y_max);
                                result.ID = Guid.NewGuid();

                                objects.Add(result);

                                string o = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", result.Label, result.Confidence, result.X_min, result.Y_min, result.X_max, result.Y_max);
                                Dbg.Trace(o);
                            }
                        }
                        // DebugWriter.Write(jsonString);
                    }
                }
            }

            aiResult.ObjectsFound = objects;
            return(aiResult);
        }