繁体   English   中英

将python opencv mat图像转换为tensorflow图像数据

[英]Convert python opencv mat image to tensorflow image data

我想用 python 和 opencv 从视频中捕获帧,然后用 tensorflow 对捕获的 Mat 图像进行分类。 问题是我不知道如何将 de Mat 格式转换为 3D Tensor 变量。 这就是我现在使用 tensorflow 的方式(从文件加载图像):

image_data = tf.gfile.FastGFile(imagePath, 'rb').read()
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})

我将不胜感激任何帮助,提前致谢

使用 imread 加载 OpenCV 图像,然后将其转换为 numpy 数组。

为了提供给 inception v3,您需要使用 Mult:0 Tensor 作为入口点,这需要一个具有以下布局的 4 维 Tensor:[Batch index,Width,Height,Channel] 最后三个完全来自 cv: :Mat,第一个只需要为 0,因为您不想提供一批图像,而是单个图像。 代码如下:

#Loading the file
img2 = cv2.imread(file)
#Format for the Mul:0 Tensor
img2= cv2.resize(img2,dsize=(299,299), interpolation = cv2.INTER_CUBIC)
#Numpy array
np_image_data = np.asarray(img2)
#maybe insert float convertion here - see edit remark!
np_final = np.expand_dims(np_image_data,axis=0)

#now feeding it into the session:
#[... initialization of session and loading of graph etc]
predictions = sess.run(softmax_tensor,
                           {'Mul:0': np_final})
#fin! 

亲切的问候,

克里斯

编辑:我刚刚注意到,初始网络希望将强度值标准化为浮点数为 [-0.5,0.5],因此请在构建 RGB 图像之前使用此代码转换它们:

np_image_data=cv2.normalize(np_image_data.astype('float'), None, -0.5, .5, cv2.NORM_MINMAX)

看起来您正在使用预训练和预定义的 Inception 模型,该模型具有一个名为DecodeJpeg/contents:0的张量。 如果是这样,则此张量需要一个包含 JPEG 图像字节的标量字符串。

您有几种选择,一种是进一步查看网络以查找 JPEG 转换为矩阵的节点。 我不确定 MAT 格式是什么,但这将是[height, width, colour_depth]表示。 如果您可以获得该格式的图像,您可以将DecodeJpeg...字符串替换为您想要输入的节点的名称。

另一种选择是简单地将您的图像转换为 JPEG 并直接输入。

使用 Tensorflow 2.0 和 OpenCV 4.2.0,您可以通过以下方式进行转换:

import numpy as np
import tensorflow as tf
import cv2 as cv

width = 32
height = 32

#Load image by OpenCV
img = cv.imread('img.jpg')

#Resize to respect the input_shape
inp = cv.resize(img, (width , height ))

#Convert img to RGB
rgb = cv.cvtColor(inp, cv.COLOR_BGR2RGB)

#Is optional but i recommend (float convertion and convert img to tensor image)
rgb_tensor = tf.convert_to_tensor(rgb, dtype=tf.float32)

#Add dims to rgb_tensor
rgb_tensor = tf.expand_dims(rgb_tensor , 0)

#Now you can use rgb_tensor to predict label for exemple :

#Load pretrain model, made from: https://www.tensorflow.org/tutorials/images/cnn
model = tf.keras.models.load_model('cifar10_model.h5')

#Create probability model 
probability_model = tf.keras.Sequential([model, 
                                     tf.keras.layers.Softmax()])
#Predict label
predictions = probability_model.predict(rgb_tensor, steps=1)

您应该能够将 opencv mat 格式转换为 numpy 数组,如下所示:

np_image_data = np.asarray(image_data)

将数据作为 numpy 数组后,您可以通过馈送机制将其传递给张量流,如@thesonyman101 引用的链接所示:

feed_dict = {some_tf_input:np_image_data}
predictions = sess.run(some_tf_output, feed_dict=feed_dict)

在我的情况下,我必须从文件中读取图像,进行一些处理,然后注入 inception 以获取来自称为最后一层的特征层的返回。 我的解决方案简短但有效。

        img = cv2.imread(file)
        ... do some processing 
        img_as_string = cv2.imencode('.jpg', img)[1].tostring()
        features = sess.run(last_layer, {'DecodeJpeg/contents:0': img_as_string})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM