Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python tensorflow中将base64作为图像读取_Python_Tensorflow_Encoding - Fatal编程技术网

在python tensorflow中将base64作为图像读取

在python tensorflow中将base64作为图像读取,python,tensorflow,encoding,Python,Tensorflow,Encoding,我训练了一个模型,我可以像这样给它输入一个图像: 方法1: 现在,我希望能够执行类似的操作,但不是使用.load\u img()从文件中读取图像,而是希望在base64中获取图像并获得相同的输入数组 方法2(不提供与上述相同的输入\u数组): 正如我们所见,方法1上的输入数组与方法2上的输入数组不同。如何获得与方法1相同的结果?其思想是构建一个API来进行推理,并将数据作为base64获取 fpath = 'image.jpg' image_size = (224, 224) img = tf

我训练了一个模型,我可以像这样给它输入一个图像:

方法1: 现在,我希望能够执行类似的操作,但不是使用
.load\u img()
从文件中读取图像,而是希望在base64中获取图像并获得相同的
输入数组

方法2(不提供与上述相同的
输入\u数组
): 正如我们所见,方法1上的
输入数组
与方法2上的
输入数组
不同。如何获得与方法1相同的结果?其思想是构建一个API来进行推理,并将数据作为base64获取

fpath = 'image.jpg'
image_size = (224, 224)

img = tf.keras.preprocessing.image.load_img(
    fpath,
    grayscale=False,
    color_mode='rgb',
    target_size=IMAGE_SIZE,
    interpolation='bilinear')

input_array = tf.keras.preprocessing.image.img_to_array(img)
input_array[0][0] # output: array([167., 103.,  66.], dtype=float32)
pred = model_loaded.predict(np.expand_dims(input_array, axis = 0)/255)
fpath = 'image.jpg'

# convert image base64 to be sent
image_base64 = base64.b64encode(open(fpath, 'rb').read()).decode("utf-8")
data = json.dumps({'image': image_base64})

# on the server side I get `data` and I want to read the base64 to feed tensorflow model
image = base64.b64decode(json.loads(data)['image'])
image[0:5] # output: b'\xff\xd8\xff\xe0\x00'

image = tf.io.decode_image(image, channels=3)
image = tf.image.resize(image, 
                        method="bilinear", 
                        size=IMAGE_SIZE)

input_array = np.array(image) 
input_array[0][0] # output array([163.3482  , 102.01786 ,  62.383926], dtype=float32)
pred = model.predict(np.expand_dims(input_array, axis=0))