前提・実現したいこと
ここに質問の内容を詳しく書いてください。
python3,MacOSX でGoogle Vision APIを活用して画像を読み取りOCRで認識させるシステムを作っていますが、実行中に以下のエラーメッセージが発生しました。
File "/Users/○○○/.pyenv/versions/3.7.3/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
結局何も生成されない状況です。
次のとおりpython3 で Atomでスクリプトを作り、デスクトップ場のフォルダの中に保存し、ターミナルで実行してみました。
#!/usr/bin/python
#coding:utf-8
import base64
import json
from requests import Request, Session
from bs4 import BeautifulSoup
path = "image.JPG"
def recognize_captcha(str_image_path):
bin_captcha = open(str_image_path, 'rb').read()
#str_encode_file = base64.b64encode(bin_captcha) str_encode_file = base64.b64encode(bin_captcha).decode("utf-8") str_url = "https://vision.googleapis.com/v1/images:annotate?key=" str_api_key = "xxxxxxxxx" str_headers = {'Content-Type': 'application/json'} str_json_data = { 'requests': [ { 'image': { 'content': str_encode_file }, 'features': [ { 'type': "TEXT_DETECTION", 'maxResults': 10 } ] } ] } print("begin request") 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 ) print("end request") if obj_response.status_code == 200: #print (obj_response.text) with open('data.json', 'w') as outfile: json.dump(obj_response.text, outfile) return obj_response.text else: return "error"
if name == 'main':
data = json.loads(recognize_captcha(path))
data = data["responses"]
print(data)
for i in data:
print(i["fullTextAnnotation"]["text"])
該当のソースコード
上記のうちこの部分と思われます。
if name == 'main':
data = json.loads(recognize_captcha(path))
data = data["responses"]
print(data)
for i in data:
print(i["fullTextAnnotation"]["text"])
補足情報(FW/ツールのバージョンなど)
なおPython 3.7.3を使っています。
原因と解決策がもしわかればご教示いただければ幸いです。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。