另外附上 百度AI 文字识别 Json 及其模型类
图片
笔者在弄了一段时间的百度 Ai 平台的SDK,封装了OCR SDK,由于现在在找实习工作,所以有部分没有弄完,有兴趣可以添加笔者的微信免费获取。微信在右侧导航栏。
百度AI 识别文字,返回Json结果, 名字随意.格式建议为 json,如果使用记事本保存,注意编码格式是 utf-8,因为c# string默认为utf8,不然会乱码。
{
"log_id": 3413661945235258919,
"direction": 0,
"words_result_num": 2,
"words_result": [
{
"vertexes_location": [
{
"y": 81,
"x": 51
},
{
"y": 81,
"x": 151
},
{
"y": 103,
"x": 151
},
{
"y": 103,
"x": 51
}
],
"probability": {
"variance": 0.0,
"average": 0.999861,
"min": 0.999627
},
"chars": [
{
"char": "今",
"location": {
"width": 17,
"top": 83,
"left": 60,
"height": 20
}
},
{
"char": "天",
"location": {
"width": 17,
"top": 83,
"left": 78,
"height": 20
}
},
{
"char": "除",
"location": {
"width": 12,
"top": 83,
"left": 103,
"height": 20
}
},
{
"char": "了",
"location": {
"width": 16,
"top": 83,
"left": 116,
"height": 20
}
},
{
"char": "皮",
"location": {
"width": 13,
"top": 83,
"left": 140,
"height": 20
}
}
],
"min_finegrained_vertexes_location": [
{
"y": 81,
"x": 51
},
{
"y": 81,
"x": 151
},
{
"y": 103,
"x": 151
},
{
"y": 103,
"x": 51
}
],
"finegrained_vertexes_location": [
{
"y": 81,
"x": 51
},
{
"y": 81,
"x": 71
},
{
"y": 81,
"x": 90
},
{
"y": 81,
"x": 110
},
{
"y": 81,
"x": 129
},
{
"y": 81,
"x": 149
},
{
"y": 81,
"x": 151
},
{
"y": 91,
"x": 151
},
{
"y": 100,
"x": 151
},
{
"y": 103,
"x": 151
},
{
"y": 103,
"x": 132
},
{
"y": 103,
"x": 112
},
{
"y": 103,
"x": 93
},
{
"y": 103,
"x": 73
},
{
"y": 103,
"x": 54
},
{
"y": 103,
"x": 51
},
{
"y": 93,
"x": 51
},
{
"y": 84,
"x": 51
}
],
"location": {
"width": 102,
"top": 81,
"left": 51,
"height": 24
},
"words": "今天除了皮"
},
{
"vertexes_location": [
{
"y": 109,
"x": 52
},
{
"y": 109,
"x": 152
},
{
"y": 130,
"x": 152
},
{
"y": 130,
"x": 52
}
],
"probability": {
"variance": 8E-05,
"average": 0.9907,
"min": 0.973259
},
"chars": [
{
"char": "又",
"location": {
"width": 16,
"top": 111,
"left": 61,
"height": 20
}
},
{
"char": "啥",
"location": {
"width": 12,
"top": 111,
"left": 85,
"height": 20
}
},
{
"char": "也",
"location": {
"width": 16,
"top": 111,
"left": 98,
"height": 20
}
},
{
"char": "没",
"location": {
"width": 15,
"top": 111,
"left": 123,
"height": 20
}
},
{
"char": "干",
"location": {
"width": 13,
"top": 111,
"left": 141,
"height": 20
}
}
],
"min_finegrained_vertexes_location": [
{
"y": 109,
"x": 52
},
{
"y": 109,
"x": 152
},
{
"y": 130,
"x": 152
},
{
"y": 130,
"x": 52
}
],
"finegrained_vertexes_location": [
{
"y": 109,
"x": 52
},
{
"y": 109,
"x": 71
},
{
"y": 109,
"x": 91
},
{
"y": 109,
"x": 110
},
{
"y": 109,
"x": 129
},
{
"y": 109,
"x": 149
},
{
"y": 109,
"x": 152
},
{
"y": 119,
"x": 152
},
{
"y": 129,
"x": 152
},
{
"y": 130,
"x": 152
},
{
"y": 130,
"x": 133
},
{
"y": 130,
"x": 113
},
{
"y": 130,
"x": 94
},
{
"y": 130,
"x": 74
},
{
"y": 130,
"x": 55
},
{
"y": 130,
"x": 52
},
{
"y": 121,
"x": 52
},
{
"y": 111,
"x": 52
}
],
"location": {
"width": 102,
"top": 109,
"left": 52,
"height": 22
},
"words": "又啥也没干"
}
],
"language": -1
}
对应的模型 ,将 cs 文件,名字 GeneralModel.cs
/// <summary> /// 通用文字识别(含位置版)返回结果 /// </summary> public class GeneralModel { /// <summary> /// 必选 /// 唯一的log id,用于问题定位 /// </summary> public long log_id { get; set; } /// <summary> /// 图像方向,当detect_direction=true时存在。 /// 非必选 ///- -1:未定义, ///- 0:正向, ///- 1: 逆时针90度, ///- 2:逆时针180度, ///- 3:逆时针270度 /// </summary> public int direction { get; set; } /// <summary> /// 必选 /// 识别结果数,表示words_result的元素个数 /// </summary> public int words_result_num { get; set; } /// <summary> /// 检测语言 默认值会返回 -1 /// </summary> public string language { get; set; } /// <summary> /// 定位和识别文字结果数组 /// </summary> public List<Words_result> words_result { get; set; } public class Words_result { /// <summary> /// 图片中文字段四个顶点位置(矩形范围) /// </summary> public List<XY> vertexes_Location { get; set; } /// <summary> /// 可选 /// 行置信度信息;如果输入参数 probability = true 则输出 /// </summary> public Probability probability { get; set; } /// <summary> /// 每个字 /// </summary> public List<Chars> chars { get; set; } /// <summary> /// 最小细粒度顶点坐标 /// </summary> public List<XY> min_finegrained_vertexes_location { get; set; } /// <summary> /// 细粒度顶点坐标,多边形 /// </summary> public List<XY> finegrained_vertexes_location { get; set; } /// <summary> /// 文字在图片中的相对位置 /// </summary> public Location location { get; set; } /// <summary> /// 识别出的文字 /// </summary> public string words { get; set; } /// <summary> /// 坐标 /// </summary> public class XY { public int x { get; set; } public int y { get; set; } } /// <summary> /// 行置信度 /// </summary> public class Probability { /// <summary> /// 行置信度平均值方差 /// </summary> public double variance { get; set; } /// <summary> /// 行置信度平均值 /// </summary> public double average { get; set; } /// <summary> /// 行置信度最小值 /// </summary> public double min { get; set; } } /// <summary> /// 单个文字 /// </summary> public class Chars { /// <summary> /// 识别的单个文字 /// </summary> public char chaR { get; set; } /// <summary> /// 该文字范围(矩形) /// </summary> public Location location { get; set; } } } public class Location { public int left { get; set; } public int top { get; set; } public int width { get; set; } public int height { get; set; } } } }
可用控制台进行检验
static void Main(string[] args) { StreamReader streamReader = new StreamReader(System.IO.File.OpenRead(@"json文件位置")); string str = ""; string jsonstr; while ((jsonstr = streamReader.ReadLine()) != null) { str += jsonstr; } GeneralModel generalModel = JsonConvert.DeserializeObject<GeneralModel>(str); Console.WriteLine("图片id:" + generalModel.log_id); Console.WriteLine("图像方向:" + generalModel.direction); Console.WriteLine("检测语言为:" + generalModel.language); Console.WriteLine("有几个结果:" + generalModel.words_result_num); foreach (var item in generalModel.words_result) { Console.WriteLine("识别结果:" + Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(item.words))); foreach (var itemi in item.vertexes_Location) { Console.WriteLine("{x:" + itemi.x + ";y:" + itemi.y + "}"); } Console.WriteLine("Probability:可信度:" + "行置信度平均值" + item.probability.average + ";行置信度平均值方差:" + item.probability.variance + ";行置信度平均值最小值:" + item.probability.min); foreach (var itemi in item.chars) { Console.WriteLine(itemi.chaR); Console.WriteLine("位置: left:" + itemi.location.left + "; height: " + itemi.location.height + "top: " + itemi.location.top + "; width: " + itemi.location.width); } } Console.ReadKey();