Search code examples
pythonopencvtensorflow

Convert python opencv mat image to tensorflow image data


I want to capture frames from a video with python and opencv and then classify the captured Mat images with tensorflow. The problem is that i don´t know how to convert de Mat format to a 3D Tensor variable. This is how i am doing now with tensorflow (loading the image from file) :

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})

I will appreciate any help, thanks in advance


Solution

  • Load the OpenCV image using imread, then convert it to a numpy array.

    For feeding into inception v3, you need to use the Mult:0 Tensor as entry point, this expects a 4 dimensional Tensor that has the layout: [Batch index,Width,Height,Channel] The last three are perfectly fine from a cv::Mat, the first one just needs to be 0, as you do not want to feed a batch of images, but a single image. The code looks like:

    #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! 
    

    Kind regards,

    Chris

    Edit: I just noticed, that the inception network wants intensity values normalized as floats to [-0.5,0.5], so please use this code to convert them before building the RGB image:

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