Issue
Can someone help me with this? I am sending an image from flutter to django (python) server. I am successfully in taking the image from mobile camera/gallery and converting(encoded) the image to base64.
Flutter code:
Future classifyImage() async{
if(imageFile==null) return;
String base64Image = base64Encode(imageFile.readAsBytesSync());
String fileName = imageFile.path.split("/").last;
http.post(phpEndPoint, body:
{
"image" : base64Image,
//"name" : fileName,
}).then((res) {
print(res.body);
}).catchError((err) {
print(err);
//print(base64Image);
//print(fileName);
});
}
here is the converted string in flutter by base64
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQICAQEBAQMCAgICAwMEBAMDAwMEBAYFBAQFBAMDBQcFBQYGBgYGBAUHBwcGBwYGBgb/2wBDAQEBAQEBAQMCAgMGBAMEBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgb/wAARCAPXAy0DASIAAhEBAxEB/8QAHwAAAgICAwEBAQAAAAAAAAAABgcEBQMIAAIJAQoL/8QAaBAAAQMDAwIEAwQFBwQJEAITAQIDEQAEIQUSMQZBEyJRYQcycRRCgZEICSNSoRUzYnKxtMEWN3bRCiRDZHSChJLwFyY2U1ZjZWZzhZSipbO14Sc0NUR1g5WkGEWTJSg4RkdXWHej8f/EABcBAQEBAQAAAAAAAAAAAAAAAAACAQP/xAAaEQEAAwEBAQAAAAAAAAAAAAAAAgMyATER/9oADAMBAAIRAxEAPwDzr/2W4wXv0yf0cSCRH6MDY/8Abuo1+SpOmqUoJEyTzX66/wDZZbC3v0x/0dClBUB+jG3JA/8ADmo1+U6ysFF9uWiM+lXZvqY5DbfTDy07hvIA4FW+n9E3Nw4ghLme4px6ZpqC2NyEkd5FMbp/SrfcgKQ2M+lQomrHogtIAcSQoeoq7Z6SS2UmAY4pwa1Zhq6CGmgRnKRUZixddKR4RAjPloAS20YNH5AcZxV41aBsD9mD6YpgWnTy3YBQoe8VeNdIKWAYI9hQK9Ol/avOGgkTkRU9jQCCP2ZwKbNv019nSEFAk8kirVnQgIGz+FAqWNBOPJ/CrRvQDM7DEelNpnQhjyfhFWzegDEJ+hIoF7pekFpopKYxgRVujTck7e1HrOjBsQUYqa1pSceWgB2dLKgPKffFWKNJMfIaOmdMAgBP8KsEaanAKe1AGWmn7EREGPSpYssj/VRWLMIJAQRHEiu6bOe0fhQDKbPvH8KzC0BE8fhRKizjEc94rKLIA8A+o20
Now, the problem is the string which is received in django server is different from the string which is converted in flutter.
here is the string which is received in django server
Python code:
@csrf_exempt
def android_predict(request):
try :
print('Request method is : ' + request.method)
if request.method == 'POST' :
print('method is POST')
print('Body is : ' + str(request.body))
decoded = request.body.decode("UTF-8")
print(decoded)
name_image = decoded.split('&')[1].split('=')[1]
print('name is : ' + name_image)
b64_image = decoded.split('&')[0].split('=')[1]
print('Base64 image is : ' + b64_image)
missing_padding = len(b64_image)%4
print('Length is : ' + str(len(b64_image)))
if missing_padding :
b64_image += '='*(4-missing_padding)
print('Modified Base64 image is : ' + b64_image)
print('New length is : ' + str(len(b64_image)))
image = PIL.Image.open(io.BytesIO(base64.b64decode(b64_image)))
target_image = image.resize((500,500),PIL.Image.ANTIALIAS)
print(type(target_image))
image_array = np.array(target_image)
image_file, x1 = predict_image(image_array,name_image)
# image = PIL.Image.open(io.BytesIO(b64_image))
context_dict = {'statusCode' : 0, 'statusMessage' : 'working', 'prediction' : x1}
else :
print('method is GET')
context_dict = {'statusCode' : 1, 'statusMessage' : 'request type is not post'}
print(context_dict)
return JsonResponse(context_dict, safe=False)
except :
context_dict = {'statusCode' : 1, 'statusMessage' : 'exception occurred'}
print(context_dict)
return JsonResponse(context_dict, safe=False)
Solution
when you send to server in base64 String, convert encording urlencode
var encoded = Uri.encodeFull(base64String);
and in server your string value convert
urldecording -> base64decording
Answered By - Junsu Cho
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.