我正在将图像作为base64字符串发送到服务器。这些图像来自在canvas元素中绘制的cv.Mat对象:
cv.imshow(canvas, matObject);
let base64Image = canvas.toDataURL("image/jpeg");
base64Image = base64Image.split(',')[1]; // Remove the unnecessary header
let dims = [matObject.rows, matObject.cols, matObject.channels()];
let form = new FormData();
form.append("data", base64Image);
form.append("dimensions", dims.join(','));
// The form object is sent to the server
然后,在服务器中,我对图像进行解码,但是尺寸不匹配:
imBase64 = flask.request.form['data']
shape = flask.request.form['dimensions']
shape = tuple(list(map(int, shape.split(','))))
if sys.version_info.major == 3:
imBase64 = bytes(imBase64, encoding='utf-8')
image = np.frombuffer(base64.decodestring(imBase64), dtype=uint8)
image = image.reshape(shape) # here is the error
所以,错误在于图像的尺寸,我无法正确调整解码图像的形状。例如,如果来自客户端的尺寸为height = 216, width = 204, channels = 1
,则我希望服务器中解码图像的长度为44064 = 216 * 204 * 1
,但是我得到了完全不同的信息(15531),因此,我可以重塑它。
您认为这是什么错误?