- 帖子
- 997
- 积分
- 1789
- 技术
- 205
- 捐助
- 50
- 注册时间
- 2016-4-17
|
[原创] OCRImage-图片云识别工具(百度云版)
最近正好要批量OCR一些PDF提取出来的图片,就写了一个,
用的是百度云OCR接口,腾讯云倒是也有这种接口,不过比较麻烦,还要实名认证,懒的折腾~
如果可以的话,希望可以放到第三方库里面@CrLf。
用法如下:
老刘封装——图像云OCR工具(百度云版,支持中英文混排)
用法:
CSCRIPT -NOLOGO OCRImage.VBS <APIKey> <SecretKey> <ImageFilePath>
APIKey 你创建的百度云人工智能应用所附带的APIKey
SecretKey 你创建的百度云人工智能应用所附带的SecretKey
ImageFilePath 图像文件的路径(支持相对路径)
百度云人工智能OCR: https://cloud.baidu.com/product/ocr
输出信息:
句柄1 OCR结果
句柄2 帮助、错误信息、执行耗时
注意:
一、不支持XP系统。
二、图片大小必须小于3M,建议小于1M。否则就会非常慢(VBS的锅,下次换Vb.Net),而且识别效果极差甚至无法识别(这是百度云的锅)。
大家如果懒得注册百度云,添加应用,就先拿去用吧,反正5w次LZ用不完^_^
TsjtQn5zM9jjptHFjlOPwfQp kQnaefbF0PmAQOruq7hBbQZBd1MtWVl1
码云链接:https://gitee.com/OldLiu001/codes/mwcqbtel2po8uf54xrzkh10
GitHub_Gist链接:
源码如下(ImageOCR.VBS,使用自制工具VbsHighLight-Vbs代码着色工具上色):
——————————————————
Rem CODE BY OLDLIU
Option Explicit
Randomize
Dim ExitCode
ExitCode = Main()
WSH.Quit ExitCode
Function Main()
Const OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
If WSH.Arguments.Count = 3 Then
Dim fso,APIKey,SecretKey,Start_Time
Start_Time = Timer
Set fso = CreateObject("Scripting.FileSystemObject")
APIKey = WSH.Arguments(0)
SecretKey = WSH.Arguments(1)
If fso.FileExists(WSH.Arguments(2)) Then
Dim ImageFilePath,Token
ImageFilePath = fso.GetFile(WSH.Arguments(2)).Path '处理相对路径
Token = GetToken(APIKey,SecretKey)
If Token <> "ERROR" Then
With CreateObject("HTMLFILE")
.ParentWindow.execScript _
"var ImageBase64String_URLEnc = encodeURIComponent('" & FileBase64Enc(ImageFilePath) & "');"
If Len(.ParentWindow.ImageBase64String_URLEnc) <= 4*1024*1024 Then
.ParentWindow.execScript _
"var JSON = " & _
HTTP_POST( _
OCR_URL & "?access_token=" & Token, _
"image="& .ParentWindow.ImageBase64String_URLEnc & _
"&language_type=CHN_ENG" & _
"&detect_direction=false" & _
"&detect_language=false" _
)
On Error Resume Next
Dim Error_Code
Error_Code = .ParentWindow.JSON.error_code
If Error_Code = Empty Then
On Error Goto 0
Dim i
For i = 0 To .ParentWindow.JSON.words_result_num - 1
WSH.Echo Eval(".ParentWindow.JSON.words_result.[" & i & "].words")
Next
Main = 0
WSH.StdErr.WriteLine "成功,用时 " & Timer - Start_Time & " sec。"
Else
On Error Goto 0
Main = 5
WSH.StdErr.WriteLine "Error! Code:" & Error_Code & ", msg:" & .ParentWindow.JSON.error_msg
End If
Else
Main = 4
WSH.StdErr.WriteLine "文件过大。"
End If
End With
Else
Main = 3
WSH.StdErr.WriteLine "APIKey或SecretKey有误。"
End If
Else
Main = 2
WSH.StdErr.WriteLine "文件未找到。"
End If
Else
Main = 1
WSH.StdErr.WriteLine "老刘封装——图像云OCR工具(百度云版,支持中英文混排)" & vbNewLine & vbNewLine & _
"用法:" & vbNewLine & _
"CSCRIPT -NOLOGO OCRImage.VBS <APIKey> <SecretKey> <ImageFilePath>" & vbNewLine & vbNewLine & _
"APIKey 你创建的百度云人工智能应用所附带的APIKey" & vbNewLine & _
"SecretKey 你创建的百度云人工智能应用所附带的SecretKey" & vbNewLine & _
"ImageFilePath 图像文件的路径(支持相对路径)"& vbNewLine & vbNewLine & _
"百度云人工智能OCR: https://cloud.baidu.com/product/ocr" & vbNewLine & vbNewLine & _
"输出信息:" & vbNewLine & _
"句柄1 OCR结果" & vbNewLine & _
"句柄2 帮助、错误信息、执行耗时" & vbNewLine & vbNewLine & _
"注意:" & vbNewLine & _
"一、不支持XP系统。" & vbNewLine & _
"二、图片大小必须小于3M,建议小于1M。否则就会非常慢(VBS的锅,下次换Vb.Net),而且识别效果极差甚至无法识别(这是百度云的锅)。"
End If
End Function
Function HTTP_POST(ByVal Address,ByVal Args)
With CreateObject("MSXML2.XMLHTTP")
.Open "POST",Address, False
.SetRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
.Send Args
HTTP_POST = .ResponseText
End With
End Function
Function GetToken(ByVal APIKey,ByVal SecretKey)
Const GET_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token"
On Error Resume Next
GetToken = _
Split( _
HTTP_POST( _
GET_TOKEN_URL, _
"grant_type=client_credentials" & _
"&client_id="&APIKey & _
"&client_secret=" & SecretKey _
) _
,"""")(13)
If Err.Number <> 0 Then GetToken = "ERROR"
On Error Goto 0
End Function
Function FileBase64Enc(ByVal strFilePath)
Dim fso,WshShell,TmpFilePath,oReadFile
Const ForReading = 1
Const MyBirthDay = 1228
Const WINDOW_HIDE = 0
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
TmpFilePath = fso.GetSpecialFolder(2) & "\" & Fix(Rnd * MyBirthDay) & ".TMP"
WshShell.Run "Certutil.EXE -ENCODE """ & strFilePath & """ """ & TmpFilePath & """" ,WINDOW_HIDE ,True
Set oReadFile = fso.OpenTextFile(TmpFilePath,ForReading)
oReadFile.ReadLine '跳过首行的”-----BEGIN CERTIFICATE-----“标志
Dim Str
While Str <> "-----END CERTIFICATE-----"
FileBase64Enc = FileBase64Enc & Str
Str = oReadFile.ReadLine
Wend
oReadFile.Close
fso.DeleteFile TmpFilePath
End Function
——————————————————
如果这个工具帮到了您,您可以赞助我们,您的赞助就是我们最大的动力!
个人赞助:
[attach]11267[/attach] |
-
3
评分人数
-
|