Last active
July 17, 2018 09:33
-
-
Save umihico/784ad69c5c79560e9928f265535989f5 to your computer and use it in GitHub Desktop.
google画像認識を使って画像から文字起こしする https://youtu.be/gQ-9aTBlgvA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
import base64 | |
import json | |
from requests import post | |
from passpacker import passwords | |
from pprint import pprint | |
from io import BytesIO | |
from umihico_commons.functools import chunks | |
def _post_request(images): | |
request_list = [ | |
{'image': image, 'features': [ | |
{'type': "TEXT_DETECTION", 'maxResults': 1000}]} | |
for image in images] | |
json_data = json.dumps({'requests': request_list}) | |
url = "https://vision.googleapis.com/v1/images:annotate?key=" | |
api_key = passwords['google_cloud_vision_api'] | |
headers = {'Content-Type': 'application/json'} | |
raw_response = post(url + api_key, data=json_data, json=headers) | |
raw_response.raise_for_status() | |
result = ast.literal_eval(raw_response.text) | |
return result | |
def _encode_image(image): | |
return {'content': base64.b64encode(image).decode("UTF-8")} | |
def _to_image(data): | |
if bool(type(data) is str): | |
if data.startswith("http"): | |
image = {"source": {'imageUri': data}} # url | |
else: | |
image = _encode_image(open(data, 'rb').read()) # local path | |
else: | |
f = BytesIO() | |
data.save(f, format="png") # data was pil_image | |
content = f.getbuffer() | |
# f.close() | |
image = _encode_image(content) | |
return image | |
def _detect_text_raw_response(datas): | |
"""datas is list of urls, local paths and PIL.Images""" | |
images = [_to_image(data) for data in datas] | |
result = _post_request(images) | |
return result | |
def detect_text(datas): | |
chunked_datas = chunks(datas, chunk_len=16) | |
# https://cloud.google.com/vision/docs/limits?hl=ja | |
texts = [] | |
try: | |
for chunked_data in chunked_datas: | |
result = _detect_text_raw_response(chunked_data) | |
for response, path in zip(result["responses"], chunked_data): | |
if response and 'error' not in response.keys(): | |
text = response["textAnnotations"][0]['description'] | |
else: | |
pprint(response) | |
print("error", path) | |
text = f"__error__:{path}" | |
texts.append(text) | |
return texts | |
except (Exception, ) as e: | |
from umihico_commons.functools import save_as_txt | |
save_as_txt("google_text_detection_error.txt", texts) | |
print('save_as_txt("google_text_detection_error.txt", texts)') | |
raise | |
if __name__ == '__main__': | |
from umihico_commons.functools import save_as_txt | |
paths = ["test_pic_seven.jpg", | |
"test_pic_famima.jpg", "test_pic_lawson.jpg", ] | |
texts = detect_text(paths) | |
result = {path: text for path, text in zip(paths, texts)} | |
print(result) | |
save_as_txt("result.txt", result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{'test_pic_famima.jpg': 'FamilyMart\n' | |
'の橋店\n' | |
'東京都港区麻布十番1-2-10\n' | |
'電話: 03-3560-3895\n' | |
'領収証\n' | |
'2016年9月29日\n' | |
'(木)\n' | |
'12:45\n' | |
'十六茶\n' | |
'値引き\n' | |
'#151■\n' | |
'-22\n' | |
'#198\n' | |
'¥133\n' | |
'吊るしベーコン\n' | |
'0茶碗蒸し\n' | |
'(商品合計\n' | |
'(値引合計\n' | |
'小\n' | |
'¥482)\n' | |
'-22)\n' | |
'計\n' | |
'¥460\n' | |
'¥4 60\n' | |
'(内消費税等\n' | |
'お預\n' | |
'お\n' | |
'¥34)\n' | |
'¥510\n' | |
'釣\n' | |
'¥50\n' | |
'レジ2-4986\n' | |
'No. 022\n', | |
'test_pic_lawson.jpg': 'マチの健康ステーション\n' | |
'LAWSON\n' | |
'[領収証]\n' | |
'麻布十番一丁目店\n' | |
'東京都港区麻布十番1-10-3\n' | |
'電話:03-3224-3060\n' | |
'2016年9月29日休) 13:00\n' | |
'コカ·コーラ 300ML\n' | |
'キシュウノウエン カツオウメ\n' | |
'合計\n' | |
'(内消費税等\n' | |
'108\n' | |
'152\n' | |
'¥260\n' | |
'上記正に領収いたしました\n' | |
'お預り合計\n' | |
'お釣\n' | |
'260\n' | |
'¥0\n', | |
'test_pic_seven.jpg': 'セブン-イレブン\n' | |
'品川プリンスホテル店\n' | |
'東京都港区高輪4丁目10-30\n' | |
'電話:03-3445-8701\n' | |
'い48\n' | |
'2012年02月27日(月)\n' | |
'21:44\n' | |
'040\n' | |
'令頁収書\n' | |
'明治エッセル11"-ラ\n' | |
'@126x 2\n' | |
'#252\n' | |
'スターハ、ツクス ミラノエスプレッソ200\n' | |
'¥420\n' | |
'¥215\n' | |
'¥887\n' | |
'¥887\n' | |
'0210x 2\n' | |
'アサヒスーパードライ缶350ML\n' | |
'合計\n' | |
'nanaco支払\n' | |
'お買上明細は上記のとおりです。\n' | |
'商品価格には消費税等を含みます。\n' | |
'nanaco番号\n' | |
'nanaco残高\n' | |
'今回ホ·イント\n' | |
'ポイント残高\n' | |
'伝票番号\n' | |
'¥2,\n' | |
'764\n' | |
'8 P\n' | |
'263 P\n' | |
'120-227-391-4384\n'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment