private static AzureOcrEngine GetAzureOcrEngine() { Console.WriteLine("To OCR process an image with Azure Vision API you need a azure account and access to their vision API."); Console.WriteLine("Input your Azure Vision API subscription key:"); string subscriptionKey = null; while (string.IsNullOrWhiteSpace(subscriptionKey)) { subscriptionKey = Console.ReadLine(); } Console.WriteLine("Input your Azure endpoint:"); string endpoint = null; while (string.IsNullOrWhiteSpace(endpoint)) { endpoint = Console.ReadLine(); } var ocr = AzureOcrEngine.Build(new AzureVisionConfigurations(subscriptionKey, endpoint)); Console.WriteLine("Azure ocr engine instantiated."); Console.WriteLine(); return(ocr); }
static void Main(string[] args) { Console.WriteLine("###### OCR Wrapper demo project ######"); GoogleOcrEngine googleOcrEngine = null; AzureOcrEngine azureOcrEngine = null; string visionApiChoice = null; while (true) { Console.WriteLine("Input file path:"); var userChoice = Console.ReadLine(); if (userChoice == null || userChoice.ToLower().Equals("q")) { return; } var imageFormat = GetImageFormat(userChoice); if (imageFormat == FileFormatEnum.Unsupported) { Console.WriteLine("Unsupported file format. Supported types are; jpg, jpeg, png and pdf."); continue; } if (imageFormat == FileFormatEnum.Pdf) { PerformAction(DoPdfExtraction(userChoice)).ContinueWith(t => Pause()).Wait(); } else { if (visionApiChoice == null) { Console.WriteLine("What vision API do you wish to use, Google or Azure? \nOr you can use an installation of Tranym ocr if you have it installed on this machine. \n(Options: azure, google, transym)."); while (string.IsNullOrWhiteSpace(visionApiChoice) || (visionApiChoice != "google" && visionApiChoice != "azure" && visionApiChoice != "transym")) { visionApiChoice = Console.ReadLine()?.ToLower(); } } if (visionApiChoice == "google") { if (googleOcrEngine == null) { googleOcrEngine = GetGoogleOcrEngine(); } PerformAction(DoGoogleOcr(googleOcrEngine, imageFormat, userChoice)).ContinueWith(t => Pause()).Wait(); } else if (visionApiChoice == "azure") { if (azureOcrEngine == null) { azureOcrEngine = GetAzureOcrEngine(); } PerformAction(DoAzureOcr(azureOcrEngine, imageFormat, userChoice)).ContinueWith(t => Pause()).Wait(); } else if (visionApiChoice == "transym") { Console.WriteLine("Do you have a licensed Transym installation on this machine? (y/n)\nOtherwise the ocr will fail."); var isTransymInstalled = Console.ReadLine()?.ToLower(); if (isTransymInstalled == "y") { PerformAction(DoTransymOcr(userChoice)).ContinueWith(t => Pause()).Wait(); } } } } }
private static async Task <OcrResult> DoAzureOcr(AzureOcrEngine azureOcrEngine, FileFormatEnum fileFormat, string file) { return(await azureOcrEngine.OcrImage(file, fileFormat)); }