0

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

Jennings
  • 506
  • 2
  • 9
  • 22
  • See https://stackoverflow.com/questions/42470920/python-convert-png-string-obtained-via-todataurl-to-binary-png-file And some advise: Including the real problem (how to get a usable image out of a base64-encoded dataurl) in the title helps get an answer. This isn't a question about warpPerspective, and labeling it as such reduces the chance of having people who might not have used warpPerspective, but who might now about base64 decoding images, even look at the question. – Dave W. Smith Feb 09 '19 at 18:20
  • Possible duplicate of [Python: Convert PNG string obtained via toDataURL to binary PNG file](https://stackoverflow.com/questions/42470920/python-convert-png-string-obtained-via-todataurl-to-binary-png-file) – Dave W. Smith Feb 09 '19 at 18:20
  • @DaveW.Smith I have edited my question Could you help me plz – Jennings Feb 11 '19 at 06:07
  • I would expect that you're looking for something like `_, jpg = cv2.imencode('.jpg', warped_image)` followed by `warped_image_b64 = base64.b64encode(jpg)`. – Dave W. Smith Feb 12 '19 at 04:22

0 Answers0