上一篇写的,OCR调用是失败的
后来仔细研究过,用base64编码,最后成功了.
另外一个问题,百度原来提供两种方式传递原图片,但是只有第一种,即图片base64编码后上传才有效,第二种上传原文件,怎么都不成功.后来想到把在线的图片读到缓存再base64编码,用第一种方式传递图片,结果可行.
另 安装 simplejson
,然后 import simplejson as json
也直接可用 ,而且,.decode()
都可以不需要.比原本的json
好用.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@Author : BigBro
#@DateTime : 2015-11-21 16:41:31
#@Filename : baidu_ocr_ver1.0.py
#@Description : 百度 图像 ocr
import sys, urllib, json
import urllib.request
import urllib.parse
import base64
url = 'http://apis.baidu.com/apistore/idlocr/ocr'
data = {}
data['fromdevice'] = "pc"
data['clientip'] = "10.10.10.0"
data['detecttype'] = "LocateRecognize"
data['languagetype'] = "CHN_ENG"
data['imagetype'] = "1"
#图片在本地
with open(r'C:\Users\bigbrolee\Desktop\abc.jpg',mode = 'rb') as f:
img = f.read()
#图片在网络
# img_url = 'https://pic4.zhimg.com/b8189c1ac3a2d5b790d8bc9bf97f41d7_l.jpg'
# try:
# resp_img=urllib.request.urlopen(img_url, timeout=5)
# img=resp_img.read()
# except socket.timeout:
# raise socket.timeout
data['image'] =base64.b64encode(img)
decoded_data = urllib.parse.urlencode(data)
decoded_data = decoded_data.encode('utf-8')
# #print(decoded_data)
req = urllib.request.Request(url,decoded_data)
req.add_header("Content-Type", "application/x-www-form-urlencoded")
req.add_header("apikey", "f46299494a15949102e3d040fdbd5bbc") #a63fd60067141f18cffa9af6a1563b4e
resp = urllib.request.urlopen(req)#, data = decoded_data)
content = resp.read()
if(content):
content = json.loads(content.decode())
print(content)
网友评论