I have a Django project that receives an image, process it and return a response. I am writing a script to test my API, but the bytes that client sends is not the same that the server receives.
Client code:
# client.py
from urllib.parse import urlencode
from urllib.request import Request, urlopen
import cv2
img = cv2.imread(image_file)
data = {'image': img.tobytes(), 'shape': img.shape}
data = urlencode(data).encode("utf-8")
req = Request(service_url, data)
response = urlopen(req)
print(response.read().decode('utf-8'))
Views code:
# service/app/views.py
import ast
import numpy as np
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def process_image(request):
if request.method == 'POST':
# Converts string to tuple
shape = ast.literal_eval(request.POST.get('shape'))
img_bytes = request.POST.get('image')
# Reconstruct the image
img = np.fromstring(img_bytes, dtype=np.uint8).reshape(shape)
# Process image
return JsonResponse({'result': 'Hello'})
When i run cliente code i get ValueError: total size of new array must be unchanged. I did the following checks, with a 8x8 RGB image:
# client.py
>> print(img.shape)
(8, 8, 3)
>> print(img.dtype)
uint8
>> print(len(img.tobytes()))
192
# service/app/views.py
>> print(shape)
(8, 8, 3)
>> print(len(img_bytes))
187
The shape field is ok, but the image filed has different size. As the image is small, i printed the bytes from client and server, and i did not get the same. I think that this is an encoding problem.
I want to send image as bytes, because i think this is a compact way to send this kind of data. If anyone know a better approach to send image via HTTP, let me know.
Thanks!
literal_evalhere.literal_evalconvertsshapefrom string to tuple.