public ConcurrentQueue <RecognitionInfo> MakePrediction(List <String> images) //doesnt check in db context { MessageToUser?.Invoke(this, "If you want to stop recognition press ctrl + C"); CancellationToken token = cancel.Token; try { ParallelOptions po = new ParallelOptions(); po.CancellationToken = token; po.MaxDegreeOfParallelism = Environment.ProcessorCount; var tasks = Parallel.ForEach <string>(images, po, img => { CQ.Enqueue(ProcessImage(img)); OutputResult?.Invoke(this, CQ); }); } catch (OperationCanceledException) { MessageToUser?.Invoke(this, "-----------------Interrupted-----------------"); Trace.WriteLine("-----------------Interrupted-----------------"); } catch (Exception e) when(e is ArgumentException || e is IOException) { Trace.WriteLine(e.Message); }; return(CQ); }
public ConcurrentQueue <RecognitionInfo> MakePrediction(Dictionary <string, string> info) //doesnt check in db context { MessageToUser?.Invoke(this, "If you want to stop recognition press ctrl + C"); CancellationToken token = cancel.Token; try { ParallelOptions po = new ParallelOptions(); po.CancellationToken = token; po.MaxDegreeOfParallelism = Environment.ProcessorCount; Dictionary <string, string> temp_info = new Dictionary <string, string>(); lock (recognitionLibraryContext) { foreach (var image in info) { RecognitionInfo temp = new RecognitionInfo(image.Key, "", 0); temp.Image = Convert.FromBase64String(image.Value); var sameimage = recognitionLibraryContext.FindOne(temp); if (sameimage != null) { sameimage.Statistic++; Trace.WriteLine("Statisticv " + sameimage.Statistic); recognitionLibraryContext.SaveChanges(); } else { Trace.WriteLine("Const temp_info " + image.Key); temp_info.Add(image.Key, image.Value); } } } var task = Task.Factory.StartNew(() => { Trace.WriteLine("StartNew"); foreach (var img in temp_info) { var t = Task.Run(() => { var tmp = ProcessImage(Convert.FromBase64String(img.Value), img.Key); Trace.WriteLine("Start nFactory = " + tmp.Path); CQ.Enqueue(tmp); OutputResult?.Invoke(this, CQ); }, cancel.Token); Task.WaitAll(t); } }); Task.WaitAll(task); } catch (OperationCanceledException) { MessageToUser?.Invoke(this, "-----------------Interrupted-----------------"); Trace.WriteLine("-----------------Interrupted-----------------"); } catch (Exception e) when(e is ArgumentException || e is IOException) { Trace.WriteLine(e.Message); }; return(CQ); }
public ConcurrentQueue <RecognitionInfo> MakePrediction(string path) //should check dbcontext { MessageToUser?.Invoke(this, "If you want to stop recognition press ctrl + C"); CancellationToken token = cancel.Token; try { IEnumerable <string> images; ParallelOptions po = new ParallelOptions(); po.CancellationToken = token; po.MaxDegreeOfParallelism = Environment.ProcessorCount; if (path == "") // Если пользователь передал пустую директорию, меняем на дефолтную директори проекта с тестовыми изображениями { MessageToUser?.Invoke(this, "You haven't set the path. Changed to embedded directory with images"); images = from file in Directory.GetFiles(DefaultImageDir) where file.Contains(".jpg") || file.Contains(".jpeg") || file.Contains(".png") select file; } images = from file in Directory.GetFiles(path) // пустой путь - throw exception where file.Contains(".jpg") || file.Contains(".jpeg") || file.Contains(".png") select file; if (images.Count() == 0) // Если пользователь передал пустую директорию, меняем на дефолтную директори проекта с тестовыми изображениями { MessageToUser?.Invoke(this, "Your directory is empty. Change to embedded directory with images"); images = from file in Directory.GetFiles(DefaultImageDir) where file.Contains(".jpg") || file.Contains(".jpeg") || file.Contains(".png") select file; } List <string> paths = new List <string>(); lock (recognitionLibraryContext) { foreach (var image in images) { RecognitionInfo temp = new RecognitionInfo(image, "", 0); var sameimage = recognitionLibraryContext.FindOne(temp); if (sameimage != null) { sameimage.Statistic++; recognitionLibraryContext.SaveChanges(); } else { paths.Add(image); } } } var task = Task.Factory.StartNew(() => { Trace.WriteLine("StartNew"); foreach (var img in images) { var t = Task.Run(() => { var tmp = ProcessImage(img); CQ.Enqueue(tmp); OutputResult?.Invoke(this, CQ); }, cancel.Token); } }); task.Wait(); Trace.WriteLine("After wait"); } catch (OperationCanceledException) { MessageToUser?.Invoke(this, "-----------------Interrupted-----------------"); Trace.WriteLine("-----------------Interrupted-----------------"); } catch (Exception e) when(e is ArgumentException || e is IOException) { Trace.WriteLine(e.Message); }; return(CQ); }