Example #1
0
        static void Main(string[] args)
        {
            SiftComparison(@"C:\temp\2.jpg", @"C:\temp\1.jpg");

            try
            {
                if (ConfigurationManager.AppSettings["address"] != "")
                {
                    var proxy = new WebProxy(ConfigurationManager.AppSettings["address"], true);
                    proxy.Credentials          = new NetworkCredential(ConfigurationManager.AppSettings["user"], ConfigurationManager.AppSettings["password"]);
                    WebRequest.DefaultWebProxy = proxy;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: Please check proxy settings in KMB_ImageComparison.exe.config");
                Console.ReadLine();
                Environment.Exit(0);
            }

            PictureparkService.InnerChannel.OperationTimeout = new TimeSpan(0, 20, 0);

            while (true)
            {
                Console.WriteLine("Choose option: ");
                Console.WriteLine("1: Run comparison");
                Console.WriteLine("2: Generate configuration-file");

                switch (Console.ReadLine())
                {
                //run comparison
                case "1":
                    #region run comparison

                    LogFile = DateTime.Now.ToString("yyyyMMdd_hhmmss") + "_ImageComparison_Log.txt";
                    if (CheckConfiguration())
                    {
                        Log("Started comparison");

                        Log("Config file ok..");

                        //get asset fields
                        AssetFields = PictureparkService.GetAssetFields(coreInfo);

                        Log("Creating folder structure if not already existing");

                        //create temp folder if not already exists
                        string tempPath = Configuration.ResultPath + "\\temp";
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        }

                        //higher PHASH score than 65 or SIFT matching above 100
                        string matchingFolder = Configuration.ResultPath + "\\01_matching";
                        if (!Directory.Exists(matchingFolder))
                        {
                            Directory.CreateDirectory(matchingFolder);
                        }
                        int matchingCount = 0;

                        //neither PHASH score above 65 or SIFT matching above 100
                        string noMatchingFolder = Configuration.ResultPath + "\\02_not_matching";
                        if (!Directory.Exists(noMatchingFolder))
                        {
                            Directory.CreateDirectory(noMatchingFolder);
                        }
                        int notMatchingCount = 0;

                        //create missing images folder if not already exists
                        string missingImgPath = Configuration.ResultPath + "\\03_missing_on_pp";
                        if (!Directory.Exists(missingImgPath))
                        {
                            Directory.CreateDirectory(missingImgPath);
                        }
                        int missingImgCount = 0;

                        //create multiple standard images folder if not already exists
                        string multipleStandardImgPath = Configuration.ResultPath + "\\04_multiple_w11_in_pp";
                        if (!Directory.Exists(multipleStandardImgPath))
                        {
                            Directory.CreateDirectory(multipleStandardImgPath);
                        }
                        int multipleW11Count = 0;

                        //create missing images folder if not already exists
                        string invalidAssetPath = Configuration.ResultPath + "\\05_invalid_asset_on_pp";
                        if (!Directory.Exists(invalidAssetPath))
                        {
                            Directory.CreateDirectory(invalidAssetPath);
                        }
                        int invalidAssetOnPPCount = 0;

                        int siftMatchingCount = 0;

                        //loop images in ImageFolder
                        DirectoryInfo imgDir = new DirectoryInfo(Configuration.ImagePath);

                        using (var client = new WebClient())
                        {
                            foreach (FileInfo fileToCheck in imgDir.GetFiles())
                            {
                                coreInfo = LoginPP(PictureparkService);

                                Log("Checking file: " + fileToCheck.Name);

                                //only check jpg files, ignore others
                                if (fileToCheck.Extension.ToLower() == ".jpg")
                                {
                                    string strObjId = fileToCheck.Name.Replace(fileToCheck.Extension, "");
                                    int    objId;
                                    if (int.TryParse(strObjId, out objId))
                                    {
                                        List <ComparisonOperation> comparisonOperations = new List <ComparisonOperation>();
                                        StringEqualOperation       stringEqualOperation = new StringEqualOperation()
                                        {
                                            FieldName = "OBJID", EqualString = objId.ToString()
                                        };
                                        comparisonOperations.Add(stringEqualOperation);
                                        AndOperation searchOperations = new AndOperation()
                                        {
                                            ComparisonOperations = comparisonOperations.ToArray()
                                        };

                                        ExtendedAssetFilter filter = new ExtendedAssetFilter()
                                        {
                                            AdditionalSelectFields = new string[] { "AssetName", "OBJID", "KeinDownloadOnline", "MP_Copyright" },
                                            SearchOperation        = searchOperations
                                        };

                                        AssetItemCollection collection = PictureparkService.GetAssets(coreInfo, filter);

                                        List <AssetItem> assets = collection.Assets
                                                                  .Where(a => a.FieldValues
                                                                         .Where(fv => fv.FieldId == GetFieldIdByName(AssetFields.ToList(), "AssetName"))
                                                                         .Any(fv => fv.StringValue.Substring(1, 3).ToLower() == "w11")).ToList();

                                        if (assets.Count() > 1)
                                        {
                                            //more than one w11 exists for this objId
                                            Log("More than one w11 exists for this objId");
                                            File.Move(fileToCheck.FullName, multipleStandardImgPath + "\\" + fileToCheck.Name);
                                            multipleW11Count++;
                                        }
                                        else if (assets.Count() == 1)
                                        {
                                            AssetItem asset = assets[0];

                                            List <AssetSelection> assetSelection = new List <AssetSelection>()
                                            {
                                                new AssetSelection()
                                                {
                                                    AssetId = asset.AssetId,
                                                    DerivativeDefinitionId = 7
                                                }
                                            };

                                            DownloadOptions downloadOptions = new DownloadOptions()
                                            {
                                                UserAction = UserAction.DerivativeDownload
                                            };

                                            Download download = new Download();

                                            try
                                            {
                                                download = PictureparkService.Download(coreInfo, assetSelection.ToArray(), downloadOptions);
                                            }
                                            catch (Exception ex)
                                            {
                                                File.Move(fileToCheck.FullName, invalidAssetPath + "\\" + fileToCheck.Name);
                                                Log("Problem with asset on picturepark");
                                                invalidAssetOnPPCount++;
                                                continue;
                                            }

                                            //download asset into temp directory
                                            string downloadFilePath = tempPath + "\\" + download.DownloadFileName;
                                            client.DownloadFile(download.URL, downloadFilePath);

                                            double phashScore = CompareImages(fileToCheck.FullName, downloadFilePath);

                                            Log("PHASH-Score: " + phashScore);

                                            int siftScore = 0;

                                            // PHASH does not match, check if SIFT finds matches
                                            if (phashScore < 0.65)
                                            {
                                                Log("PHASH-Score too low, checking SIFT Algorithm");
                                                siftScore = SiftComparison(fileToCheck.FullName, downloadFilePath);
                                                Log("SIFT-Score: " + siftScore);

                                                if (siftScore > 100)
                                                {
                                                    siftMatchingCount++;
                                                }
                                            }

                                            string folder;

                                            if (phashScore > 0.65 || siftScore > 100)
                                            {
                                                Log("Images match!");
                                                folder = matchingFolder;
                                                matchingCount++;
                                            }
                                            else
                                            {
                                                Log("Images do not match, lower PHASH than 0.65 and lower SIFT match than 100!");
                                                folder = noMatchingFolder;
                                                notMatchingCount++;
                                            }

                                            string fullObjId = "0000000".Substring(0, 7 - objId.ToString().Length) + objId.ToString();

                                            //move pair to folder
                                            File.Move(fileToCheck.FullName, folder + "\\" + fullObjId + "_mp.jpg");
                                            File.Move(downloadFilePath, folder + "\\" + fullObjId + "_pp.jpg");
                                        }
                                        else
                                        {
                                            //no asset found with this obj-id
                                            Log("No asset found for ObjId: " + objId.ToString());
                                            File.Move(fileToCheck.FullName, missingImgPath + "\\" + fileToCheck.Name);
                                            missingImgCount++;
                                        }
                                    }
                                    else
                                    {
                                        Log("Filename is not an obj-id, skipping");
                                    }
                                }
                                else
                                {
                                    Log("No jpg, skipping..");
                                }
                            }

                            Log("-------------------------");
                            Log("Image comparison finished");
                            Log("-------------------------");
                            Log("Total matches: " + matchingCount);
                            Log("Matches with PHASH: " + matchingCount + siftMatchingCount);
                            Log("Matches with SIFT: " + siftMatchingCount);
                            Log("-------------------------");
                            Log("Not matching: " + notMatchingCount);
                            Log("-------------------------");
                            Log("Missing assets in Picturepark: " + missingImgCount);
                            Log("-------------------------");
                            Log("Multiple W11 in Picturepark: " + multipleW11Count);
                            Log("-------------------------");
                            Log("Invalid assets in Picturepark: " + invalidAssetOnPPCount);
                            Log("-------------------------");
                        }
                    }
                    else
                    {
                        Log("Config file invalid, aborting");
                    }
                    #endregion
                    break;

                //generate config file
                case "2":
                    #region generate config file
                    if (File.Exists("Configuration.txt"))
                    {
                        Console.WriteLine("Configuration file already exists, overwrite with new one? (y)");
                        if (Console.ReadKey().Key.ToString().ToLower() != "y")
                        {
                            Console.WriteLine("Cancel..");
                            continue;
                        }
                    }

                    Configuration config = new Configuration()
                    {
                        ImagePath     = "PATH_TO_IMAGE_FOLDER",
                        ResultPath    = "PATH_TO_RESULTS_FOLDER",
                        LogPath       = "PATH_TO_LOG_FOLDER",
                        PP_CustomerId = 0,
                        PP_ClientGUID = "PP_CLIENT_GUID",
                        PP_Email      = "PP_EMAIL",
                        PP_Password   = "******"
                    };

                    File.WriteAllText("Configuration.txt", JsonConvert.SerializeObject(config, Formatting.Indented));

                    FileInfo fi = new FileInfo("Configuration.txt");
                    Console.WriteLine("Configuration file generated: " + fi.FullName);
                    #endregion
                    break;

                default:
                    Console.WriteLine("Invalid input..");
                    break;
                }
            }
        }
