I've trained a CNN on the CIFAR10 dataset (placeholder, will be replaced with a different model later) and integrated the model into a flask API. The API is hosted on Heroku, and I would now like to use Flutter / Dart to take pictures on my phone, send them to the Flask API, run my trained model on them and return the prediction.
Using python, I can easily make a post request to my API and return the predictions. Here is my simple python code for this:
import requests
import json
img = open('some-picture.jpg', 'rb')
files = {'image': img}
response = requests.post("url_to_api", files=files)
print(response.text)
I haven't been using Flutter / Dart for very long, and I gather that the process of making htpp requests is a little more complex than in python. Could someone give me some pointers or perhaps code that allows me to take a picture with my camera, upload it to my API, and store the response in a variable? Here's my (simplified) python code for the flask API:
from flask import Flask, request
import os
import numpy as np
from PIL import Image
from tensorflow import keras
app = Flask(__name__)
app.config["DEBUG"] = True
model = keras.models.load_model('cifar10_cnn.h5')
labels = ["Airplane", "Automobile", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship", "Truck"]
@app.route('/', methods=["POST"])
def predict():
# stuff not relevant to question, left out for conciseness #
file = request.files['image']
image = Image.open(file).resize((32, 32))
image = np.array(image)
image = image / 255
image = image.reshape(-1, 32, 32, 3)
predictions = model.predict([image])
index = np.argmax(predictions)
results = {'Prediction:': labels[index]}
return results
if __name__ == '__main__':
app.run()
So far I know that Multipart files seem like the way to go, and that the Dio package might be worth looking into. If further tips or code could be provided I would be grateful.
You may already know how to choose an image from the gallery/camera (e.g. using
image_picker
library). You fill a class field likeFile image;
with the result of that picker. This could be as easy as:(Change the
ImageSource source
to match your desire: camera or gallery)Then you can upload that file to your api. Using
http
library:For each one of these libraries, you have to add them as dependency inside
pubspec.yaml
in your flutter project: