/
gcvocr.py
65 lines (57 loc) · 2.02 KB
/
gcvocr.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
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import base64
import json
import os
import codecs
import sys
from requests import Request, Session
from io import BytesIO
from PIL import Image
args = sys.argv
filename = args[1]
APIKEY = args[2]
savedir = args[3]
def pil_image_to_base64(pil_image):
buffered = BytesIO()
pil_image.save(buffered, format="PNG")
str_encode_file = base64.b64encode(buffered.getvalue()).decode("utf-8")
return str_encode_file
def recognize_image(pil_image):
str_encode_file = pil_image_to_base64(pil_image)
str_url = "https://vision.googleapis.com/v1/images:annotate?key="
str_api_key = APIKEY
str_headers = {"Content-Type": "application/json"}
str_json_data = {
"requests": [
{
"image": {
"content": str_encode_file
},
"features": [
{
"type": "TEXT_DETECTION",
"maxResults": 2048
}
]
}
]
}
obj_session = Session()
obj_request = Request("POST",
str_url + str_api_key,
data=json.dumps(str_json_data),
headers=str_headers
)
obj_prepped = obj_session.prepare_request(obj_request)
obj_response = obj_session.send(obj_prepped,
verify=True,
# timeout=60
)
if obj_response.status_code == 200:
with codecs.open(savedir + "/" + os.path.basename(filename) + ".json", "w", "utf-8") as outfile:
dump=json.dumps(obj_response.json(), indent=2, ensure_ascii=False)
outfile.write(dump)
if __name__ == "__main__":
image_path = filename
pil_image = Image.open(image_path)
recognize_image(pil_image)