baidu OCR

百度OCR文字识别API for Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, urllib, urllib2, json
f = open("test.txt","w")
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" #`2`代表图片原文件(只支持JPG)
data['image'] = "" ##image base64 data
decoded_data = urllib.urlencode(data)
req = urllib2.Request(url, data = decoded_data)
req.add_header("Content-Type", "application/x-www-form-urlencoded")
req.add_header("apikey", "自己的apikey")
resp = urllib2.urlopen(req)
content = resp.read()
if(content):
print(content)
f.write(content)

image convert to base64 code

1
2
3
4
5
openssl base64 < image.jpg | tr -d '\n' | pbcopy
cat image.jpg | openssl base64 | tr -d '\n' | pbcopy
openssl base64 -in out.jpg -out filename.b64
  • data:image/png;base64,{base64 image code}//直接复制到浏览器地址栏回车
  • <img src="data:image/jpg;base64,{base64 image code}/>//或者网页里嵌入

References: