ali ocr not done yet.
Browse files- src/WpfEditor/AliOcr.cs +49 -0
- src/WpfEditor/MainWindow.xaml.cs +8 -1
- src/WpfEditor/WpfEditor.csproj +12 -3
- src/WpfEditor/global.cs +5 -5
src/WpfEditor/AliOcr.cs
CHANGED
|
@@ -10,6 +10,12 @@ using System.Windows.Controls;
|
|
| 10 |
using System.Net.Http;
|
| 11 |
using Newtonsoft.Json;
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
using Formatting = Newtonsoft.Json.Formatting;
|
| 14 |
|
| 15 |
namespace WpfEditor
|
|
@@ -17,6 +23,7 @@ namespace WpfEditor
|
|
| 17 |
|
| 18 |
// see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyunV2.js
|
| 19 |
|
|
|
|
| 20 |
// https://github.com/aliyun/alibabacloud-csharp-sdk
|
| 21 |
// https://github.com/aliyun/alibabacloud-csharp-sdk/blob/master/ocr-api-20210707/core/Client.cs
|
| 22 |
// dotnet add package AlibabaCloud.SDK.Ocr-api20210707
|
|
@@ -25,6 +32,9 @@ namespace WpfEditor
|
|
| 25 |
{
|
| 26 |
private static readonly HttpClient httpClient = new HttpClient();
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
private static async Task<string> ocrAsync(string api, string appCode, string imgData)
|
| 29 |
{
|
| 30 |
try
|
|
@@ -84,7 +94,46 @@ namespace WpfEditor
|
|
| 84 |
|
| 85 |
public static string RecognizeImage(string imagePath)
|
| 86 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
try
|
| 90 |
{
|
|
|
|
| 10 |
using System.Net.Http;
|
| 11 |
using Newtonsoft.Json;
|
| 12 |
|
| 13 |
+
using System.Collections;
|
| 14 |
+
using System.Collections.Generic;
|
| 15 |
+
using System.IO;
|
| 16 |
+
using Tea;
|
| 17 |
+
using Tea.Utils;
|
| 18 |
+
|
| 19 |
using Formatting = Newtonsoft.Json.Formatting;
|
| 20 |
|
| 21 |
namespace WpfEditor
|
|
|
|
| 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
|
| 28 |
// https://github.com/aliyun/alibabacloud-csharp-sdk/blob/master/ocr-api-20210707/core/Client.cs
|
| 29 |
// dotnet add package AlibabaCloud.SDK.Ocr-api20210707
|
|
|
|
| 32 |
{
|
| 33 |
private static readonly HttpClient httpClient = new HttpClient();
|
| 34 |
|
| 35 |
+
private static AlibabaCloud.SDK.Ocr_api20210707.Client _client = null;
|
| 36 |
+
|
| 37 |
+
|
| 38 |
private static async Task<string> ocrAsync(string api, string appCode, string imgData)
|
| 39 |
{
|
| 40 |
try
|
|
|
|
| 94 |
|
| 95 |
public static string RecognizeImage(string imagePath)
|
| 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);
|
| 108 |
+
var cfg = JObject.Parse(cf);
|
| 109 |
+
string access_key_id = cfg["aliOCR"]["myOCRAccessKeyID"].Value<string>();
|
| 110 |
+
string access_key_secret = cfg["aliOCR"]["myOCRAccessKeySecret"].Value<string>();
|
| 111 |
+
|
| 112 |
|
| 113 |
+
Aliyun.Credentials.Client credential = new Aliyun.Credentials.Client();
|
| 114 |
+
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
|
| 115 |
+
{
|
| 116 |
+
Type = "access_key",
|
| 117 |
+
AccessKeyId = access_key_id,
|
| 118 |
+
AccessKeySecret = access_key_secret,
|
| 119 |
+
Endpoint = "ocr-api.cn-hangzhou.aliyuncs.com",
|
| 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 |
{
|
src/WpfEditor/MainWindow.xaml.cs
CHANGED
|
@@ -47,6 +47,13 @@ namespace WpfEditor
|
|
| 47 |
ReviseEdit
|
| 48 |
}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
public class CursorPositionChangedEventArgs : EventArgs
|
| 51 |
{
|
| 52 |
public int Line { get; set; }
|
|
@@ -1058,7 +1065,7 @@ namespace WpfEditor
|
|
| 1058 |
|
| 1059 |
//OnOpenImage_Click(openFileDialog.FileName);
|
| 1060 |
|
| 1061 |
-
var (jsn, m5, pth_img, pth_json) = Global.img_md5_jsn(openFileDialog.FileName);
|
| 1062 |
|
| 1063 |
// 加载原图
|
| 1064 |
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
|
|
|
| 47 |
ReviseEdit
|
| 48 |
}
|
| 49 |
|
| 50 |
+
public enum OcrMode
|
| 51 |
+
{
|
| 52 |
+
WeChatOcr,
|
| 53 |
+
AliOCR,
|
| 54 |
+
PPOCR
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
public class CursorPositionChangedEventArgs : EventArgs
|
| 58 |
{
|
| 59 |
public int Line { get; set; }
|
|
|
|
| 1065 |
|
| 1066 |
//OnOpenImage_Click(openFileDialog.FileName);
|
| 1067 |
|
| 1068 |
+
var (jsn, m5, pth_img, pth_json) = Global.img_md5_jsn(openFileDialog.FileName, OcrMode.AliOCR);
|
| 1069 |
|
| 1070 |
// 加载原图
|
| 1071 |
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
src/WpfEditor/WpfEditor.csproj
CHANGED
|
@@ -32,8 +32,17 @@
|
|
| 32 |
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
| 33 |
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
| 34 |
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" />
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
</ItemGroup>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
</Project>
|
|
|
|
| 32 |
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
| 33 |
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
| 34 |
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" />
|
| 35 |
+
<PackageReference Include="OpenCvSharp4.Windows" Version="4.11.0.20250507" />
|
| 36 |
+
<PackageReference Include="OpenCvSharp4.WpfExtensions" Version="4.11.0.20250507" />
|
| 37 |
+
</ItemGroup>
|
| 38 |
+
|
| 39 |
+
<ItemGroup>
|
| 40 |
+
<PackageReference Include="AlibabaCloud.SDK.Ocr-api20210707" Version="3.1.3"/>
|
| 41 |
+
<PackageReference Include="AlibabaCloud.OpenApiClient" Version="0.1.15"/>
|
| 42 |
+
<PackageReference Include="AlibabaCloud.TeaConsole" Version="0.1.0"/>
|
| 43 |
+
<PackageReference Include="AlibabaCloud.TeaUtil" Version="0.1.19"/>
|
| 44 |
+
<PackageReference Include="Aliyun.Credentials" Version="1.5.2"/>
|
| 45 |
+
<PackageReference Include="Tea" Version="1.1.3"/>
|
| 46 |
+
</ItemGroup>
|
| 47 |
|
| 48 |
</Project>
|
src/WpfEditor/global.cs
CHANGED
|
@@ -236,7 +236,7 @@ namespace WpfEditor
|
|
| 236 |
return jsn_new;
|
| 237 |
}
|
| 238 |
|
| 239 |
-
public static Tuple<JObject, string, string, string> img_md5_jsn(string pthimg,
|
| 240 |
{
|
| 241 |
|
| 242 |
string m5 = MD5.ComputeFileHash(pthimg);
|
|
@@ -247,15 +247,15 @@ namespace WpfEditor
|
|
| 247 |
{
|
| 248 |
string result = "";
|
| 249 |
|
| 250 |
-
if (ocrEngine ==
|
| 251 |
{
|
| 252 |
result = WeChatOcr.RecognizeImage(pthimg);
|
| 253 |
}
|
| 254 |
-
else if (ocrEngine ==
|
| 255 |
{
|
| 256 |
-
|
| 257 |
}
|
| 258 |
-
else if (ocrEngine ==
|
| 259 |
{
|
| 260 |
|
| 261 |
}
|
|
|
|
| 236 |
return jsn_new;
|
| 237 |
}
|
| 238 |
|
| 239 |
+
public static Tuple<JObject, string, string, string> img_md5_jsn(string pthimg, OcrMode ocrEngine = OcrMode.WeChatOcr)
|
| 240 |
{
|
| 241 |
|
| 242 |
string m5 = MD5.ComputeFileHash(pthimg);
|
|
|
|
| 247 |
{
|
| 248 |
string result = "";
|
| 249 |
|
| 250 |
+
if (ocrEngine == OcrMode.WeChatOcr)
|
| 251 |
{
|
| 252 |
result = WeChatOcr.RecognizeImage(pthimg);
|
| 253 |
}
|
| 254 |
+
else if (ocrEngine == OcrMode.AliOCR)
|
| 255 |
{
|
| 256 |
+
result = AliOcr.RecognizeImage(pthimg);
|
| 257 |
}
|
| 258 |
+
else if (ocrEngine == OcrMode.PPOCR)
|
| 259 |
{
|
| 260 |
|
| 261 |
}
|