forked from 13o-bbr-bbq/SAIVS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyRecognitionImage.py
67 lines (56 loc) · 2.14 KB
/
MyRecognitionImage.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
66
67
#!/usr/bin/python
#coding:utf-8
import base64
import json
from requests import Request, Session
# Cloud Vision APIで画像を分析
class RecognizeImage():
def __init__(self):
return
# CAPTCHAの分析
def recognize_captcha(self, str_image_path):
# CAPTCHA画像の読み込み
bin_captcha = open(str_image_path, 'rb').read()
# base64でCAPTCHA画像をエンコード
str_encode_file = base64.b64encode(bin_captcha)
# APIのURLを指定
str_url = "https://vision.googleapis.com/v1/images:annotate?key="
# 事前に取得したAPIキー
str_api_key = "your_api_ket"
# Content-TypeをJSONに設定
str_headers = {'Content-Type': 'application/json'}
# Cloud Vision APIの仕様に沿ってJSONのペイロードを定義。
# CAPTCHA画像からテキストを抽出するため、typeは「TEXT_DETECTION」にする。
str_json_data = {
'requests': [
{
'image': {
'content': str_encode_file
},
'features': [
{
'type': "TEXT_DETECTION",
'maxResults': 10
}
]
}
]
}
# リクエスト送信
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:
print obj_response.text
return obj_response.text
else:
return "error"