OCRResult.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2021 raoyutian Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Runtime.InteropServices;
  17. namespace PaddleOCRSharp
  18. {
  19. /// <summary>
  20. /// OCR识别结果
  21. /// </summary>
  22. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  23. public class OCRResult
  24. {
  25. /// <summary>
  26. /// 文本块列表
  27. /// </summary>
  28. public List<TextBlock> TextBlocks { get; set; } = new List<TextBlock>();
  29. /// <summary>
  30. /// 识别结果文本
  31. /// </summary>
  32. public string Text => this.ToString();
  33. /// <summary>
  34. /// 识别结果文本Json格式
  35. /// </summary>
  36. public string JsonText { get; set; }
  37. /// <summary>
  38. /// 返回字符串格式
  39. /// </summary>
  40. public override string ToString()
  41. {
  42. if (TextBlocks == null) return "";
  43. return string.Join("", TextBlocks.Select(x => x.Text).ToArray());
  44. }
  45. }
  46. /// <summary>
  47. /// 识别的文本块
  48. /// </summary>
  49. public class TextBlock
  50. { /// <summary>
  51. /// 文本块四周顶点坐标列表
  52. /// </summary>
  53. public List<OCRPoint> BoxPoints { get; set; } = new List<OCRPoint>();
  54. /// <summary>
  55. /// 文本块文本
  56. /// </summary>
  57. public string Text { get; set; }
  58. /// <summary>
  59. ///文本识别置信度
  60. /// </summary>
  61. public float Score { get; set; }
  62. /// <summary>
  63. ///角度分类置信度
  64. /// </summary>
  65. public float cls_score { get; set; }
  66. /// <summary>
  67. ///角度分类标签
  68. /// </summary>
  69. public int cls_label { get; set; }
  70. /// <summary>
  71. /// 返回字符串格式
  72. /// </summary>
  73. public override string ToString()
  74. {
  75. if (BoxPoints == null) return "";
  76. string str = string.Join(",", BoxPoints.Select(x => x.ToString()).ToArray());
  77. return $"{Text},Score:{Score},[{str}],cls_label:{cls_label},cls_score:{cls_score}";
  78. }
  79. }
  80. /// <summary>
  81. /// 点对象
  82. /// </summary>
  83. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  84. public class OCRPoint
  85. {
  86. /// <summary>
  87. /// X坐标,单位像素
  88. /// </summary>
  89. public int X;
  90. /// <summary>
  91. /// Y坐标,单位像素
  92. /// </summary>
  93. public int Y;
  94. /// <summary>
  95. ///默认构造函数
  96. /// </summary>
  97. public OCRPoint()
  98. {
  99. }
  100. /// <summary>
  101. /// 构造函数
  102. /// </summary>
  103. /// <param name="x"></param>
  104. /// <param name="y"></param>
  105. public OCRPoint(int x, int y)
  106. {
  107. X = x;
  108. Y = y;
  109. }
  110. /// <summary>
  111. /// 返回字符串格式
  112. /// </summary>
  113. public override string ToString() => $"({X},{Y})";
  114. }
  115. }