前提・実現したいこと
cloud vision apiを用いてテキスト検出をしています。
その際、各テキストボックスのx座標最大値から降順でjsonデータとして格納されているのですが、これを
x座標の最大値から降順、y座標は0からにして
画像の右上から左下に向かってテキスト検出をさせたい
該当のソースコード
↓↓↓返って来るjsonデータ↓↓↓ { "property": { "detectedLanguages": [ { "languageCode": "ja" } ] }, "boundingBox": { "vertices": [ { "x": 35, "y": 201 }, { "x": 47, "y": 201 }, { "x": 47, "y": 206 }, { "x": 35, "y": 206 } ] }, "text": "\u308b", "confidence": 0.99 },
python
1def request_cloud_vison_api(image_base64): 2 api_url = GOOGLE_CLOUD_VISION_API_URL + API_KEY 3 req_body = json.dumps({ 4 'requests': [{ 5 'image': { 6 'content': image_base64.decode('utf-8') # jsonに変換するためにstring型に変換する 7 }, 8 'features': [{ 9 'type': 'DOCUMENT_TEXT_DETECTION', # ここを変更することで分析内容を変更できる 10 'maxResults': 10, 11 }] 12 }] 13 }) 14 15 res = requests.post(api_url, data=req_body) 16 return res.json() 17 18# 画像読み込み 19def img_to_base64(filepath): 20 with open(filepath, 'rb') as img: 21 img_byte = img.read() 22 return base64.b64encode(img_byte) 23 24# 文字認識させたい画像を./img.pngとする 25img_base64 = img_to_base64('images/konan.jpg') 26result = request_cloud_vison_api(img_base64) 27 28i = 0; 29for rect in result["responses"][0]["textAnnotations"]: 30 31 x1 = rect["boundingPoly"]["vertices"][0]["x"] 32 y1 = rect["boundingPoly"]["vertices"][0]["y"] 33 x2 = rect["boundingPoly"]["vertices"][1]["x"] 34 y2 = rect["boundingPoly"]["vertices"][2]["y"] 35 36 37 38 39 print(result["responses"][0]["textAnnotations"][i]["boundingPoly"]) 40 print(result["responses"][0]["textAnnotations"][i]["description"])
Terminal
1 2//検出したテキストboxの四つ角の座標(0番目のx座標、降順になっている。) 3//検出した文字 4 5 6{'vertices': [{'x': 604, 'y': 363}, {'x': 617, 'y': 363}, {'x': 617, 'y': 411}, {'x': 604, 'y': 411}]} 7引っ越しまん 8{'vertices': [{'x': 603, 'y': 50}, {'x': 616, 'y': 50}, {'x': 616, 'y': 65}, {'x': 603, 'y': 65}]} 9から 10{'vertices': [{'x': 594, 'y': 35}, {'x': 604, 'y': 35}, {'x': 606, 'y': 86}, {'x': 596, 'y': 86}]} 11トラックの上に 12{'vertices': [{'x': 583, 'y': 36}, {'x': 593, 'y': 36}, {'x': 592, 'y': 71}, {'x': 582, 'y': 71}]} 13売ってたん 14{'vertices': [{'x': 592, 'y': 361}, {'x': 600, 'y': 360}, {'x': 600, 'y': 361}, {'x': 592, 'y': 362}]} 15「 16{'vertices': [{'x': 592, 'y': 371}, {'x': 601, 'y': 370}, {'x': 603, 'y': 398}, {'x': 594, 'y': 399}]} 17ってたよ 18{'vertices': [{'x': 595, 'y': 402}, {'x': 603, 'y': 401}, {'x': 603, 'y': 402}, {'x': 595, 'y': 403}]} 19! 20{'vertices': [{'x': 578, 'y': 249}, {'x': 589, 'y': 249}, {'x': 589, 'y': 278}, {'x': 578, 'y': 278}]} 21そのに
補足情報(FW/ツールのバージョンなど)
python3.6.5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/25 06:30