コード例 #1
0
        /*
         * public static string FixTag( string tagText, TagCasing casingFormat, out List<char> rejectedChars)
         * {
         *  var result = FilterChars(tagText, out rejectedChars);
         *  result = ChangeToSnakeCasing(result);
         *  return result;
         * }
         */
        public static void FixAllTagsInFiles(ImageFiles imageFiles)//, TagCasing casingFormat = TagCasing.SnakeCase)
        {
            var cancel        = false;
            var cancelContext = new CancelDialogDataContext();
            var cancelDialog  = new CancelDialog();

            cancelContext.CurrentValue  = 0;
            cancelContext.MaxValue      = imageFiles.Count;
            cancelContext.PerformAction = () =>
            {
                foreach (var filePath in imageFiles)
                {
                    if (cancel)
                    {
                        break;
                    }
                    var cleaned = ImageFileUtil.GetImageTags(filePath);
                    ImageFileUtil.ApplyTagsToImage(filePath, cleaned);
                    cancelContext.CurrentValue++;
                }
            };
            cancelContext.OnCancel = (s, e) => { cancel = true; cancelDialog.Close(); };
            cancelContext.OnClosed = (s, e) => { cancel = true; cancelDialog.Close(); };
            cancelDialog.SetContext(cancelContext);
            cancelDialog.ShowDialog();
        }
コード例 #2
0
        }                                                                                            //, "jpeg", "gif", "png", };

        public static List <string> GetImageFilenames(string sourcePath = null, TagQueryCriteria tagQueryCriteria = null)
        {
            sourcePath = sourcePath ?? PersistanceUtil.RetreiveSetting(Setting.SourceDirectory);
            if (!Directory.Exists(sourcePath))
            {
                sourcePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            }
            tagQueryCriteria = tagQueryCriteria ?? new TagQueryCriteria();

            var result = new List <string>();
            var query  = tagQueryCriteria.GetQueryClause(sourcePath, out bool randomize);

            var windowsSearchConnection = @"Provider=Search.CollatorDSO.1;Extended Properties=""Application=Windows""";

            using (OleDbConnection connection = new OleDbConnection(windowsSearchConnection))
            {
                connection.Open();
                var command = new OleDbCommand(query, connection);
                try
                {
                    using (var r = command.ExecuteReader())
                    {
                        var cont     = true;
                        var context  = new CancelDialogDataContext();
                        var dialogue = new CancelDialog(context);
                        context.OnCancel = (n, m) => cont = false;
                        context.OnClosed = (n, m) => context.OnCancel(n, null);

                        dialogue.Show();
                        while (cont)
                        {
                            try
                            {
                                cont = r.Read();
                                var filepath = $"{r[0]}";

                                if (IsJpg(filepath))
                                {
                                    result.Add(filepath);
                                }
                            }
                            catch (Exception e) { Debug.WriteLine(e); }
                            finally { dialogue.Close(); }
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    var dialogue = new CancelDialog();
                    var context  = new CancelDialogDataContext();
                    int i;
                    var files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
                    context.MaxValue      = files.Length;
                    context.PerformAction = () =>
                    {
                        for (i = 0; i < files.Length; i++)
                        {
                            var fileName = files[i];
                            if (IsJpg(fileName) && MatchesCriteria(fileName, tagQueryCriteria))
                            {
                                result.Add(fileName);
                            }

                            context.CurrentValue++;
                        }
                    };
                    context.OnCancel = (n, m) => { i = files.Length; dialogue.Close(); };
                    context.OnClosed = (n, m) => context.OnCancel(n, null);

                    dialogue.SetContext(context);
                    dialogue.ShowDialog();
                }
                finally
                {
                    connection.Close();
                }
            }
            if (randomize)
            {
                result.Shuffle();
            }

            return(result);
        }
コード例 #3
0
        public static System.Collections.Async.IAsyncEnumerable <Dictionary <string, List <TagSuggestion> > > RequestBatchAnalysis(IEnumerable <string> imageFilePaths, bool skipAutoTagged = true)
        {
            return(new AsyncEnumerable <Dictionary <string, List <TagSuggestion> > >(async yield =>
            {
                var result = new Dictionary <string, List <TagSuggestion> >();
                if (IsPerformingBatchOperation)
                {
                    MessageBox.Show("only one batch operation may be performed at a time!");
                    yield.Break();
                }

                IsPerformingBatchOperation = true;
                var tokenSource = new CancellationTokenSource();
                var token = tokenSource.Token;
                var cancelContext = new CancelDialogDataContext()
                {
                    MaxValue = imageFilePaths.Count(),
                    OnCancel = (s, e) => { tokenSource.Cancel(); },
                    OnClosed = (s, e) => { tokenSource.Cancel(); }
                };
                CancelDialog cancelWindow = null;
                await App.Current.Dispatcher.InvokeAsync(() =>
                {
                    cancelWindow = new CancelDialog(cancelContext);
                    cancelWindow.Show();
                });
                var splitPaths = new List <List <string> >();
                splitPaths.Add(new List <string>());
                long maxfilesize = 9000000;//9mb
                long splitSize = 0;
                foreach (var imageFilePath in imageFilePaths)
                {
                    var i = splitPaths.Count - 1;
                    var thisFileSize = new FileInfo(imageFilePath).Length;
                    if (thisFileSize > maxfilesize)
                    {
                        continue;
                    }
                    if ((splitPaths[i].Count >= 8) || (splitSize + thisFileSize > maxfilesize))
                    {
                        splitPaths.Add(new List <string>());
                        splitSize = 0;
                        i++;
                    }

                    if (skipAutoTagged && ImageFileUtil.GetImageTags(imageFilePath).Any(t => t.TagName.ToLower().Contains("auto") && t.TagName.ToLower().Contains("tagged")))
                    {
                        App.Current.Dispatcher.Invoke(() => cancelContext.CurrentValue++);
                        continue;
                    }
                    splitSize += thisFileSize;
                    splitPaths[i].Add(imageFilePath);
                }
                foreach (var paths in splitPaths)
                {
                    try
                    {
                        result = VisionAPISuggestions.VisionApi.RequestBatchVisionAnalysis(paths);

                        var input = paths.Select((path) => new ClarifaiFileImage(File.ReadAllBytes(path), path));
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }
                        var res = await clarifaiClient.WorkflowPredict("workflow", input).ExecuteAsync();
                        if (res.IsSuccessful)
                        {
                            foreach (var workflow in res.Get().WorkflowResults)
                            {
                                var imageFilePath = workflow.Input.ID;
                                var suggestions = ParsePredictions(imageFilePath, workflow.Predictions);
                                if (result.ContainsKey(imageFilePath))
                                {
                                    result[imageFilePath].AddRange(suggestions);
                                }
                                else
                                {
                                    result.Add(imageFilePath, suggestions);
                                }
                            }
                        }
                        else
                        {
                            Debug.WriteLine("batch clarifai analysis was unsuccessful");
                        }
                        foreach (var path in paths)
                        {
                            if (result.ContainsKey(path))
                            {
                                result[path].Add(new TagSuggestion(new ImageTag("autoTagged"), 1, "general"));
                            }
                            else
                            {
                                result.Add(path, new List <TagSuggestion>(new TagSuggestion[] { new TagSuggestion(new ImageTag("autoTagged"), 1, "general") }));
                            }

                            result[path].AddRange(ImageFileUtil.GetImageTags(path).Select(tag => new TagSuggestion(tag, 1, "preexisting")));
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("error thrown in batch conversion\n\n" + e);
                    }
                    finally
                    {
                        await yield.ReturnAsync(result);
                    }
                    Thread.Sleep(1000);//can only make a call every 1 sec
                    App.Current.Dispatcher.Invoke(() => cancelContext.CurrentValue += paths.Count);
                }
                App.Current.Dispatcher.Invoke(() => cancelWindow.Close());
                IsPerformingBatchOperation = false;
            }));
        }