fasdfsa commited on
Commit
ca159a3
·
1 Parent(s): 3fefa46

ali ocr c# ok?

Browse files
src/WeChatOcrCpp/ocr_aliyun.py CHANGED
@@ -11,6 +11,9 @@ pip install "alibabacloud_darabonba_stream>=0.0.2,<1.0.0"
11
  pip install "alibabacloud_credentials>=1.0.2,<2.0.0"
12
  """
13
 
 
 
 
14
  import json
15
  import decimal
16
  import datetime
 
11
  pip install "alibabacloud_credentials>=1.0.2,<2.0.0"
12
  """
13
 
14
+ # see huggingface\imradv3\src\WpfEditor\AliOcr.cs
15
+ # see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyunV2.js
16
+
17
  import json
18
  import decimal
19
  import datetime
src/WeChatOcrCpp/ocr_aliyunV2.js CHANGED
@@ -21,6 +21,9 @@
21
 
22
  (async () => {
23
 
 
 
 
24
  let Credential = require('@alicloud/credentials')
25
 
26
  const { Readable } = require('stream')
 
21
 
22
  (async () => {
23
 
24
+ // see huggingface\imradv3\src\WpfEditor\AliOcr.cs
25
+ // see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyun.py
26
+
27
  let Credential = require('@alicloud/credentials')
28
 
29
  const { Readable } = require('stream')
src/WpfEditor/AliOcr.cs CHANGED
@@ -22,6 +22,7 @@ namespace WpfEditor
22
  {
23
 
24
  // see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyunV2.js
 
25
 
26
  // // https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeadvanced#api-detail-35
27
  // https://github.com/aliyun/alibabacloud-csharp-sdk
@@ -96,12 +97,10 @@ namespace WpfEditor
96
  {
97
  if (_client == null)
98
  {
99
- //cfg = fs.readFileSync("../../src/WpfEditor/config.json")
100
-
101
  string pth_config = Path.Combine(Constant.ExecutablePath, "config.json");
102
  if (!System.IO.File.Exists(pth_config))
103
  {
104
- throw new Exception("config.json not found.");
105
  }
106
 
107
  string cf = File.ReadAllText(pth_config);
@@ -120,78 +119,48 @@ namespace WpfEditor
120
  Credential = credential
121
  };
122
  _client = new AlibabaCloud.SDK.Ocr_api20210707.Client(config);
 
123
 
124
- AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeAdvancedRequest recognizeAdvancedRequest = new AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeAdvancedRequest();
125
- AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
126
- try
127
- {
128
- AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeAdvancedResponse resp = _client.RecognizeAdvancedWithOptions(recognizeAdvancedRequest, runtime);
129
- AlibabaCloud.TeaConsole.Client.Log(AlibabaCloud.TeaUtil.Common.ToJSONString(resp));
130
- }
131
- catch (Exception error)
132
- {
133
- Console.WriteLine(error.Message);
134
- }
135
-
136
  }
137
 
 
 
 
 
 
 
 
 
 
 
138
  try
139
  {
 
 
 
140
 
141
- // 将C字符串转换为C#字符串
142
- string result = ""; // ocrAsync(string api, string appCode, string imgData);
143
-
144
- // 给每个字符加上 guid
145
- JObject jsn = null;
146
- try
147
  {
148
- jsn = JObject.Parse(result);
 
149
  }
150
- catch (Exception ex)
151
  {
152
- MessageBox.Show("json 解析失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
153
  return "";
154
  }
155
 
156
- int width = jsn.Value<int>("width");
157
- int height = jsn.Value<int>("height");
158
-
159
- var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
160
- foreach (var item in prism_wordsInfo)
161
- {
162
- JArray charInfo = item["charInfo"].Value<JArray>();
163
- string line_word = item["word"].Value<string>();
164
-
165
- var textLine = new TextLine();
166
- foreach (var item2 in charInfo)
167
- {
168
- item2["guid"] = Guid.NewGuid().ToString();
169
- item2["isDeleted"] = 0;
170
-
171
- string oneChar = item2["word"].Value<string>();
172
- int x = item2["x"].Value<int>();
173
- int y = item2["y"].Value<int>();
174
- int w = item2["w"].Value<int>();
175
- int h = item2["h"].Value<int>();
176
-
177
- //// 遍历 oneChar 中的每个字符
178
- //for (int i = 0; i < oneChar.Length;)
179
- //{
180
- // Rune rune = Rune.GetRuneAt(oneChar, i); // 4 个字节表示单个字符
181
-
182
- // // 移动到下一个 Rune 的位置
183
- // i += rune.Utf16SequenceLength;
184
- //}
185
-
186
- }
187
- }
188
-
189
- return jsn.ToString(Formatting.Indented);
190
  }
191
- catch (Exception ex)
192
  {
193
- return $"调用OCR函数时出错: {ex.Message}";
 
194
  }
 
195
  }
196
 
197
  }
 
22
  {
23
 
24
  // see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyunV2.js
25
+ // see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyun.py
26
 
27
  // // https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeadvanced#api-detail-35
28
  // https://github.com/aliyun/alibabacloud-csharp-sdk
 
97
  {
98
  if (_client == null)
99
  {
 
 
100
  string pth_config = Path.Combine(Constant.ExecutablePath, "config.json");
101
  if (!System.IO.File.Exists(pth_config))
102
  {
103
+ return "";
104
  }
105
 
106
  string cf = File.ReadAllText(pth_config);
 
119
  Credential = credential
120
  };
121
  _client = new AlibabaCloud.SDK.Ocr_api20210707.Client(config);
122
+ }
123
 
124
+ byte[] bytes = null;
125
+ using (FileStream stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
126
+ using (BinaryReader reader = new BinaryReader(new BufferedStream(stream)))
127
+ {
128
+ bytes = reader.ReadBytes(Convert.ToInt32(stream.Length));
 
 
 
 
 
 
 
129
  }
130
 
131
+ Stream strm = new MemoryStream(bytes);
132
+
133
+ AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeAdvancedRequest recognizeAdvancedRequest = new AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeAdvancedRequest
134
+ {
135
+ Body = strm,
136
+ OutputCharInfo = true,
137
+ NeedSortPage = true,
138
+ NeedRotate = true
139
+ };
140
+ AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
141
  try
142
  {
143
+ AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeAdvancedResponse resp = _client.RecognizeAdvancedWithOptions(recognizeAdvancedRequest, runtime);
144
+ int? StatusCode = resp.StatusCode;
145
+ string data = resp.Body.Data;
146
 
147
+ if (StatusCode == 200)
 
 
 
 
 
148
  {
149
+ JObject tmp = JObject.Parse(data);
150
+ return data;
151
  }
152
+ else
153
  {
 
154
  return "";
155
  }
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  }
158
+ catch (Exception error)
159
  {
160
+ Console.WriteLine(error.Message);
161
+ return "";
162
  }
163
+
164
  }
165
 
166
  }