Example #2
0
        private static bool GetReviewedAssets(List <string> alreadyCompared)
        {
            ////DEBUG
            //List<ComparisonOperation> orComparisonOperations = new List<ComparisonOperation>();
            //foreach (string tmp in tempcheck)
            //{
            //    StringEqualOperation asd = new StringEqualOperation() { FieldName = "OBJID", EqualString = "39894" };
            //  StringEqualOperation asd = new StringEqualOperation() { FieldName = "AssetName", EqualString = tmp };
            //    orComparisonOperations.Add(asd);
            //}
            //OrOperation orOperations = new OrOperation() { ComparisonOperations = orComparisonOperations.ToArray() };
            ////DEBUG


            List <ComparisonOperation> andComparisonOperations = new List <ComparisonOperation>();

            foreach (string ignoreAssets in alreadyCompared)
            {
                StringNotEqualOperation notEqualOperator = new StringNotEqualOperation()
                {
                    FieldName = "AssetName", NotEqualString = ignoreAssets
                };
                andComparisonOperations.Add(notEqualOperator);
            }

            StringEqualOperation contentApprovedFlag = new StringEqualOperation()
            {
                EqualString = "True", FieldName = "InhaltlichKontrolliert"
            };

            andComparisonOperations.Add(contentApprovedFlag);

            AndOperation andOperations = new AndOperation()
            {
                ComparisonOperations = andComparisonOperations.ToArray()
            };

            List <LogicalOperation> logicalOperations = new List <LogicalOperation>();

            logicalOperations.Add(andOperations);
            //logicalOperations.Add(orOperations);
            AndOperation searchOperations = new AndOperation()
            {
                LogicalOperations = logicalOperations.ToArray()
            };

            ExtendedAssetFilter filter = new ExtendedAssetFilter()
            {
                AdditionalSelectFields = new string[] { "AssetName", "OBJID", "FreigabeDatum", "InhaltlichKontrolliert" },
                SearchOperation        = searchOperations
            };

            AssetItemCollection collection = PictureparkService.GetAssets(coreInfo, filter);

            //get preview assets
            List <AssetItem> assets = collection.Assets
                                      .Where(a => a.FieldValues
                                             .Where(fv => fv.FieldId == GetFieldIdByName(AssetFields.ToList(), "AssetName"))
                                             .Any(fv => fv.StringValue.Substring(1, 3).ToLower() == "w11")).ToList();

            Log(assets.Count + " noch nicht geprüfte Werke");

            if (assets.Count != 0)
            {
                if (MessageBox.Show(assets.Count + " noch nicht geprüfte Werke vorhanden. Diese jetzt herunterladen und abgleichen?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    //create new archive directory
                    CreateArchiveDirectory();

                    using (var client = new WebClient())
                    {
                        foreach (AssetItem asset in assets)
                        {
                            List <AssetSelection> assetSelection = new List <AssetSelection>()
                            {
                                new AssetSelection()
                                {
                                    AssetId = asset.AssetId,
                                    DerivativeDefinitionId = 7     //todo: check which derivative id to pass here
                                }
                            };

                            //List<ExtendedDerivative> availableDerivatives = PictureparkService.GetDerivatives(coreInfo, assetSelection.ToArray()).ToList();

                            DownloadOptions downloadOptions = new DownloadOptions()
                            {
                                UserAction = UserAction.DerivativeDownload
                            };

                            Download download = PictureparkService.Download(coreInfo, assetSelection.ToArray(), downloadOptions);

                            //download asset into temp directory
                            client.DownloadFile(download.URL, TempFolderPath + "\\" + download.DownloadFileName);

                            Log(download.DownloadFileName + " in temporäres Verzeichnis heruntergeladen");
                        }

                        // continue with local comparison
                        return(true);
                    }
                }
                else
                {
                    Log("Abgleich abgebrochen durch Benutzer");
                }
            }
            else
            {
                MessageBox.Show("Es gibt keine neu geprüften Werke");
                Log("Es gibt keine neu geprüften Werke");
            }

            return(false);
        }