StrokesPlus.net Forum
»
General Discussion
»
Scripts
»
A script to snip screen area and use Baidu OCR API to recognize the text
Rank: Member
Groups: Approved
Joined: 11/22/2020(UTC) Posts: 7 Was thanked: 7 time(s) in 2 post(s)
|
Replace clientId and clientSecret with your own keys. Baidu OCR documents: https://ai.baidu.com/tech/ocrCode:var clientId = "5Pkb*************CqL";
var clientSecret = "fRoXM****************qydekZ";
sp.Sleep(100);
//Create a new Bitmap in memory
var memoryImage = new drawing.System.Drawing.Bitmap(action.Bounds.Width, action.Bounds.Height);
//Create a graphics object associated with the bitmap
var memoryGraphics = drawing.System.Drawing.Graphics.FromImage(memoryImage);
//Copy the screen within the bounding rectangle of the drawn gesture area
//I used a square gesture since that seems more intuitive, but it's not neccessary
memoryGraphics.CopyFromScreen(action.Bounds.X, action.Bounds.Y, 0, 0, action.Bounds.Size);
var ms = new clr.System.IO.MemoryStream();
memoryImage.Save(ms, drawing.System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
var base64Image = clr.System.Convert.ToBase64String(ms.GetBuffer());
memoryGraphics.Dispose();
memoryImage.Dispose();
ms.Dispose()
var authHost = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret;
var client = new http.System.Net.Http.HttpClient();
var json = client.GetStringAsync(authHost);
var obj = JSON.parse(json.Result);
client.Dispose();
if (obj.access_token) {
var token = obj.access_token;
var ocrHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + token;
var request = clr.System.Net.WebRequest.Create(ocrHost);
request.Method = "post";
var encoding = clr.System.Text.Encoding.Default;
var paraString = "image=" + clr.System.Net.WebUtility.UrlEncode(base64Image);
var buffer = encoding.GetBytes(paraString);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
var reader = new clr.System.IO.StreamReader(response.GetResponseStream(), clr.System.Text.Encoding.UTF8);
var result = reader.ReadToEnd();
var json = JSON.parse(result);
var jsonArray = json.words_result;
if (jsonArray) {
var OCRText = "";
for(var i=0; i<jsonArray.length; i++) {
OCRText += jsonArray[i].words
}
if (OCRText.length) {
clip.SetText(OCRText);
clr.System.Media.SystemSounds.Asterisk.Play();
}
}
else {
clip.SetText(result);
}
response.Dispose();
reader.Dispose();
}
Edited by user Sunday, March 7, 2021 7:31:42 AM(UTC)
| Reason: Not specified
|
4 users thanked Jack Ma for this useful post.
|
|
|
Rank: Advanced Member
Groups: Approved
Joined: 9/26/2018(UTC) Posts: 73 Location: 北京 Thanks: 18 times Was thanked: 1 time(s) in 1 post(s)
|
I thank you very much for your script, but there is an error when my computer runs this script. What is the reason for this?
|
|
|
|
Rank: Member
Groups: Approved
Joined: 11/22/2020(UTC) Posts: 7 Was thanked: 7 time(s) in 2 post(s)
|
Originally Posted by: liuchina I thank you very much for your script, but there is an error when my computer runs this script. What is the reason for this? I can't load the image, can you paste the error message?
|
|
|
|
Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
Originally Posted by: liuchina I thank you very much for your script, but there is an error when my computer runs this script. What is the reason for this? 我也看不到图片,添加了百度OCR接口,可以运行哦,就是执行后没有提示,直接保存到剪切板了
|
|
|
|
Rank: Advanced Member
Groups: Approved
Joined: 5/6/2019(UTC) Posts: 111 Thanks: 19 times Was thanked: 1 time(s) in 1 post(s)
|
Dear Jack Ma, Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better.
|
|
|
|
Rank: Member
Groups: Approved
Joined: 11/22/2020(UTC) Posts: 7 Was thanked: 7 time(s) in 2 post(s)
|
Originally Posted by: 2014218866 Dear Jack Ma, Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better. I have updated the script to include the condition.
|
|
|
|
Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
Originally Posted by: Jack Ma Originally Posted by: 2014218866 Dear Jack Ma, Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better. I have updated the script to include the condition. 大佬, 1 如何通过其他快捷键截图,比如win10自带win+shift+s,调取剪切板图片然后识图呀 2 能否调取剪切板的文字,然后使用google或者百度API翻译,结果再保存到剪切板 识图的这样试了下发现不行 Code:
sp.Sleep(100);
sp.SendModifiedVKeys([vk.LSHIFT,vk.LWIN], [vk.VK_S]);
sp.Sleep(1000);
var ms = new clr.System.IO.MemoryStream();
var Img = clip.GetImage();
Img.Save(ms, ImageFormat.Png);
ms.Position = 0;
var base64Image = clr.System.Convert.ToBase64String(ms.GetBuffer());
ms.Dispose()
|
|
|
|
Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
Originally Posted by: soooulp Originally Posted by: Jack Ma Originally Posted by: 2014218866 Dear Jack Ma, Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better. I have updated the script to include the condition. 大佬, 1 如何通过其他快捷键截图,比如win10自带win+shift+s,调取剪切板图片然后识图呀 2 能否调取剪切板的文字,然后使用google或者百度API翻译,结果再保存到剪切板 识图的这样试了下发现不行
Code:
sp.Sleep(100);
sp.SendModifiedVKeys([vk.LSHIFT,vk.LWIN], [vk.VK_S]);
sp.Sleep(1000);
var ms = new clr.System.IO.MemoryStream();
var Img = clip.GetImage();
Img.Save(ms, ImageFormat.Png);
ms.Position = 0;
var base64Image = clr.System.Convert.ToBase64String(ms.GetBuffer());
ms.Dispose()
The first one is solved by the post https://forum.strokesplus.net/posts/t8263Thank you, Jack Ma.
|
|
|
|
Rank: Member
Groups: Approved
Joined: 11/22/2020(UTC) Posts: 7 Was thanked: 7 time(s) in 2 post(s)
|
Originally Posted by: soooulp Originally Posted by: Jack Ma Originally Posted by: 2014218866 Dear Jack Ma, Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better. I have updated the script to include the condition. 大佬, 1 如何通过其他快捷键截图,比如win10自带win+shift+s,调取剪切板图片然后识图呀 2 能否调取剪切板的文字,然后使用google或者百度API翻译,结果再保存到剪切板 识图的这样试了下发现不行 Code:
sp.Sleep(100);
sp.SendModifiedVKeys([vk.LSHIFT,vk.LWIN], [vk.VK_S]);
sp.Sleep(1000);
var ms = new clr.System.IO.MemoryStream();
var Img = clip.GetImage();
Img.Save(ms, ImageFormat.Png);
ms.Position = 0;
var base64Image = clr.System.Convert.ToBase64String(ms.GetBuffer());
ms.Dispose()
百度翻译应该也有文档,调用方法都类似,不过用浏览器扩展沙拉查词的话很方便。
|
|
|
|
StrokesPlus.net Forum
»
General Discussion
»
Scripts
»
A script to snip screen area and use Baidu OCR API to recognize the text
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The StrokesPlus.net Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close