public object Any(OCRRequest request) { OCRResponse myResponse = new OCRResponse(); if (request.Base64UploadFile == null || string.IsNullOrWhiteSpace(request.Base64UploadFile.ToString())) { myResponse.jsonResponse = "There was no file attached or no file name was specified."; return(myResponse); } try { // get the image data and save to local storage string base64ImgStr = request.Base64UploadFile.ToString().Split(',')[1]; byte[] tmpBytes = Convert.FromBase64String(base64ImgStr); Image image = (Bitmap)((new ImageConverter()).ConvertFrom(tmpBytes)); // now do the OCR junk here var ocr = new Tesseract(); ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.,/"); ocr.Init(@"C:\source\innovation\Content\tessdata", "eng", false); var resizedImage = (Bitmap)Resize(image, (3000), (3000), false); resizedImage.SetResolution(300, 300); var blackAndWhiteImage = BlackAndWhite(resizedImage, new Rectangle(0, 0, resizedImage.Width, resizedImage.Height)); var result = ocr.DoOCR(blackAndWhiteImage, Rectangle.Empty); OCRRawDataModel dataItems = new OCRRawDataModel(); dataItems.DataList = new List <OCRRawDataModel.RawDataItem>(); foreach (Word word in result) { var item = new OCRRawDataModel.RawDataItem(); item.Value = word.Text; item.Confidence = (int)word.Confidence; item.LineIndex = word.LineIndex; dataItems.DataList.Add(item); } var mapper = new IdentificationCardMapper(); var mappedObjects = mapper.MapDriversLicenseData(dataItems); myResponse.jsonResponse = JsonConvert.SerializeObject(mappedObjects); // cleanup image.Dispose(); resizedImage.Dispose(); ocr.Dispose(); } catch (Exception ex) { // i don't know what to do here... myResponse.jsonResponse = ex.Message; } return(myResponse); }
public void MapDriversLicenseDataTest() { var data = new OCRRawDataModel { DataList = new List <OCRRawDataModel.RawDataItem>() }; #region create DataList data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "NEW", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "JERSEY", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "Il", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "'\"", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "AUTO", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "DRIVER", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "LICENSE", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "....", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "{gg}", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "x9000", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "99090", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "90909", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "D", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "···", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "mm", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "05-20-1956", LineIndex = 1 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "BOAT", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "05-20-2010", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "c><~", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "05-20-2014", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "LAST", LineIndex = 4 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "NAME", LineIndex = 4 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "SUFIX", LineIndex = 4 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "FIRST", LineIndex = 5 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "MIDDLE", LineIndex = 5 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "123", LineIndex = 6 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "NORTH", LineIndex = 6 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "STATE", LineIndex = 6 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "STREET", LineIndex = 6 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "TRENTON", LineIndex = 7 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "NJ", LineIndex = 7 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "08655-1234", LineIndex = 7 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "um", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "NONE", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "¥", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "num", LineIndex = 0 }); data.DataList.Add(new OCRRawDataModel.RawDataItem { Value = "NONE", LineIndex = 0 }); #endregion create DataList var mapper = new IdentificationCardMapper(); var results = mapper.MapDriversLicenseData(data); Assert.AreNotEqual(results, string.Empty); }