Пример #1
0
        private async Task Processing()
        {
            string path = (string)labelPath.Content;

            if (path is null)
            {
                path = "..\\..\\..\\images";
            }
            int  tasksCount = 4;
            bool done       = false;

            Task extractResults = Task.Run(() => {
                ImageResult predictionOutput;
                while (true)
                {
                    Thread.Sleep(0);
                    if (ImageClassifier.cts.Token.IsCancellationRequested || done)
                    {
                        return;
                    }
                    if (ImageClassifier.predictionOutputs.TryDequeue(out predictionOutput))
                    {
                        Dispatcher.Invoke(() =>
                        {
                            Pair replacing = ClassesCounts.FirstOrDefault(x => x.ClassLabel == predictionOutput.outputLabel);
                            if (replacing != null)
                            {
                                var index = ClassesCounts.IndexOf(replacing);
                                ClassesCounts[index].Count += 1;
                            }
                            else
                            {
                                ClassesCounts.Add(new Pair(predictionOutput.outputLabel, 1));
                            }

                            ObservableCollection <string> localImages = null;
                            if (!ImagesInClass.TryGetValue(predictionOutput.outputLabel, out localImages))
                            {
                                localImages = new ObservableCollection <string>();
                                ImagesInClass[predictionOutput.outputLabel] = localImages;
                            }
                            string localPath = predictionOutput.path.Substring(predictionOutput.path.LastIndexOf('\\') + 1);
                            ImagesInClass[predictionOutput.outputLabel].Add(localPath);
                        });
                    }
                }
            });

            await ImageClassifier.parallelProcess(path, tasksCount);

            if (!ImageClassifier.cts.IsCancellationRequested)
            {
                done = true;
            }

            await extractResults;
        }
Пример #2
0
        private async void Post()
        {
            string path = (string)labelPath.Content;

            if (path is null)
            {
                path = "../../../images";
            }
            var content = new StringContent(JsonConvert.SerializeObject(path), Encoding.UTF8, "application/json");
            HttpResponseMessage httpResponse;

            try
            {
                httpResponse = await client.PostAsync(url, content, ImageClassifier.cts.Token);
            }
            catch (HttpRequestException)
            {
                await Dispatcher.BeginInvoke(new Action(() =>
                {
                    System.Windows.Forms.MessageBox.Show("No connection");
                }));

                return;
            }

            if (httpResponse.IsSuccessStatusCode)
            {
                var results = JsonConvert.DeserializeObject <List <ImageResult> >(httpResponse.Content.ReadAsStringAsync().Result);
                foreach (var a in results)
                {
                    Pair replacing = ClassesCounts.FirstOrDefault(x => x.ClassLabel == a.OutputLabel);
                    if (replacing != null)
                    {
                        var index = ClassesCounts.IndexOf(replacing);
                        ClassesCounts[index].Count += 1;
                    }
                    else
                    {
                        ClassesCounts.Add(new Pair(a.OutputLabel, 1));
                    }

                    ObservableCollection <string> localImages = null;
                    if (!ImagesInClass.TryGetValue(a.OutputLabel, out localImages))
                    {
                        localImages = new ObservableCollection <string>();
                        ImagesInClass[a.OutputLabel] = localImages;
                    }
                    string localPath = a.Path.Substring(a.Path.LastIndexOf('\\') + 1);
                    ImagesInClass[a.OutputLabel].Add(localPath);
                }
            }
            labelWait.Content = "";
        }