// New version public void ProgramStart(List <StringPathAndImage> strings) { cts = new CancellationTokenSource(); List <string> filePaths = new List <string>(); List <string> imageList = new List <string>(); foreach (var item in strings) { filePaths.Add(item.Path); imageList.Add(item.Image); } var tasks = new Task[filePaths.Count()]; for (int i = 0; i <= filePaths.Count() - 1; i++) { tasks[i] = Task.Factory.StartNew(pi => { int idx = (int)pi; ImageObject image; lock (LockObject) { image = ModelContext.DatabaseCheck(filePaths[idx]); } if (image == null) { if (!cts.Token.IsCancellationRequested) { ObjectInImageProbability result = ImageRecognition(filePaths[idx], imageList[idx]); this.ResultEvent?.Invoke(this, result); lock (LockObject) { ModelContext.DatabaseAdding(result); } } } else { if (!cts.Token.IsCancellationRequested) { ObjectInImageProbability result = new ObjectInImageProbability(filePaths[idx], image.ClassLabel, image.Probability); this.ResultEvent?.Invoke(this, result); } } }, i); } Task.WaitAll(tasks); }
// Getting all the images from existing or default directory and recognition of objects using TPL public void ProgramStart(string path) { cts = new CancellationTokenSource(); // Getting all the .jpg pictures from directory string[] filePaths = Directory.GetFiles(@path, "*.jpg"); // Data processing with TPL [tasks] var tasks = new Task[filePaths.Count()]; for (int i = 0; i <= filePaths.Count() - 1; i++) { tasks[i] = Task.Factory.StartNew(pi => { int idx = (int)pi; ImageObject image; lock (LockObject) { image = ModelContext.DatabaseCheck(filePaths[idx]); } if (image == null) { if (!cts.Token.IsCancellationRequested) { ObjectInImageProbability result = ImageRecognition(filePaths[idx]); this.ResultEvent?.Invoke(this, result); lock (LockObject) { ModelContext.DatabaseAdding(result); } } } else { if (!cts.Token.IsCancellationRequested) { ObjectInImageProbability result = new ObjectInImageProbability(filePaths[idx], image.ClassLabel, image.Probability); this.ResultEvent?.Invoke(this, result); } } }, i); } Task.WaitAll(tasks); }
public void DatabaseAdding(ObjectInImageProbability image) { ImageInformation AddedImage = new ImageInformation { Path = image.Path, ImageContext = new Blob() { ImageContext = File.ReadAllBytes(image.Path) }, Probability = image.Probability }; var query = ClassLabels.Include(item => item.ImagesInformation).Where(item => item.StringClassLabel == image.ClassLabel); if (query.Count() == 0) { AddedImage.ClassLabel = new ClassLabel() { StringClassLabel = image.ClassLabel, ClassLabelImagesNumber = 1 }; AddedImage.ClassLabel.ImagesInformation = new List <ImageInformation> { AddedImage }; ImagesInformation.Add(AddedImage); ClassLabels.Add(AddedImage.ClassLabel); ImageContext.Add(AddedImage.ImageContext); } else { AddedImage.ClassLabel = query.First(); AddedImage.ClassLabel.ImagesInformation.Add(AddedImage); AddedImage.ClassLabel.ClassLabelImagesNumber++; ImagesInformation.Add(AddedImage); ImageContext.Add(AddedImage.ImageContext); } SaveChanges(); }