ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 181|回复: 8

[分享] VBA-WEB库应用之文字识别API(百度云OCR、腾讯云OCR)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-9 14:10 | 显示全部楼层 |阅读模式
本帖最后由 gnefnuy 于 2024-5-9 17:35 编辑

发现一个封装好的WEB操作库,推荐给大家
VBA-Web | VBA-Web (vba-tools.github.io)

应用平台
OCR文字识别_免费试用_图片转文字-百度AI开放平台 (baidu.com)
通用文字识别_图片文字识别_印刷体文字识别-腾讯云 (tencent.com)
我的技术很菜,不喜欢自己造轮子
百度的调用比较简单,创建了一个BaiduAipOcrSDK类模块,token的保持功能我没有写……
WebHelpers模块中添加修改了部分模块,网上有很多的范例,Base64图片文件、UTF8转ANSI等

  1. <blockquote>'这里只能显示一行,删不掉了
复制代码
调用方式是
  1. Private Sub TestOcr01()
  2.     Dim result As New Dictionary
  3.     Dim tempPath As String
  4.     tempPath = "D:\a.jpg"
  5.         Dim bdClient As New BaiduAipOcrSDK
  6.         bdClient.Setup "你的百度云SecretId", "你的百度云SecretKey"
  7.         Set result = bdClient.VehicleLicenseFile(tempPath)
  8. debug.print WebHelpers.ConvertToJson(result, 4)
  9. End Sub
复制代码
腾讯云的比较复杂,有很多坑,我编写了的签名方法v3的版本,调用方式与上面一致,如果大家感兴趣,我再放到本贴中。

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-9 14:25 | 显示全部楼层
本帖最后由 gnefnuy 于 2024-5-9 17:39 编辑

[ 本帖最后由 gnefnuy 于 2024-5-9 17:38 编辑 ]\n\n第一段代码被吞了?放这楼吧,或者版主帮助编辑一下,谢谢

  1. ' 实现百度云OCR的支持
  2. ' gnefnuy 2024.5.9
  3. Option Explicit

  4. Private Const BASE_URL As String = "https://aip.baidubce.com/rest/2.0"
  5. Private Const OCR_URL As String = BASE_URL & "/ocr/v1/"
  6. Private Const IMAGE_CLASSIFY_URL As String = BASE_URL & "/image-classify/v1/"

  7. Public apiKey As String
  8. Public secretKey As String

  9. Public Sub Setup(apiKey As String, secretKey As String)
  10.     Me.apiKey = apiKey
  11.     Me.secretKey = secretKey
  12. End Sub

  13. ' 使用 AK,SK 生成鉴权签名(Access Token),有效期30天,需加入避免重复获取
  14. Public Function GetAccessToken() As String
  15.     Dim token As String
  16.     ' TODO:先获取本地数据,是否有Token,如果没有就去平台获取
  17.     token = "*****可以用30天************"
  18.     GetAccessToken = token: Exit Function
  19.     Dim client As New WebClient
  20.     Dim request As New WebRequest
  21.     Dim response As WebResponse
  22.    
  23.     With request
  24.         .Resource = "https://aip.baidubce.com/oauth/2.0/token"
  25.         .Method = WebMethod.HttpPost
  26.         .AddQuerystringParam "grant_type", "client_credentials"
  27.         .AddQuerystringParam "client_id", Me.apiKey
  28.         .AddQuerystringParam "client_secret", Me.secretKey
  29.     End With
  30.     Set response = client.Execute(request)
  31.     GetAccessToken = response.data("access_token")
  32. End Function
  33. ' 通过本地行驶证图片文件识别
  34. Public Function VehicleLicenseFile(Imgpath As String) As Dictionary
  35.     Set VehicleLicenseFile = OCR("vehicle_license", Imgpath)
  36. End Function
  37. ' 通过行驶证图片网络地址识别
  38. Public Function VehicleLicenseUrl(ImgUrl As String) As Dictionary
  39.     Set VehicleLicenseUrl = OCR("vehicle_license", ImgUrl, 2)
  40. End Function
  41. ' 通过本地驾驶证图片文件识别
  42. Public Function DrivingLicenseFile(Imgpath As String) As Dictionary
  43.     Set DrivingLicenseFile = OCR("driving_license", Imgpath)
  44. End Function
  45. ' 通过本地图片识别车牌
  46. Public Function LicensePlateFile(Imgpath As String) As Dictionary
  47.     Set LicensePlateFile = OCR("license_plate", Imgpath)
  48. End Function
  49. ' 通过本地图片识别VIN
  50. Public Function VinCodeFile(Imgpath As String) As Dictionary
  51.     Set VinCodeFile = OCR("vin_code", Imgpath)
  52. End Function
  53. ' 通过本地图片识别机动车销售发票
  54. Public Function VehicleInvoiceFile(Imgpath As String) As Dictionary
  55.     Set VehicleInvoiceFile = OCR("vehicle_invoice", Imgpath)
  56. End Function
  57. ' 通过本地图片识别身份证
  58. Public Function IdcardFile(Imgpath As String) As Dictionary
  59.     Set IdcardFile = OCR("idcard", Imgpath)
  60. End Function

  61. ' 识别器
  62. ' OcrType: 识别类别,按照供应商提供的数据来
  63. ' UriType: 0->本地文件路径(默认),2->网络地址,3->BASE64
  64. ' body: 对应的相应参数,默认为空
  65. Private Function OCR(OcrType As String, Uri As String, Optional UriType As Integer = 0, Optional body As Dictionary = Nothing) As Dictionary
  66.     Dim client As New WebClient
  67.     Dim request As New WebRequest
  68.     Dim response As WebResponse
  69.     Dim body_ As New Dictionary
  70.     Dim result As New Dictionary
  71.    
  72.     Set body_ = body
  73.     ' 设置请求参数
  74.     With request
  75.         .Resource = OCR_URL & OcrType & "?access_token=" & GetAccessToken()
  76.         .Method = WebMethod.HttpPost
  77.         .AddHeader "Content-Type", "application/x-www-form-urlencoded"
  78.         .AddHeader "Accept", "application/json"
  79.         .RequestFormat = FormUrlEncoded
  80.     End With
  81.    
  82.     Select Case UriType
  83.         Case 0
  84.             body_.Add "image", WebHelpers.Base64EncodeFile(Uri)  '这里我自己添加的Base64EncodeFile
  85.         Case 1
  86.             body_.Add "url", Uri
  87.         Case 2
  88.             body_.Add "image", Uri
  89.     End Select
  90.     ' 将请求正文添加到请求中
  91.     Set request.body = body_
  92.     ' 发送请求并获取响应
  93.     Set response = client.Execute(request)
  94.    Dim aStr As String
  95.     aStr = DecodeToBytes(response.body) '将返回的UTF8字符转为VB可显示的ANSI字符,我自己加的
  96.     'aStr = WebHelpers.StringToAnsiBytes(response.body)'或者试试这个自带的
  97.     Set result = WebHelpers.ParseJson(aStr)' 我去掉了非ASCII字符的编码,中文不会输出为\u****类的编码
  98.      ' 输出响应内容
  99.    Set OCR = result
  100. End Function
复制代码

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-5-10 20:46 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-5-10 21:00 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-5-11 09:34 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-5-11 10:24 | 显示全部楼层
挺好的,现在百度云收费贵吗

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-11 11:07 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-11 11:09 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
兰色的回忆 发表于 2024-5-11 10:24
挺好的,现在百度云收费贵吗

目前OCR类别的百度云和腾讯云都有1000的额度每月,阿里云100个免费额度,我自己用用够了,大项目估计不需要VBA了

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-11 11:32 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
占个楼,等会儿直接传文件吧,省了字数超限
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-16 15:02 , Processed in 0.046246 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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