Python tf.gfile.FastGFile(imagePath,rb';).read()

Python tf.gfile.FastGFile(imagePath,rb';).read(),python,tensorflow,Python,Tensorflow,我试着举一个例子。 我使用下面的代码,但我想把图像本身,而不是图像路径。有办法吗 def run_inference_on_image(): answer = None if not tf.gfile.Exists(imagePath): tf.logging.fatal('File does not exist %s', imagePath) return answer image_data = tf.gfile.FastGFile(

我试着举一个例子。 我使用下面的代码,但我想把图像本身,而不是图像路径。有办法吗

def run_inference_on_image():
    answer = None

    if not tf.gfile.Exists(imagePath):
        tf.logging.fatal('File does not exist %s', imagePath)
        return answer

    image_data = tf.gfile.FastGFile(imagePath, 'rb').read()

    create_graph()

    with tf.Session() as sess:

        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image_data})
        predictions = np.squeeze(predictions)

        top_k = predictions.argsort()[-2:][::-1]  
        f = open(labelsFullPath, 'rb')
        lines = f.readlines()
        labels = [str(w).replace("\n", "") for w in lines]
        for node_id in top_k:
            human_string = labels[node_id]
            score = predictions[node_id]
            print('%s (score = %.5f)' % (human_string, score))

        answer = labels[top_k[0]]
    return answer

图像是我要分类为已学习PB文件的图像。

image\u data
是代码中的实际图像字节。它们在sess.run()函数中使用。下面的代码行以字符串形式返回图像字节

tf.gfile.FastGFile(imagePath,'rb').read()

因此,您只需将图像字节的
字符串
传递给
sess.run()
,这可以通过以下几种方式完成:-

# method 1
import cv2
img_str = cv2.imencode('.jpg', img)[1].tostring()

# method 2
from scipy.ndimage import imread
imgbytes = imread(img_path)
img_str = imgbytes.tostring()

检查适合您的内容。

image\u数据是代码中的实际图像字节。它们在sess.run()函数中使用。下面的代码行以字符串形式返回图像字节

tf.gfile.FastGFile(imagePath,'rb').read()

因此,您只需将图像字节的
字符串
传递给
sess.run()
,这可以通过以下几种方式完成:-

# method 1
import cv2
img_str = cv2.imencode('.jpg', img)[1].tostring()

# method 2
from scipy.ndimage import imread
imgbytes = imread(img_path)
img_str = imgbytes.tostring()

检查适合您的内容。

您所说的“图像本身”是什么意思?您可以修改内容以检查问题的内容。您所说的“图像本身”是什么意思?您可以修改内容以检查问题的内容。