紫猫学院社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 34|回复: 0

[求助] 标注下怎么用

[复制链接]

1

主题

1

帖子

1

积分

学前班

Rank: 1

猫粮
4
发表于 2021-4-8 01:02:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
新手,标注下在哪加API秘钥这些,能正常弹出识别就行
参数1,参数2,去百度云申请,参数3为文件路径,参数4为识别的种类
举例:text_ocr = BaiDuOCR(文字识别APIKey, 文字识别SecretKey, 存储位置 & ".bmp", "通用文字识别")

Function BaiDuOCR(APIKey, SecretKey, ImgPath, OcrType)
    Dim http, ReJson, url, formStr, token,  PhotoBS, httpBody
    Dim xml_dom, Node, FileByteArrs, Stream, ReExpObj, Matches
    Dim adTypeBinary, adModeReadWrite, adTypeText
    adTypeBinary = 1
    adModeReadWrite = 3
    adTypeText = 2
    Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
    Set sStream = CreateObject("ADODB.Stream")

    url = "https://aip.baidubce.com/oauth/2.0/token?"
    formStr = "grant_type=client_credentials&" & _
    "client_id=" & APIKey & "&" & _
    "client_secret=" & SecretKey & "&"

    With http
        .Open "POST", url
        .SetRequestHeader "Conent-Length", Len(formStr)
        .Send formStr
    End With
    ReJson = http.ResponseText


    Set ReExpObj = CreateObject("VBScript.RegExp")
    ReExpObj.[Global] = True
    ReExpObj.Pattern = "access_token"":""(.+?)"""
    If ReExpObj.Test(ReJson) Then
        Set Matches = ReExpObj.Execute(ReJson)
        token = Matches(0).SubMatches(0)
    Else
        MsgBox "获取Token失败!" & vbcrlf & ReJson
    End If

    With sStream
        .Mode = adModeReadWrite
        .Type = adTypeBinary
        .Open
        .LoadFromFile (ImgPath)
        .Position = 0
        FileByteArr = .Read
        .Close
    End With

    Set xml_dom = CreateObject("Microsoft.XMLDOM")
    With xml_dom
        .loadXML ("")
        Set Node = .createElement("HTML")
        With Node
            .dataType = "bin.base64"
            .nodeTypedValue = FileByteArr
            PhotoBS = .Text
        End With
    End With

    PhotoBS = Replace(PhotoBS,"+","%2b")
    PhotoBS = Replace(PhotoBS, "/", "%2f")
    PhotoBS = Replace(PhotoBS,"=","%3d")
    With http
        If OcrType = "通用文字识别" Then
            formStr = "image=" & PhotoBS & "&image_url="
            .Open "POST", "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" & CStr(token), False
        ElseIf OcrType = "通用图像识别" Then
            formStr = "image=" & PhotoBS
            .Open "POST", "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=" & CStr(token), False
        End If
        .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .SetRequestHeader "Conent-Length", Len(formStr)
        .Send formStr
        httpBody = .ResponseBody
    End With

    With sStream
        .Mode = adModeReadWrite
        .Type = adTypeBinary
        .Open
        .Write httpBody
        .Position = 0
        .Type = adTypeText
        .Charset = "utf-8"
        BaiDuOCR = .ReadText
        .Close
    End With

    Set Node = Nothing
    Set xml_dom = Nothing
    Set sStream = Nothing
End Function

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|紫猫编程学院  

GMT+8, 2021-4-24 00:53

Powered by Discuz! X3.4

Copyright © 2012-2020 紫猫编程学院

快速回复 返回顶部 返回列表