private void Bgw_DoWork() { if (CheckConfiguration()) { string ArchiveDirectory = Configuration.ArchivPfad + "\\" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss"); LogFile = DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + "_log.txt"; Log("Zielverzeichnis: " + Configuration.ZielPfad); Log("Archivverzeichnis: " + ArchiveDirectory); Log("Logverzeichnis: " + Configuration.LogPfad); coreInfo = LoginPP(PictureparkService); if (coreInfo == null) { return; } AssetFields = PictureparkService.GetAssetFields(coreInfo); //create temp directory for images or wipe content if already exists if (!CreateTempDirectory()) { return; } List <string> alreadyCheckedAssets = new List <string>(); //get already completed assets from Completed.csv if (!GetAlreadyCheckedAssets(out alreadyCheckedAssets)) { return; } //get all aproved assets in given timespan if (!GetReviewedAssets(alreadyCheckedAssets)) { return; } //compare and synchronise images int failures = CompareAndSyncImages(); MessageBox.Show("Abgleich abgeschlossen, " + failures + " fehlgeschlagen, mehr Informationen in der Logdatei"); Log("Abgleich abgeschlossen, " + failures + " Bilder konnten nicht synchronisiert werden"); //save last sync to config file Configuration.LetzterAbgleich = DateTime.Now; string strConfig = JsonConvert.SerializeObject(Configuration, Formatting.Indented); File.WriteAllText(ConfigPath, strConfig); } }
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; } } }