import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像数据
img = tf.gfile.FastGFile('/path/to/picture/3.jpg','rb').read()
with tf.Session() as sess:
img_data_jpg = tf.image.decode_jpeg(img)
img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)
img_data_jpg=tf.image.resize_images(img_data_jpg,(180,267), method=1)
# tf.image.draw_bounding_boxes要求图像矩阵中的数字为实数
# 利用tf.image.convert_image_dtype将图像矩阵转化为实数
batched = tf.expand_dims(tf.image.convert_image_dtype(img_data_jpg, tf.float32), 0)
# 边界框坐标是相对于宽度和宽度在[0.0,1.0]内的浮点数,即这里给出的都是图像的相对位置[0.1, 0.2, 0.8, 0.8]即(0.1*wide, 0.2*high)到(0.8*wide, 0.8*high)
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7],[0.35,0.47,0.5,0.56]]])
# 在图像上绘制边界框
result = tf.image.draw_bounding_boxes(batched, boxes)
plt.subplot(121), plt.imshow(img_data_jpg.eval()), plt.title('original')
plt.subplot(122), plt.imshow(result[0].eval()), plt.title('result')
tensorflow画方框图
最新推荐文章于 2024-02-16 15:23:11 发布