pyボールドテキスト### 前提・実現したいこと
Google apiを用いたpythonのプログラムで、認識した数字を変数に変え、それを別の答えに計算したい。
発生している問題・エラーメッセージ
画像を、数字認識させるとこまではできたが、その検出された数字を変数や、そこから別の数をかけたりして別の数字に変えることができない。
該当のソースコード
python
1#!/usr/bin/python 2#coding:utf-8 3import base64 4import json 5import cv2 6from requests import Request, Session 7from bs4 import BeautifulSoup 8 9if __name__ == '__main__': 10 11 im = cv2.imread('/Users/yamamotoyuuki/Desktop/python/ aiu.png',0) 12 13 dat = im[200] 14 15 16 17path = "/Users/yamamotoyuuki/Desktop/python/ aiu.png" 18 19def recognize_captcha(str_image_path): 20 bin_captcha = open(str_image_path, 'rb').read() 21 22 #str_encode_file = base64.b64encode(bin_captcha) 23 str_encode_file = base64.b64encode(bin_captcha).decode("utf-8") 24 25 26 str_url = "https://vision.googleapis.com/v1/images:annotate?key=" 27 28 str_api_key = "ここは鍵です" 29 30 str_headers = {'Content-Type': 'application/json'} 31 32 str_json_data = { 33 'requests': [ 34 { 35 'image': { 36 'content': str_encode_file 37 }, 38 'features': [ 39 { 40 'type': "TEXT_DETECTION", 41 'maxResults': 10 42 } 43 ] 44 } 45 ] 46 } 47 48 print("begin request") 49 obj_session = Session() 50 obj_request = Request("POST", 51 str_url + str_api_key, 52 data=json.dumps(str_json_data), 53 headers=str_headers 54 ) 55 obj_prepped = obj_session.prepare_request(obj_request) 56 obj_response = obj_session.send(obj_prepped, 57 verify=True, 58 timeout=60 59 ) 60 print("end request") 61 62 if obj_response.status_code == 200: 63 #print (obj_response.text) 64 with open('data.json', 'w') as outfile: 65 json.dump(obj_response.text, outfile) 66 return obj_response.text 67 else: 68 return "error" 69 70if __name__ == '__main__': 71 data = json.loads(recognize_captcha(path)) 72 data = data["responses"] 73 print(data) 74 for i in data: 75 print(i["fullTextAnnotation"]["text"]) 76
試したこと
下から3行目をprint(int(data) * 2)にしてみましたがだめでした
数字認識のやり方をかえるべきなんでしょうか。。
補足情報(FW/ツールのバージョンなど)
プログラミング始めて1ヶ月もたってない学生で、この質問サイトも始めて使うのでよくわかってません。失礼があれば言ってください。
回答1件
あなたの回答
tips
プレビュー