-
Notifications
You must be signed in to change notification settings - Fork 1
/
baiduocr.py
58 lines (52 loc) · 1.65 KB
/
baiduocr.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
import sys, urllib, json
import base64
import urllib.parse
import urllib.request
def get_image_data():
'''
# local image
with open("chineseVal.jsp", 'rb') as f:
jpg_data = f.read()
jpg_data = base64.b64encode(jpg_data)
#print(content)
'''
image_addr = "http://zzxh.zjsgat.gov.cn:6081/zjwwzzxh/include/chineseVal.jsp"
req = urllib.request.Request(image_addr)
resp = urllib.request.urlopen(req)
jpg_data = resp.read()
jpg_data = base64.b64encode(jpg_data)
print(jpg_data)
return jpg_data
def get_words_from_img(img_data):
ocr_url = 'http://apis.baidu.com/apistore/idlocr/ocr'
post_data = {
'fromdevice' : "pc",
'clientip' : "10.10.10.0",
'detecttype' : "LocateRecognize",
'languagetype' : "CHN_ENG",
'imagetype' : "1",
'image' : img_data,
}
post_data = urllib.parse.urlencode(post_data).encode('utf-8')
req = urllib.request.Request(ocr_url, data = post_data)
req.add_header("Content-Type", "application/x-www-form-urlencoded")
req.add_header("apikey", "04d3977f3751c9ba2e7fb2663059cb94")
resp = urllib.request.urlopen(req)
content = resp.read()
if content:
print(content)
content = eval(content.decode('utf-8'))
if content["errNum"] == "0":
try:
words = content["retData"][0]["word"]
print(words)
except:
return None
else:
return None
return words
def get_words():
img_data = get_image_data()
return get_words_from_img(img_data)
# get_words()