示例#1
0
        // Called by the AI to navigate through the working set
        public async Task <List <ImageObject> > ProcessVideoImageViaAI(Stream stream, string imageName)
        {
            List <ImageObject> result = null;
            AILocation         ai     = AILocation.GetAvailableAI();

            if (ai != null)
            {
                result = await ProcessImageSync(ai, stream, imageName).ConfigureAwait(false); // yes, it is not really async, but this doesn't know it
            }
            return(result);
        }
示例#2
0
        // This function is used by the (semi) live data, not the UI
        public static 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 }
                    })
                    {
                        AILocation ai = AILocation.GetAvailableAI();
                        if (ai == null)
                        {
                            throw new AiNotFoundException("No AI Location is Currently Defined");
                        }
                        string url = string.Format("http://{0}:{1}/v1/vision/detection", ai.IPAddress, ai.Port);

                        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);
        }