I'm a newbie to Flutter and trying to build a Flutter App.
I want to upload an image from android device to django, process it and send the results back to flutter in a json format without saving the image anywhere. But I'm facing difficulty in reading image in django. However, the image is sent successfully from flutter.
Here is my flutter code for sending an image using Dio :
Future<void> _clearImage() async {
try {
String filename = imageFile!.path.split('/').last;
FormData formData = FormData.fromMap({
"image": await MultipartFile.fromFile(imageFile!.path,
filename: filename, contentType: MediaType('image', 'jpg')),
"type": "image/png"
});
Response response = await dio.post('http://IP:8000/scan/',
data: formData,
options: Options(
followRedirects: false,
// will not throw errors
validateStatus: (status) => true,
headers: {
"accept": "*/*",
"Content-Type": "multipart/form-data",
}));
print(response);
} catch (e) {
print(e);
}
setState(() {});
}
}
Here is my code for django:
file = request.FILES['image']
print(file)
img = cv2.imread(file.read())
print(img)
I'm getting the following error:
img = cv2.imread(file.read())
TypeError: Can't convert object of type 'bytes' to 'str' for 'filename'
Your help will really be appreciated. Thank you in advance!