I am setting a Flask server to which I send an image through base64 format. I want to perform cv2.warpPerspective function on that image.The result numpy array is converted back to base64 which is used to render in html page.I am able to change perspective but to convert the result numpy array to base64 is not proper and it is not rendering in html page.
def change_perspective(data):
points = []
print("type of data",type(data))
data_decoded = data.decode("utf-8")
print("type of data_decoded",type(data_decoded))
json_data =data_decoded.replace("'", "\"")
data = json.loads(json_data)
coordinates =data["Coordinates"]
for c in coordinates:
x=c['x']
y=c['y']
points.append([x,y])
image_b64=data["image_base64"].split(",")[1]
print("base 64 image data",type(image_b64))
nparr = np.fromstring(base64.b64decode(image_b64), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
print('points',points)
pts1 = np.float32(points)
pts2 = np.float32([[0,0],[300,0],[0,500],[300,500]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,500))
print("Succeeeded")
return dst
@app.route('/')
def hello_world():
return render_template('perspective_front.html')
@app.route('/perspective', methods=['POST'])
def perspective():
# print("Request Data",request.data)
# print("request.data",request.data)
print("type of request.data",type(request.data))
warped_image = change_perspective(request.data)
warped_image_bytes = warped_image.tobytes()
# print("cv2.imencode('.jpg', warped_image)",cv2.imencode('.jpg', warped_image))
warped_image_b64= base64.b64encode(warped_image_bytes).decode('utf-8')
return render_template('perspective_result.html', img_data="data:image/jpeg;base64,"+warped_image_b64)
Is the conversion from numpy array to base64 is wrong ?
or
Way of rendering is wrong ? Thank in advance