/// <summary> /// 从文件中识别图片中的文字 /// </summary> /// <param name="path">图片路径</param> /// <returns></returns> public static JObject IdentifyImageFile(string path) { var image = File.ReadAllBytes(path); // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 //var result = client.GeneralBasic(image);//基本版 var result = client.Accurate(image);//高精度版 位置 return(result); }
/// <summary> /// 圖片辨識 /// </summary> public static string BaiduOCR(string imagepath) // 百度_圖片辨識 { var API_KEY = "cohIahxAt7HveHLYSHYK6G5N"; // "FGPi0QpCbZxZxBaN6dvqt87X"; var SECRET_KEY = "e8SAsDIWSK9NPUKviYiPQNlfaVDXQSY5"; // "HunNq6XsLjF3a7aCAuirVaVQO7CKBuwW"; var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超時時間 var image = File.ReadAllBytes(imagepath); // 呼叫通用文字識別, 圖片引數為本地圖片,可能會丟擲網路等異常,請使用try/catch捕獲 //使用者向服務請求識別某張圖中的所有文字 //var result = client.GeneralBasic(image); //本地圖圖片 //var result = client.GeneralBasicUrl(url); //網路圖片 //var result = client.General(image); //本地圖片:通用文字識別(含位置資訊版) //var result = client.GeneralUrl(url); //網路圖片:通用文字識別(含位置資訊版) //var result = client.GeneralEnhanced(image); //本地圖片:呼叫通用文字識別(含生僻字版) //var result = client.GeneralEnhancedUrl(url); //網路圖片:呼叫通用文字識別(含生僻字版) //var result = client.WebImage(image); //本地圖片:使用者向服務請求識別一些背景複雜,特殊字型的文字。 //var result = client.WebImageUrl(url); //網路圖片:使用者向服務請求識別一些背景複雜,特殊字型的文字。 string JsonString = client.Accurate(image).ToString(); //本地圖片:相對於通用文字識別該產品精度更高,但是識別耗時會稍長。 string cut = JsonString.Substring(45, 10); // 擷取jason 文字串中 解析出的圖片文字 這段 string CutResult = Regex.Replace(cut, "[^0-9A-Za-z]", ""); //去除掉符號空白...只留下字母&数字 return(CutResult); }
public static string getString(Image img) { var API_KEY = "FGPi0QpCbZxZxBaN6dvqt87X"; var SECRET_KEY = "HunNq6XsLjF3a7aCAuirVaVQO7CKBuwW"; var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 var result = client.Accurate(imageToByteArray(img)); JToken words = null; if (result.TryGetValue("words_result", out words)) { if (words.Count() > 0) { if (words[0].Contains("words")) { return(words[0]["words"].ToString()); } } return(null); } return(result.ToString()); //var result = client.General(File.ReadAllBytes("c:\\untitled.png")); return(result["words_result"][0]["words"].ToString()); }
public static void Accurate() { var client = new Baidu.Aip.Ocr.Ocr("Api Key", "Secret Key"); var image = File.ReadAllBytes("图片文件路径"); // 高精度识别 var result = client.Accurate(image); }
public void AccurateDemo() { var client = new Baidu.Aip.Ocr.Ocr(API_Key, SECRE_KEY); client.Timeout = 60000; var image = File.ReadAllBytes(ImagePath); OutResult = client.Accurate(image); }
public static String Accurate(string imgpath) { var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); var image = File.ReadAllBytes(imgpath); // 高精度识别 var result = client.Accurate(image); return(result.ToString()); }
public static string SECRET_KEY = ConfigurationManager.AppSettings["BAIDU_SECRET_KEY"].ToString(); //"HunNq6XsLjF3a7aCAuirVaVQO7CKBuwW"; /// <summary> /// https://ai.baidu.com/ai-doc/OCR/Mk3h7y6j8 百度接口文档 /// </summary> /// <param name="imgpath"></param> /// <returns></returns> public static string Transformation(string imgpath) { var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 var image = File.ReadAllBytes(imgpath); // var url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564654456007&di=7832dd6f515e654bdf5074e47b6803b1&imgtype=0&src=http%3A%2F%2Fpic.962.net%2Fup%2F2018-5%2F2018527102938219310.jpg"; // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 //用户向服务请求识别某张图中的所有文字 // var result = client.GeneralBasic(image); //本地图图片 // 如果有可选参数 // var options = new Dictionary<string, object>{ // {"language_type", "CHN_ENG"}, // {"detect_direction", "true"}, // {"detect_language", "true"}, // {"probability", "true"} //}; // // 带参数调用通用文字识别, 图片参数为本地图片 // result = client.GeneralBasic(image, options); //var result = client.GeneralBasicUrl(url); //网络图片 var result = client.Accurate(image); // var options = new Dictionary<string, object>{ // {"detect_direction", "true"}, // {"probability", "true"} //}; //var result = client.Accurate(image,options); //本地图片:相对于通用文字识别该产品精度更高,但是识别耗时会稍长。 //var result = client.General(image); //本地图片:通用文字识别(含位置信息版) //var result = client.GeneralUrl(url); //网络图片:通用文字识别(含位置信息版) //var result = client.GeneralEnhanced(image); //本地图片:调用通用文字识别(含生僻字版) //var result = client.GeneralEnhancedUrl(url); //网络图片:调用通用文字识别(含生僻字版) //var result = client.WebImage(image); //本地图片:用户向服务请求识别一些背景复杂,特殊字体的文字。 //var result = client.WebImageUrl(url); //网络图片:用户向服务请求识别一些背景复杂,特殊字体的文字。 YunCe.OCR.Common.BaiduEntity.LocalImageResponseEntity info = JsonConvert.DeserializeObject <YunCe.OCR.Common.BaiduEntity.LocalImageResponseEntity>(result.ToString()); StringBuilder str = new StringBuilder(); foreach (var item in info.WordsResult) { str.Append(item.words + "<br>"); } return(str.ToString()); }
private void GetOcrJsonAndPrint(Baidu.Aip.Ocr.Ocr client, byte[] bytes) { IsProgressVisibility = Visibility.Visible; List <Root> rootList = new List <Root>(); string jsonStr = string.Empty; try { if (NormalRadioButton == true) { jsonStr = JsonConvert.SerializeObject(client.GeneralBasic(bytes)); } else { jsonStr = JsonConvert.SerializeObject(client.Accurate(bytes)); } } catch (Exception ex) { System.Windows.MessageBox.Show("Ocr获取结果失败" + ex); IsProgressVisibility = Visibility.Collapsed; return; } Root root = null; if (!string.IsNullOrWhiteSpace(jsonStr)) { root = JsonConvert.DeserializeObject <Root>(jsonStr); } OcrResult = string.Empty; foreach (var item in root.words_result) { OcrResult = OcrResult + item.words + "\n"; } IsProgressVisibility = Visibility.Collapsed; MyWindow.Dispatcher.Invoke(new Action(() => { AddRecord(); })); }
private void LoadImage(string filePath) { _textSegments.Clear(); List <Mat> croppedChars = new List <Mat>(); byte[] image = File.ReadAllBytes(filePath); Mat originalMat = Mat.FromImageData(image, ImreadModes.AnyColor); Mat displayMat = originalMat.Clone(); _imageSize = originalMat.Size(); Dictionary <string, object> options = new Dictionary <string, object> { { "recognize_granularity", "small" }, { "detect_direction", "true" }, { "vertexes_location", "true" }, { "probability", "true" }, { "detect_language", "true" } }; JObject resultJson = _client.Accurate(image, options); // OCR accurate //Debug.Print(resultJson.ToString()); dynamic ocrResult = JsonConvert.DeserializeObject <dynamic>(resultJson.ToString()); int wordCount = ocrResult.words_result_num; for (int i = 0; i < wordCount; i++) { dynamic chars = ocrResult.words_result[i].chars; for (int j = 0; j < chars.Count; j++) { TextSegmentData segmentData = new TextSegmentData { TextLine = ocrResult.words_result[i].words, TextLineWidth = ocrResult.words_result[i].location.width, TextLineTop = ocrResult.words_result[i].location.top, TextLineLeft = ocrResult.words_result[i].location.left, TextLineHeight = ocrResult.words_result[i].location.height, TextChar = chars[j]["char"], TextCharWidth = chars[j].location.width, TextCharTop = chars[j].location.top, TextCharLeft = chars[j].location.left, TextCharHeight = chars[j].location.height }; segmentData.IsCJK = segmentData.TextChar.Any(x => x.IsChinese()); Debug.Print($"Text: {segmentData.TextChar}, IsCJK? {segmentData.IsCJK}, W {segmentData.TextCharWidth}, H {segmentData.TextCharHeight}, T {segmentData.TextCharTop}, L {segmentData.TextCharLeft} "); if (segmentData.IsCJK) { Rect cropCharRect = new Rect( segmentData.TextCharLeft, segmentData.TextCharTop, GetSizeSafe((int)(segmentData.TextCharWidth * 1.5), segmentData.TextCharLeft, originalMat.Width), GetSizeSafe((int)(segmentData.TextCharHeight * 1.2), segmentData.TextCharTop, originalMat.Height) ); //displayMat.Rectangle(cropCharRect, Scalar.RandomColor(), 2, LineTypes.AntiAlias); // mark every word Mat croppedChar = new Mat(originalMat, cropCharRect); croppedChars.Add(croppedChar); segmentData.TextCharCroppedMat = croppedChar.Clone(); Rect cropTextRect = new Rect(segmentData.TextLineLeft, segmentData.TextLineTop, segmentData.TextLineWidth, segmentData.TextLineHeight); Mat croppedLine = new Mat(originalMat, cropTextRect); segmentData.TextLineCroppedMat = croppedLine.Clone(); //croppedChar.SaveImage("!" + DateTime.Now.Ticks + ".png"); } _textSegments.Add(segmentData); } } int netInputWidth = 80; int netInputHeight = 80; using (Net net = CvDnn.ReadNetFromTensorflow(AppDomain.CurrentDomain.BaseDirectory + "all_freezed_vgg19_tf115.pb")) { foreach (TextSegmentData sgData in _textSegments.Where(x => x.IsCJK).ToArray()) { // preprocess //sgData.TextCharCroppedMat.SaveImage("!" + DateTime.Now.Ticks + ".png"); Mat greyText = sgData.TextCharCroppedMat.CvtColor(ColorConversionCodes.BGR2GRAY); //Mat textAfterThreshold = new Mat(); //Cv2.Threshold(greyText, textAfterThreshold, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); //Mat textAfterMorph = new Mat(); //Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 1)); //Cv2.MorphologyEx(textAfterThreshold, textAfterMorph, MorphTypes.Open, kernel); //Cv2.MorphologyEx(textAfterMorph, textAfterMorph, MorphTypes.Close, kernel); // resize double scaleW = netInputWidth / (double)sgData.TextCharCroppedMat.Width; double scaleH = netInputHeight / (double)sgData.TextCharCroppedMat.Height; double scale = scaleW < scaleH ? scaleW : scaleH; Mat resizedText = new Mat(); Cv2.Resize(greyText, resizedText, new Size(0, 0), scale, scale, InterpolationFlags.Cubic); int padTop = 0; int padBottom = 0; int padLeft = 0; int padRight = 0; if (resizedText.Width < netInputWidth) { padLeft = (netInputWidth - resizedText.Width) / 2; if ((netInputWidth - resizedText.Width) % 2 > 0) { padRight = padLeft + 1; } else { padRight = padLeft; } } else if (resizedText.Height < netInputHeight) { padTop = (netInputHeight - resizedText.Height) / 2; if ((netInputHeight - resizedText.Height) % 2 > 0) { padBottom = padTop + 1; } else { padBottom = padTop; } } resizedText = resizedText.CopyMakeBorder(padTop, padBottom, padLeft, padRight, BorderTypes.Constant, Scalar.White); resizedText = resizedText.CvtColor(ColorConversionCodes.GRAY2BGR); // inferring needs BGR input instead of gray //Cv2.ImShow("" + Guid.NewGuid(), resizedText); //resizedText.SaveImage("!" + DateTime.Now.Ticks + ".png"); int classId1; double classProb1; List <CharProbClass> probList; var inputBlob = CvDnn.BlobFromImage(resizedText, 1, new Size(netInputWidth, netInputHeight), new Scalar(104, 117, 123)); net.SetInput(inputBlob); var prob = net.Forward(); GetMaxClass(prob, out classId1, out classProb1, out probList); sgData.ClassLable = GetClassText(classId1); sgData.ClassProb = classProb1; sgData.ProbClassList = probList; Debug.Print($"Char:{sgData.TextChar}, ClassID:{GetClassText(classId1)}, classProb:{classProb1}"); } } // done image processing, calculating var groupedTextLines = _textSegments.Where(x => x.IsCJK).GroupBy( x => new { x.TextLineWidth, x.TextLineTop, x.TextLineLeft, x.TextLineHeight }).ToArray(); foreach (var textLine in groupedTextLines) { Dictionary <string, double> fontProbDict = new Dictionary <string, double>(); foreach (TextSegmentData segmentData in textLine) { if (!fontProbDict.ContainsKey(segmentData.ClassLable)) { fontProbDict.Add(segmentData.ClassLable, segmentData.ClassProb); } else if (segmentData.ClassProb > fontProbDict[segmentData.ClassLable]) { fontProbDict[segmentData.ClassLable] += segmentData.ClassProb; } } var orderedFontProb = fontProbDict.OrderByDescending(x => x.Value).ToArray(); Debug.Print($"Text Line: {textLine.FirstOrDefault()?.TextLine}, Font Name: {orderedFontProb[0].Key}"); foreach (TextSegmentData data in textLine) { data.TextLineFont = orderedFontProb[0].Key; data.ProbClassList = textLine.ToList().FirstOrDefault()?.ProbClassList; } Rect textLineRect = new Rect((int)textLine.FirstOrDefault()?.TextLineLeft, (int)textLine.FirstOrDefault()?.TextLineTop, (int)textLine.FirstOrDefault()?.TextLineWidth, (int)textLine.FirstOrDefault()?.TextLineHeight); displayMat.Rectangle(textLineRect, Scalar.RandomColor(), 2, LineTypes.AntiAlias); } pictureBoxOriginal.Image = displayMat.ToBitmap(); }
public static void Run(string path, List <Models> models) { var image = File.ReadAllBytes(path); // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 //var result = client.AccurateBasic(image); //Console.WriteLine(result); // 如果有可选参数 var options = new Dictionary <string, object> { { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, //是否显示置信度 { "probability", "true" } }; // 带参数调用通用文字识别, 图片参数为本地图片 var result = client.Accurate(image, options); Console.WriteLine(result); Console.Read(); //解释Json Root rt = JsonConvert.DeserializeObject <Root>(result.ToString()); //List<Models> models = new List<Models>(); //右半页OCR后的识别判断,location.left的值只适用于河北省招生计划右半页 if (path.Contains("right")) { //要定位left筛选出专业代号和专业名称,只测试过右半部分,有时会误选专业说明,是IEnumerable var majors = (from t in rt.words_result where t.location.left > 0 && t.location.left < 250 && Regex.IsMatch(t.words, @"^\d{2}\D") select t).ToList(); //要定位left筛选出院校代号和院校名称,只测试过右半部分,是IEnumerable var colleges = (from t in rt.words_result where t.location.left >= 0 && t.location.left < 60 && Regex.IsMatch(t.words, @"^\d{4}") && !t.words.Contains("代号") select t).ToList(); //要left及与院校名称差不多高度的top定位,筛选出院校招生人数 var collegeNums = from t in rt.words_result from c in colleges where Math.Abs(t.location.top - c.location.top) < 30 && t.location.left > 600 select t.words; for (int i = 0; i < majors.Count; i++) { //要left及与专业名称差不多高度的top定位,筛选出招生人数、学制和学费 string majorNum = (from t in rt.words_result where Math.Abs(t.location.top - majors[i].location.top) < 40 && t.location.left > 600 select t.words).OrderBy(wordsResultItem => wordsResultItem.Length).LastOrDefault(); var college = (from c in colleges where c.location.top - majors[i].location.top < 0 select c).LastOrDefault(); if (college != null) { string collegeNum = (from t in rt.words_result where Math.Abs(t.location.top - college.location.top) < 40 && t.location.left > 600 select t.words).FirstOrDefault(); models.Add(new Models(majors[i].words, majorNum, college.words, collegeNum)); } else { models.Add(models.Count > 0 ? new Models(majors[i].words, majorNum, models.Last().CollegeName, models.Last().CollegeNum) : new Models(majors[i].words, majorNum, string.Empty, string.Empty)); } } //保存出现在页未的学校名,但下面没专业,留待下一页用 if (!models.Last().Equals(colleges.Last())) { models.Add(new Models(string.Empty, string.Empty, colleges.Last().words, collegeNums.Last())); } //foreach (var model in models) //{ // Console.WriteLine(model.MajorName + " " + model.MajorNum + " " + model.CollegeName + " " + model.CollegeNum); //} } //左半页OCR后的识别判断,location.left的值只适用于河北省招生计划左半页 else if (path.Contains("left")) { var majors = (from t in rt.words_result where t.location.left > 0 && t.location.left <= 250 && Regex.IsMatch(t.words, @"^\d{2}\D") select t).ToList(); //要定位left筛选出院校代号和院校名称,只适用左半部分,是IEnumerable var colleges = rt.words_result.Where(t => t.location.left >= 130 && t.location.left < 210 && Regex.IsMatch(t.words, @"^\d{4}") && !t.words.Contains("代号")).ToList(); //要left及与院校名称差不多高度的top定位,筛选出院校招生人数 var collegeNums = rt.words_result.SelectMany(t => colleges, (t, c) => new { t, c }) .Where(@t1 => Math.Abs(@t1.t.location.top - @t1.c.location.top) < 30 && @t1.t.location.left > 600) .Select(@t1 => @t1.t.words).ToList(); for (int i = 0; i < majors.Count; i++) { //要left及与专业名称差不多高度的top定位,筛选出招生人数、学制和学费 string majorNum = (from t in rt.words_result where Math.Abs(t.location.top - majors[i].location.top) < 40 && t.location.left > 600 select t.words).OrderBy(wordsResultItem => wordsResultItem.Length).LastOrDefault(); var college = (from c in colleges where c.location.top - majors[i].location.top < 0 select c).LastOrDefault(); if (college != null) { string collegeNum = (from t in rt.words_result where Math.Abs(t.location.top - college.location.top) < 40 && t.location.left > 600 select t.words).FirstOrDefault(); models.Add(new Models(majors[i].words, majorNum, college.words, collegeNum)); } else { models.Add(models.Count > 0 ? new Models(majors[i].words, majorNum, models.Last().CollegeName, models.Last().CollegeNum) : new Models(majors[i].words, majorNum, string.Empty, string.Empty)); } } //保存出现在页未的学校名,但下面没专业,留待下一页用 if (!models.Last().Equals(colleges.Last())) { models.Add(new Models(string.Empty, string.Empty, colleges.Last().words, collegeNums.Last())); } //foreach (var model in models) //{ // Console.WriteLine(model.MajorName + " " + model.MajorNum + " " + model.CollegeName + " " + model.CollegeNum); //} } }
public void TestMethod1() { var fileList = new string[] { "A", "B", "C", "D" }; var client = new Baidu.Aip.Ocr.Ocr(BaiduHelper.API_KEY, BaiduHelper.SECRET_KEY); foreach (var fItem in fileList) { //var ocr = new TesseractEngine("./tessdata", "chi_sim", EngineMode.Default); //ocr.SetVariable("tessedit_char_whitelist", "委买卖一"); var img = new Bitmap(@"F:\byj\pic\" + fItem + ".png"); var image = File.ReadAllBytes(@"F:\byj\pic\" + fItem + ".png"); // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 var options = new Dictionary <string, object> { { "recognize_granularity", "big" }, { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "vertexes_location", "true" }, { "probability", "true" } }; // 带参数调用通用文字识别(含位置信息版), 图片参数为本地图片 var result = client.General(image, options); var show = result["words_result"].ToList(); var one = show.Where(a => a["words"].ToString().StartsWith("委买队列")).FirstOrDefault(); Newtonsoft.Json.Linq.JToken buyone = null; if (one == null) { var AccurateOptions = new Dictionary <string, object> { { "recognize_granularity", "big" }, { "detect_direction", "true" }, { "vertexes_location", "true" }, { "probability", "true" } }; result = client.Accurate(image, AccurateOptions); show = result["words_result"].ToList(); one = show.Where(a => a["words"].ToString().StartsWith("委买队列")).FirstOrDefault(); } if (one == null) { throw new Exception("分析失败 "); } Pen myPen = new Pen(System.Drawing.Color.BurlyWood, 3); var g = Graphics.FromImage(img); int width = Convert.ToUInt16(one["location"]["width"].ToString()); int height = Convert.ToUInt16(one["location"]["height"].ToString()); int x = Convert.ToUInt16(one["location"]["left"].ToString()); int y = Convert.ToUInt16(one["location"]["top"].ToString()); g.DrawRectangle(myPen, new Rectangle(x, y, width, height)); g.DrawRectangle(myPen, new Rectangle(x - 68, y + height, 55, height)); //g.DrawRectangle(myPen, new Rectangle(x - 40, y + 37, 80, 31)); g.DrawRectangle(myPen, new Rectangle(x + 36, y + height, 40, height)); g.DrawRectangle(myPen, new Rectangle(x + 89, y + height, 30, height)); Bitmap bmp = new Bitmap(img); img.Dispose(); bmp.Save(@"F:\byj\pic\" + fItem + "_b.png"); // } }