次のコードは、両方のケースを処理する必要があります。直接文字列を含む
import numpy as np
from PIL import Image
image_file = 'test.jpeg'
with tf.Session() as sess:
# softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
if image_file.lower().endswith('.jpeg'):
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
prediction = sess.run('final_result:0', {'DecodeJpeg/contents:0': image_data})
elif image_file.lower().endswith('.png'):
image = Image.open(image_file)
image_array = np.array(image)[:, :, 0:3]
prediction = sess.run('final_result:0', {'DecodeJpeg:0': image_array})
prediction = prediction[0]
print(prediction)
または短いバージョン:
image_file = 'test.png' # or 'test.jpeg'
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
ph = tf.placeholder(tf.string, shape=[])
with tf.Session() as sess:
predictions = sess.run(output_layer_name, {ph: image_data})
私はあなたの最初の方法で試してみました。 「フィードから要素を取得できません」と出力されます。なぜ私は分からない。しかし、あなたの第二の仕事。ありがとう!! –
"フィードから要素を取得できません"というエラーは異常です。つまり、 'image_array'が文字列の配列として扱われていることを意味しています。したがって、画像のTensorFlowテンソルへの型変換に欠けている可能性があります。 – mrry
2番目の答えから、私は 'DecodeJpeg:0'が 'jpg_data = tf.placeholder(tf.string、shape = []);で構築されていると仮定します。 decoded_jpg = tf.image.decode_jpeg(jpg_data、channels = 3) 'numpy配列ではなく文字列を待ちます。 –