|
4 | 4 | import io |
5 | 5 |
|
6 | 6 | # Imports for the REST API |
7 | | -from flask import Flask, request |
| 7 | +from flask import Flask, request, jsonify |
8 | 8 |
|
9 | 9 | # Imports for image procesing |
10 | 10 | from PIL import Image |
11 | | -#import scipy |
12 | | -#from scipy import misc |
13 | 11 |
|
14 | 12 | # Imports for prediction |
15 | 13 | from predict import initialize, predict_image, predict_url |
16 | 14 |
|
17 | 15 | app = Flask(__name__) |
18 | 16 |
|
19 | 17 | # 4MB Max image size limit |
20 | | -app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024 |
| 18 | +app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024 |
21 | 19 |
|
22 | 20 | # Default route just shows simple text |
23 | 21 | @app.route('/') |
24 | 22 | def index(): |
25 | 23 | return 'CustomVision.ai model host harness' |
26 | 24 |
|
27 | 25 | # Like the CustomVision.ai Prediction service /image route handles either |
28 | | -# - octet-stream image file |
| 26 | +# - octet-stream image file |
29 | 27 | # - a multipart/form-data with files in the imageData parameter |
30 | 28 | @app.route('/image', methods=['POST']) |
31 | | -def predict_image_handler(): |
| 29 | +@app.route('/<project>/image', methods=['POST']) |
| 30 | +@app.route('/<project>/image/nostore', methods=['POST']) |
| 31 | +@app.route('/<project>/classify/iterations/<publishedName>/image', methods=['POST']) |
| 32 | +@app.route('/<project>/classify/iterations/<publishedName>/image/nostore', methods=['POST']) |
| 33 | +@app.route('/<project>/detect/iterations/<publishedName>/image', methods=['POST']) |
| 34 | +@app.route('/<project>/detect/iterations/<publishedName>/image/nostore', methods=['POST']) |
| 35 | +def predict_image_handler(project=None, publishedName=None): |
32 | 36 | try: |
33 | 37 | imageData = None |
34 | 38 | if ('imageData' in request.files): |
35 | 39 | imageData = request.files['imageData'] |
| 40 | + elif ('imageData' in request.form): |
| 41 | + imageData = request.form['imageData'] |
36 | 42 | else: |
37 | 43 | imageData = io.BytesIO(request.get_data()) |
38 | 44 |
|
39 | | - #img = scipy.misc.imread(imageData) |
40 | 45 | img = Image.open(imageData) |
41 | 46 | results = predict_image(img) |
42 | | - return json.dumps(results) |
| 47 | + return jsonify(results) |
43 | 48 | except Exception as e: |
44 | 49 | print('EXCEPTION:', str(e)) |
45 | 50 | return 'Error processing image', 500 |
46 | 51 |
|
47 | 52 |
|
48 | 53 | # Like the CustomVision.ai Prediction service /url route handles url's |
49 | 54 | # in the body of hte request of the form: |
50 | | -# { 'Url': '<http url>'} |
| 55 | +# { 'Url': '<http url>'} |
51 | 56 | @app.route('/url', methods=['POST']) |
52 | | -def predict_url_handler(): |
| 57 | +@app.route('/<project>/url', methods=['POST']) |
| 58 | +@app.route('/<project>/url/nostore', methods=['POST']) |
| 59 | +@app.route('/<project>/classify/iterations/<publishedName>/url', methods=['POST']) |
| 60 | +@app.route('/<project>/classify/iterations/<publishedName>/url/nostore', methods=['POST']) |
| 61 | +@app.route('/<project>/detect/iterations/<publishedName>/url', methods=['POST']) |
| 62 | +@app.route('/<project>/detect/iterations/<publishedName>/url/nostore', methods=['POST']) |
| 63 | +def predict_url_handler(project=None, publishedName=None): |
53 | 64 | try: |
54 | | - image_url = json.loads(request.get_data())['Url'] |
| 65 | + image_url = json.loads(request.get_data().decode('utf-8'))['url'] |
55 | 66 | results = predict_url(image_url) |
56 | | - return json.dumps(results) |
| 67 | + return jsonify(results) |
57 | 68 | except Exception as e: |
58 | 69 | print('EXCEPTION:', str(e)) |
59 | 70 | return 'Error processing image' |
60 | 71 |
|
61 | | - |
62 | 72 | if __name__ == '__main__': |
63 | 73 | # Load and intialize the model |
64 | 74 | initialize() |
65 | 75 |
|
66 | 76 | # Run the server |
67 | 77 | app.run(host='0.0.0.0', port=80) |
| 78 | + |
0 commit comments