tensorflow在图像中加入标注框

来源:互联网 发布:淘宝 充话费 退款 编辑:程序博客网 时间:2024/06/17 08:58

tensorflow提供了给图像加入标注框的函数。tf.image.draw_bounding_boxes(img, boxes).img是目标图像,不过需要变成四维,增加一个维度,利用tf.expand_dims函数,这个函数。boxes是一个三维的数组,里面的元素有4个维度,分别表示框的位置,这些数值是相对位置,

例如:

import matplotlib.pyplot as plt;import tensorflow as tf;image_raw_data_jpg = tf.gfile.FastGFile('11.jpg', 'r').read()with tf.Session() as sess:img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)img_data_jpg = tf.expand_dims(tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32), 0)boxes = tf.constant([[[0.05, 0.5, 0.9, 0.7]]])result = tf.image.draw_bounding_boxes(img_data_jpg, boxes)# print sess.run(img_data_jpg).shapeplt.figure(1)plt.imshow(result.eval().reshape([1200, 1920, 3]))plt.show()

结果: