百度通用文字识别 image format error (216201)错误解决

根据文档说明使用base64的图片发送,结果出现错误

 {

    "error_code" = 216201;

    "error_msg" = "image format error";

    "log_id" = 9064900028040021949;

}

网上查了好多都没有解决,这里整理一下。

其实原因很简单,相信很多童鞋和我一样都加上了‘data:image/png;base64,’ ,其实是不用的,也不需要urlencode,感觉整个人都不好了,差点就放弃了。。。

有博友说还不行,直接上代码吧~~

wx.chooseImage({
  success: res => {
	wx.getFileSystemManager().readFile({
	  filePath: res.tempFilePaths[0], //选择图片返回的相对路径
	  encoding: 'base64', //编码格式
	  success: res => { //成功的回调
		// 图片
		var image = res.data;
		// 文字识别
		wx.request({
		  url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' + access_token,
		  data: {
			image: image
		  },
		  header: {
			'Content-Type': 'application/x-www-form-urlencoded'
		  },
		  method: 'POST',
		  success(res) {
			console.log(res.data)
		  }
		})
	  }
	})
  }
})

ps:贴上自己做的工具小程序

 

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
您可以使用百度云提供的文字识别 API 来实现Python中的文字识别。首先,您需要在百度云平台上创建一个账号,并获取到文字识别的 API Key 和 Secret Key。 接下来,您可以使用Python中的requests库向百度云 API 发送请求。以下是一个简单的示例代码: ```python import requests # 设置API Key和Secret Key api_key = 'your_api_key' secret_key = 'your_secret_key' # 上传图片并调用文字识别API def baidu_ocr(image_path): url = 'https://aip.baidubce.com/oauth/2.0/token' data = { 'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key } response = requests.post(url, data=data) access_token = response.json().get('access_token') ocr_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic' headers = {'Content-Type': 'application/x-www-form-urlencoded'} params = {'access_token': access_token} image = open(image_path, 'rb').read() response = requests.post(ocr_url, headers=headers, params=params, data=image) result = response.json() if 'error_code' in result: print('文字识别出错:', result['error_msg']) else: for word in result['words_result']: print(word['words']) # 使用示例 image_path = 'path/to/your/image.jpg' baidu_ocr(image_path) ``` 请确保将上述代码中的`your_api_key`和`your_secret_key`替换为您在百度云平台上创建的 API Key 和 Secret Key。同时,将`image_path`替换为您要识别的图片的路径。 这样,您就可以使用Python调用百度云的文字识别 API 来实现图片中文字的识别了。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值