public void OnSelectedArea(object sender, EventArgs e) { IsLoading = true; IsOCRLoading = true; WindowVisibility = Visibility.Visible; _windowsManager.CloseAll(); Task.Run(async() => { var rectangle = (System.Drawing.Rectangle)sender; _screenshot = _machineContext.CaptureScreenFragment(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); var bitmapSource = _imageService.GetBitmapSource(_screenshot); bitmapSource.Freeze(); Dispatcher.CurrentDispatcher.Invoke(() => { CapturedImage = bitmapSource; IsLoading = false; OCRResult.Clear(); }); var ocrResult = await _OCRService.GetText(SelectedOCRLanguage, _screenshot); Dispatcher.CurrentDispatcher.Invoke(() => { OCRResult = ocrResult; IsOCRLoading = false; }); }); }
private void Slot_Click(object sender, EventArgs e) { string[] ary = (sender as Label).Name.Split('_'); if (ary.Length == 3) { string Port = ary[0]; string Slot = ary[2]; Node p = NodeManagement.Get(Port); if (p != null) { Job j; if (p.JobList.TryGetValue(Slot, out j)) { if (j.OCRImgPath.Equals("") && j.OCR_M12_ImgPath.Equals("") && j.OCR_T7_ImgPath.Equals("")) { MessageBox.Show("未找到OCR紀錄"); } else { OCRResult form2 = new OCRResult(j); form2.ShowDialog(); //// open image in default viewer //System.Diagnostics.Process.Start(j.OCRImgPath); } } else { MessageBox.Show("未找到Wafer"); } } } }
/// <summary> /// Applies a bit of logic to strip out extraneous text from the OCR /// data, like State names and invalid characters. /// </summary> /// <param name="result">The extracted text.</param> /// <returns></returns> private static string GetLicensePlateTextFromResult(OCRResult result) { var text = string.Empty; if (result.Regions == null || result.Regions.Length == 0) { return(string.Empty); } const string states = "ALABAMA,ALASKA,ARIZONA,ARKANSAS,CALIFORNIA,COLORADO,CONNECTICUT,DELAWARE,FLORIDA,GEORGIA,HAWAII,IDAHO,ILLINOIS,INDIANA,IOWA,KANSAS,KENTUCKY,LOUISIANA,MAINE,MARYLAND,MASSACHUSETTS,MICHIGAN,MINNESOTA,MISSISSIPPI,MISSOURI,MONTANA,NEBRASKA,NEVADA,NEW HAMPSHIRE,NEW JERSEY,NEW MEXICO,NEW YORK,NORTH CAROLINA,NORTH DAKOTA,OHIO,OKLAHOMA,OREGON,PENNSYLVANIA,RHODE ISLAND,SOUTH CAROLINA,SOUTH DAKOTA,TENNESSEE,TEXAS,UTAH,VERMONT,VIRGINIA,WASHINGTON,WEST VIRGINIA,WISCONSIN,WYOMING"; string[] chars = { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "_", "(", ")", ":", "|", "[", "]" }; var stateList = states.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // We are only interested in the first region found, and only the first two lines within the region. foreach (var line in result.Regions[0].Lines.Take(2)) { // Exclude the state name. if (stateList.Contains(line.Words[0].Text.ToUpper())) { continue; } foreach (var word in line.Words) { if (!string.IsNullOrWhiteSpace(word.Text)) { text += (RemoveSpecialCharacters(word.Text)) + " "; // Spaces are valid in a license plate. } } } return(text.ToUpper().Trim()); }
public override global::System.Data.DataSet Clone() { OCRResult cln = ((OCRResult)(base.Clone())); cln.InitVars(); cln.SchemaSerializationMode = this.SchemaSerializationMode; return(cln); }
public async static Task <object> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = "")] HttpRequestMessage req, [Table("receiptsTable", Connection = "AzureWebJobsStorage")] CloudTable receiptTable, [Queue("receiptitems", Connection = "AzureWebJobsStorage")] ICollector <ReceiptQueueMessage> receiptQueue, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); OCRResult result = JsonConvert.DeserializeObject <OCRResult>(jsonContent); if (result.StatusCode == "Retry") { // there is a possibility the OCR processing failed because the service was unavailable or there was too many // concurrent connections (this is something that can happen with Azure ML web services // therefore, rather than throwing an error, re-add the expense item back to the queue and try to re-process. // queue message to move to next step receiptQueue.Add(new ReceiptQueueMessage { ExpenseId = result.ItemId, Status = "Retry", ProcessingStep = 99 // set to 99 to restart process }); } else { // the process either succeeded or failed in which case update the table with the appropriate data or error text // inform the expense processor (via a queue message) that processing is complete // add row to results table var item = new ReceiptTableItem() { PartitionKey = "key", RowKey = result.ItemId, ExpenseId = result.ItemId, RawText = result.Text, ErrorText = result.ErrorText, ETag = "*" }; var operation = TableOperation.Merge(item); await receiptTable.ExecuteAsync(operation); // queue message to move to next step receiptQueue.Add(new ReceiptQueueMessage { ExpenseId = result.ItemId, Status = result.StatusCode == "Success" ? "Complete" : "Error", ProcessingStep = 1 // set to 1 to complete processing }); } return(req.CreateResponse(HttpStatusCode.OK)); }
public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedDataSetSchema(global::System.Xml.Schema.XmlSchemaSet xs) { OCRResult ds = new OCRResult(); global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType(); global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence(); global::System.Xml.Schema.XmlSchemaAny any = new global::System.Xml.Schema.XmlSchemaAny(); any.Namespace = ds.Namespace; sequence.Items.Add(any); type.Particle = sequence; global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable(); if (xs.Contains(dsSchema.TargetNamespace)) { global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream(); global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream(); try { global::System.Xml.Schema.XmlSchema schema = null; dsSchema.Write(s1); for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext();) { schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current)); s2.SetLength(0); schema.Write(s2); if ((s1.Length == s2.Length)) { s1.Position = 0; s2.Position = 0; for (; ((s1.Position != s1.Length) && (s1.ReadByte() == s2.ReadByte()));) { ; } if ((s1.Position == s1.Length)) { return(type); } } } } finally { if ((s1 != null)) { s1.Close(); } if ((s2 != null)) { s2.Close(); } } } xs.Add(dsSchema); return(type); }
private void BgWorker_DoWork(object sender, DoWorkEventArgs e) { OCRResult r = null; switch (this.MethodName) { case "GeneralBasic": r = OCRUtils_Baidu.Excute_GeneralBasic(e.Argument as byte[]); break; case "General": r = OCRUtils_Baidu.Excute_General(e.Argument as byte[]); break; case "AccurateBasic": r = OCRUtils_Baidu.Excute_AccurateBasic(e.Argument as byte[]); break; case "Accurate": r = OCRUtils_Baidu.Excute_Accurate(e.Argument as byte[]); break; } e.Result = r; }
public async Task<OCRResult> UploadFile() { // Verify that this is an HTML Form file upload request if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType)); } // Create a stream provider for setting up output streams MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder); // Read the MIME multipart asynchronously content using the stream provider we just created. await Request.Content.ReadAsMultipartAsync(streamProvider); Bitmap image = new Bitmap(streamProvider.FileData.Select(e => e.LocalFileName).First()); tessnet2.Tesseract ocr = new tessnet2.Tesseract(); //ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only try { string path = System.Web.HttpContext.Current.Server.MapPath("~/tessdata"); ocr.Init(path, "eng", false); // To use correct tessdata List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty); OCRResult res = new OCRResult(); res.FileNames = streamProvider.FileData.Select(entry => entry.LocalFileName); res.RecognizedTextItems = new List<OCRItem>(); foreach (tessnet2.Word word in result) { (res.RecognizedTextItems as List<OCRItem>).Add( new OCRItem() { Text = word.Text, Confidence = word.Confidence } ); } return res; } catch (Exception e) { throw e; } }
public async Task <OCRResult> UploadFile() { // Verify that this is an HTML Form file upload request if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType)); } // Create a stream provider for setting up output streams MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder); // Read the MIME multipart asynchronously content using the stream provider we just created. await Request.Content.ReadAsMultipartAsync(streamProvider); Bitmap image = new Bitmap(streamProvider.FileData.Select(e => e.LocalFileName).First()); tessnet2.Tesseract ocr = new tessnet2.Tesseract(); //ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only try { string path = System.Web.HttpContext.Current.Server.MapPath("~/tessdata"); ocr.Init(path, "eng", false); // To use correct tessdata List <tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty); OCRResult res = new OCRResult(); res.FileNames = streamProvider.FileData.Select(entry => entry.LocalFileName); res.RecognizedTextItems = new List <OCRItem>(); foreach (tessnet2.Word word in result) { (res.RecognizedTextItems as List <OCRItem>).Add( new OCRItem() { Text = word.Text, Confidence = word.Confidence } ); } return(res); } catch (Exception e) { throw e; } }
public async static Task Run([QueueTrigger("ocrqueue", Connection = "AzureWebJobsStorage")] OCRQueueMessage myQueueItem, int dequeueCount, TraceWriter log) { log.Info($"C# Queue trigger function processed: {myQueueItem}"); OCRResult res = await MakeOCRRequest(myQueueItem.ItemId, myQueueItem.ImageUrl, dequeueCount, log); // perform callback using (var client = new HttpClient()) { var content = new StringContent(JsonConvert.SerializeObject(res), Encoding.UTF8, "application/json"); log.Info($"callback: {myQueueItem.Callback}"); log.Info($"image: {myQueueItem.ImageUrl}"); var result = await client.PostAsync(myQueueItem.Callback, content); log.Info($"result: {result.StatusCode}"); } }
/// <summary> /// Checks if the word can be fixed by applying OCR replacements and return the result /// </summary> /// <param name="word">The word to check</param> /// <param name="ocrPatterns">The available OCR patterns</param> /// <param name="fullDictionary">The dictionary that contains valid words</param> /// <returns>The result of the OCR test</returns> private static OCRResult GetFixedTextFromOCRPattern(string word, Dictionary <string, List <string> > ocrPatterns, HashSet <string> fullDictionary) { var result = new OCRResult(); // todo test more combinations, now only 1 match at a time will be replaced var input = word; // check each pattern in the available OCR patterns foreach (var pattern in ocrPatterns) { // get all the matching characters in the word for the current pattern source var matches = Regex.Matches(input, pattern.Key); foreach (var m in matches.Cast <Match>()) { // check for each pattern target until the word exists in the dictionary foreach (var value in pattern.Value) { // build the new word where the source pattern is replaced with the target pattern var newWord = input.Substring(0, m.Index) + value + input.Substring(m.Index + m.Length); // if the dictionary contains the new word, then the OCR was successful if (fullDictionary.Contains(newWord.ToLower())) { result.IsFixed = true; result.FixedWord = newWord; result.PatternSource = pattern.Key; result.PatternTarget = value; return(result); } } } } result.IsFixed = false; return(result); }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Delete any processed files from a previous run foreach (FileInfo f in new DirectoryInfo(".").GetFiles("*_ocr.pdf")) { f.Delete(); } // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { // ** If nothing is specified then read the first PDF file from the current folder. string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.pdf"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to OCR."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); // ** Specify the various OCR related settings. ProcessingOptions processingOptions = new ProcessingOptions() { // ** Set up array of source files. For OCR we can pass in only a single file at a time. SourceFiles = new SourceFile[] { new SourceFile() { // ** Set the binary content File = File.ReadAllBytes(sourceFileName), // ** Create absolute minimum OpenOptions OpenOptions = new OpenOptions() { OriginalFileName = Path.GetFileName(sourceFileName), FileExtension = Path.GetExtension(sourceFileName), } } }, // ** Define OCR settings OCRSettings = new OCRSettings() { // ** Select OCR engine, change this to 'Muhimbi' to use the default Muhimbi OCR engine. OCREngine = "PrimeOCR", // ** Set language, keep in mind that different OCR engines support different languages (e.g. in the Muhimbi OCR engine, use 'English', not 'English_UK' / 'English_US' Language = OCRLanguage.English_UK.ToString(), // ** Set the desired output, in this case we want the OCRed PDF and OCRed text to be returned separately. OutputType = OCROutputType.Text | OCROutputType.PDF, // ** Include PrimeOCR specific properties. These settings cannot be passed into the Muhimbi default OCR engine. OCREngineSpecificSettings = new OCREngineSpecificSettings_PrimeOCR() { // ** Predefined accuracy levels can be used as well as manually defined integer values. AccuracyLevel = (int)PrimeOCR_AccuracyLevel.Level6, // ** Set various other parameters AutoZone = PrimeOCR_AutoZone.NoAutoZone, Deskew = PrimeOCR_Deskew.On, ImageProcessingOptions = PrimeOCR_ImageProcessingOptions.Autorotate | PrimeOCR_ImageProcessingOptions.Deshade | PrimeOCR_ImageProcessingOptions.Despeck | PrimeOCR_ImageProcessingOptions.Smooth, LexicalChecking = PrimeOCR_LexicalChecking.Lexical, PageQuality = PrimeOCR_PageQuality.NormalQuality, PrintType = PrimeOCR_PrintType.Machine, ZoneContent = PrimeOCR_ZoneContent.NoRestrictions }, } }; // ** Carry out the operation. Console.WriteLine("Processing file " + sourceFileName + "."); BatchResults results = client.ProcessBatch(processingOptions); // ** Get results. Both textual and PDF OCRResult ocredText = results.Results[0].OCRResult; byte[] ocredPdfFile = results.Results[0].File; // ** Process textual output if (ocredText != null) { // ** Write the text into a txt file string destFileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_ocr.txt"; File.WriteAllText(destFileName, ocredText.Text); Console.WriteLine("Text file written to " + destFileName); // ** Show the file Console.WriteLine("Launching text file in reader"); Process.Start(destFileName); } else { Console.WriteLine("No text file was produced."); } // ** Process the resulting PDF if (ocredPdfFile != null) { // ** Write the processed file back to the file system with a PDF extension. string destFileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_ocr.pdf"; File.WriteAllBytes(destFileName, ocredPdfFile); Console.WriteLine("PDF file written to " + destFileName); // ** Open the generated PDF file in a PDF Reader Console.WriteLine("Launching file in PDF Reader"); Process.Start(destFileName); } else { Console.WriteLine("No PDF file was generated."); } } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } Console.ReadKey(); }
private void OCR02_Pic_DoubleClick(object sender, EventArgs e) { OCRResult form2 = new OCRResult((sender as PictureBox).Tag as Job); form2.ShowDialog(); }
/// <summary> /// Checks if there are OCR errors in the word and suggest a fix if there are /// </summary> /// <param name="we">The word to check</param> /// <param name="ocrPatterns">The available OCR patterns</param> /// <param name="fullDictionary">The dictionary that contains valid words</param> /// <param name="result">The result of the OCR test</param> public static void Test(WordEntry we, Dictionary <string, List <string> > ocrPatterns, HashSet <string> fullDictionary, out OCRResult result) { if (we.IsUnknownWord && !we.Ignore && string.IsNullOrEmpty(we.Suggestion)) { // get the result result = GetFixedTextFromOCRPattern(we.Text, ocrPatterns, fullDictionary); if (result.IsFixed) { we.Suggestion = result.FixedWord; we.UnknownType = "OCR"; } } else { result = null; } }
protected override void OnSuccess(CodeActivityContext context, OCRResult result) { CustomOutput.Set(context, $"Custom output: '{result.Text}' has {result.Words.Length} words."); }
public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedTableSchema(global::System.Xml.Schema.XmlSchemaSet xs) { global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType(); global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence(); OCRResult ds = new OCRResult(); global::System.Xml.Schema.XmlSchemaAny any1 = new global::System.Xml.Schema.XmlSchemaAny(); any1.Namespace = "http://www.w3.org/2001/XMLSchema"; any1.MinOccurs = new decimal(0); any1.MaxOccurs = decimal.MaxValue; any1.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax; sequence.Items.Add(any1); global::System.Xml.Schema.XmlSchemaAny any2 = new global::System.Xml.Schema.XmlSchemaAny(); any2.Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1"; any2.MinOccurs = new decimal(1); any2.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax; sequence.Items.Add(any2); global::System.Xml.Schema.XmlSchemaAttribute attribute1 = new global::System.Xml.Schema.XmlSchemaAttribute(); attribute1.Name = "namespace"; attribute1.FixedValue = ds.Namespace; type.Attributes.Add(attribute1); global::System.Xml.Schema.XmlSchemaAttribute attribute2 = new global::System.Xml.Schema.XmlSchemaAttribute(); attribute2.Name = "tableTypeName"; attribute2.FixedValue = "OCR_ResultDataTable"; type.Attributes.Add(attribute2); type.Particle = sequence; global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable(); if (xs.Contains(dsSchema.TargetNamespace)) { global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream(); global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream(); try { global::System.Xml.Schema.XmlSchema schema = null; dsSchema.Write(s1); for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext();) { schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current)); s2.SetLength(0); schema.Write(s2); if ((s1.Length == s2.Length)) { s1.Position = 0; s2.Position = 0; for (; ((s1.Position != s1.Length) && (s1.ReadByte() == s2.ReadByte()));) { ; } if ((s1.Position == s1.Length)) { return(type); } } } } finally { if ((s1 != null)) { s1.Close(); } if ((s2 != null)) { s2.Close(); } } } xs.Add(dsSchema); return(type); }
public OCRResult ExtractText(OCROption options) { using (var ocr = GdPictureHelper.GetOCRInstance()) { try { var result = new OCRResult(); var contentBuilder = new StringBuilder(); ocr.SetImage(imageId); ApplySettings(ocr, options ?? DefaultOptions); var pages = new List <int>(); if (options.Pages.Count == 0) { for (int i = 1; i <= PageCount; i++) { pages.Add(i); } } else { pages.AddRange(options.Pages); } foreach (var page in pages) { GdPictureStatus status; if (isMultipageImage) { status = imaging.TiffSelectPage(imageId, page); } else { status = imaging.SelectPage(imageId, page); } if (status != GdPictureStatus.OK) { result.ErrorOccured = true; result.ErrorMessages.Add($"Error during page selection. Page: {page}"); continue; } if (ocr.SetImage(imageId) != GdPictureStatus.OK) { result.ErrorOccured = true; result.ErrorMessages.Add($"Error during setting image. Page: {page}"); continue; } var resultId = ocr.RunOCR(); string text = ocr.GetOCRResultText(resultId); // Add result var regionResult = new OCRRegionResult { OptionName = options.OptionName, Height = options.Height, Left = options.Left, Page = page, Top = options.Top, Width = options.Width, Text = text }; result.RegionResults.Add(regionResult); contentBuilder.AppendLine(text); } result.Text = contentBuilder.ToString(); ocr.ReleaseOCRResults(); return(result); } catch (Exception ex) { ocr.ReleaseOCRResults(); return(new OCRResult { Text = "", ErrorMessages = new[] { ex.Message }, ErrorOccured = true }); } } }
static async Task <OCRResult> MakeOCRRequest(string itemId, string imageFilePath, int dequeueCount, TraceWriter log) { OCRResult res = new OCRResult(); try { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConfigurationManager.AppSettings["CogServicesKey"]); string uri = ConfigurationManager.AppSettings["CogServicesAPI"]; HttpResponseMessage response; // convert image into a byte array to pass as a parameter to the OCR API byte[] byteData = await GetImageAsByteArray(imageFilePath); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); if (response.IsSuccessStatusCode) { var contents = await response.Content.ReadAsStringAsync(); log.Info($"Raw receipt text is {contents}"); string text = ParseData(contents); log.Info($"Parsed receipt text is {text}"); res.Text = text; res.StatusCode = "Success"; res.ItemId = itemId; } else if (response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) { // The ML webservice has constraints on the number of concurrent calls that can be made against the service. // If the max number of calls are made (currently 20) then the service returns a status of ServiceUnavailable. // By default the function will retry 5 times if there is a failure executing, once that retry limit has been met // the queue message is automatically added to a poison queue. We want to intervene at this point. Rather than add it to the // poison queue we want to retry the process again, but we will let the capture services govern how that retry is done if (dequeueCount < 4) { throw new RetryException(); } else { throw new MaxRetryException(); } } else { log.Info(string.Format("The request failed with status code: {0}", response.StatusCode)); string responseContent = await response.Content.ReadAsStringAsync(); log.Info($"content: {responseContent}"); throw new Exception("Error calling OCR API : " + responseContent); } } } } catch (RetryException) { // by throwing an exception, the queue message is retried throw; } catch (MaxRetryException) { // maximum number of retries achieved, let the capture service handle what to do next res.StatusCode = "Retry"; res.ItemId = itemId; } catch (Exception ex) { // a catastophic error has occurred, let the capture app fail gracefully res.StatusCode = "Error"; res.ErrorText = ex.Message; res.ItemId = itemId; } return(res); }