我有一些代码只是在图像上绘制一个边界框,并将其保存为新图像。但是,当我跑步时,我从枕头上拿到TypeError: Cannot handle this data type
。
代码是
# Tests that the processed data file can be read correctly and has correct bounding boxes
import tensorflow as tf
import numpy as np
from PIL import Image
def read_processed_data(filename, num_show):
""" Reads in the processed data file and displays the
given number of images, along with the bounding boxes.
"""
with open(filename, 'r') as f:
i = 0
while i < num_show:
for line in f:
filename = line.rstrip()
next_line = f.readline()
num_faces = int(next_line.rstrip())
face_num = 0
#while face_num < num_faces:
bb_line = f.readline().rstrip()
y1, x1, y2, x2 = bb_line.split(',')
box = [y1, x1, y2, x2]
box = tf.cast(box, tf.float32)
return box, filename
with tf.Session() as sess:
bb, fn = read_processed_data("processed.txt", 1)
image = tf.image.decode_image(tf.read_file(fn))
img = image.eval()
print(img.shape)
img_show = np.asarray(img)
Image.fromarray(img_show).save("test_no_bb.jpg") # Works
bb_image = tf.image.draw_bounding_boxes(img, bb)
print(bb_image.shape)
bb_image = tf.cast(bb_image, tf.uint8)
bb_img_jpeg = tf.image.encode_jpeg(bb_image)
bb_image_np = np.asarray(bb_img_jpeg)
Image.fromarray(bb_image_np).save("test.jpg") # Does not work
test_no_bb.jpg
创建得很好,但当我到达Image.fromarray(bb_image_np).save("test.jpg")
时,我得到上述类型错误。
我在网上搜索都无济于事,TensorFlow的文档缺乏。 bb_image
的形状是正确的,bb
的输出(边界框的坐标)也是正确的,所以我不知所措。
非常感谢任何帮助。
答案 0 :(得分:1)
查看https://www.tensorflow.org/api_docs/python/tf/image/encode_jpeg的文档,它将编码(即压缩,包括必要的格式标题等等)图像序列化为张量字符串。
首先,您可能需要从张量中获取实际数据,即此类data = bb_img_jpeg.eval()
,这应该最终具有Python字符串类型(请检查)。然后你把它写到一个文件:
with open('test.jpg', 'wb') as f:
f.write(data)
请注意,我不确定返回的字符串数据是字节字符串还是字符串,在后一种情况下,您应该使用data.encode()将其转换为字节。
或者您可以跳过TF的jpeg编码并将其保留到PIL,即img_bb = bb_image.eval()
,将其转换为数组并使用PIL将其保存为JPEG,方式与您在前半部分中所做的相似示例