次のページを参考にしました。
Use OCR to detext text in an image
Ocp-Apim-Subscription-Key と url は変更して下さい。
deutsch.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
import requests
import json
ocr_url = 'https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr'
headers = {'Ocp-Apim-Subscription-Key': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
params = {'language': 'de', 'detectOrientation ': 'true'}
data = {'url': 'https://example.com/test_de.png'}
response = requests.post(ocr_url, headers=headers, params=params, json=data)
response.raise_for_status()
analysis = response.json()
print(analysis)
実行方法
./deutsh.py > tmp01.json
sed "s/'/\"/g" < tmp01.json > tmp02.json
jq . tmp02.json | grep text
実行結果
"textAngle": 0,
"text": "Vor"
"text": "einem"
"text": "großen"
"text": "Walde"
"text": "wohnte"
"text": "ein"
"text": "armer"
"text": "Holzhacker"
"text": "mit"
"text": "seiner"
"text": "Frau"
"text": "und"
"text": "seinen"
"text": "zwei"
"text": "Kindern;"
"text": "das"
"text": "Bübchen"
"text": "hieß"
"text": "Hänsel"
"text": "und"
"text": "das"
"text": "Mädchen"
"text": "Gretel."