-
Notifications
You must be signed in to change notification settings - Fork 7
/
ocr2speech21-server.py
64 lines (52 loc) · 1.66 KB
/
ocr2speech21-server.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
import base64
import json
import yaml
from flask import Flask
from flask import request
from flask import jsonify
# import from the 21 Developer Library
from two1.wallet import Wallet
from two1.bitserv.flask import Payment
from two1 import mkt
# set up server side wallet
app = Flask(__name__)
wallet = Wallet()
payment = Payment(app, wallet)
@app.route('/manifest')
def manifest():
"""Provide the app manifest to the 21 crawler.
"""
with open('./manifest.yaml', 'r') as f:
manifest = yaml.load(f)
return json.dumps(manifest)
@app.route('/ocr2speech21')
@payment.required(15000)
def ocr2speech21():
"""
Detect text in an image using OCR, translate it to English, and generate
a computer-generated audio clip of it being read.
"""
try:
image_url = request.args.get('image_url')
except:
return jsonify({'error': 'Missing input arguments.'}, 400)
try:
detected_text = mkt.ocr2txt21.detect(image_url=image_url)['text']
except KeyError:
return jsonify({'error': 'No text found in image.'}, 400)
except Exception:
return jsonify(
{'error': 'There was an error processing this image.'}, 500)
try:
english_text = mkt.translate21.translate(text=detected_text)['results']
except Exception:
return jsonify(
{'error': 'There was an error processing the text.'}, 500)
try:
wavfile = mkt.txt2speech21.en({'text': english_text.strip()})
except Exception:
return jsonify(
{'error': 'There was an error processing the text.'}, 500)
return jsonify(wavfile)
if __name__ == '__main__':
app.run(host='::', port=6005)