Convert opencv mpython image to image image

I want to capture frames from a video using python and opencv, and then classify the captured Mat images using a tensor stream. The problem is that I do not know how to convert the Mat format to a 3D Tensor variable. Here is how I am doing now with tensorflow (loading 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 be grateful for any help, thanks in advance

+5
source share
4 answers

Download the OpenCV image with imread, then convert it to a numpy array.

To submit to the v3 source file, you need to use the Mult: 0 tensor as an entry point, this assumes a four-dimensional tensor that has a layout: [Batch index, width, height, channel] The last three combine perfectly with cv :: Mat, the first should be 0 , since you do not want to submit a batch of images, but one image. The code looks like this:

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

Yours faithfully,

Chris

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

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

It looks like you are using a pre-prepared and pre-defined starting model that has a tensor named DecodeJpeg/contents:0 . If so, this tensor expects a scalar string containing bytes for the JPEG image.

You have several options, one of them is to look further down the network for node, where JPEG is converted to a matrix. I'm not sure what the MAT format is, but it will be a view [height, width, colour_depth] . If you can get your image in this format, you can replace the string DecodeJpeg... with the name of the node you want to file into.

Another option is to simply convert your images to JPEG files and directly transfer them.

+3
source

You should be able to convert the opencv mat format to a numpy array like:

 np_image_data = np.asarray(image_data) 

Once you have the data in the form of a numpy array, you can transfer it to the tensor stream through the feed mechanism as in the link referenced by @ thesonyman101

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

In my case, I had to read the image from the file, do some processing, and then type in the beginning to get a return from the object level, called the last. My solution is short but effective.

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

Source: https://habr.com/ru/post/1258856/


All Articles