Пример #1
0
def post():
    """
    (7) Make a prediction from a URL provided via POST request.
    """
    url = request.json.get('url')
    img = utils.fetch_image(url)
    result = utils.predict_from_image(CLF, img)
    return jsonify(result)
Пример #2
0
def form():
    """
    (4b) Make a prediction from a URL given via a GET form.
    """
    url = request.args.get('url')
    if url:
        img = utils.fetch_image(url)
        result = utils.predict_from_image(CLF, img)
        result['url'] = url  # If we add this back, we can display it.
    else:
        result = {}

    return render_template('form.html', result=result)
Пример #3
0
def api():
    """
    (8) Make a prediction from a base64-encoded image via POST request.

    If accessing the web API from code, you may not have a URL to pass to
    the service, and there is no form for doing a file upload. So we need
    a way to pass the image as data. There are lots of ways to do this; one
    way is to encode as base64.
    """
    data = request.json.get('image')
    if data.startswith('http'):
        img = utils.fetch_image(data)
    else:
        img = Image.open(BytesIO(base64.b64decode(data)))
    result = utils.predict_from_image(CLF, img)
    return jsonify(result)
Пример #4
0
def upload():
    """
    (5) Make a prediction from an image uploaded via a form.

    Bonus: on a mobile device, there will automatically be an option to
    capture via the camera.
    """
    if request.method == 'POST':
        data = request.files['image'].read()
        img = Image.open(BytesIO(data))
        result = utils.predict_from_image(CLF, img)
        result['image'] = base64.b64encode(data).decode('utf-8')
    else:
        result = {}

    return render_template('upload.html', result=result)
Пример #5
0
def plot():
    """
    (6) This time we'll also send back a plot of the probabilities.

    We'll use the exact same code as (3), except we'll add the plot.
    """
    if request.method == 'POST':
        data = request.files.get('image').read()
        img = Image.open(BytesIO(data))
        result = utils.predict_from_image(CLF, img)
        result['image'] = base64.b64encode(data).decode('utf-8')

        # This is the only new line.
        result['plot'] = utils.plot(result['probs'], CLF.classes_)
    else:
        result = {}

    return render_template('plot.html', result=result)
Пример #6
0
def predict():
    """
    (3) Make a prediction from a URL given via GET request.
    
    Using a URL means we can still just accept a string as an arg.

    There's still no human interface.
    """
    url = request.args.get('url')
    img = utils.fetch_image(url)
    result = utils.predict_from_image(CLF, img)

    # Deal with not getting a URL.
    # if url:
    #     img = utils.fetch_image(url)
    #     result = utils.predict_from_image(CLF, img)
    # else:
    #     result = 'Please provide a URL'

    return jsonify(result)