[英]Trouble Displaying an Image with Bounding Boxes
I have some code which is meant to simply draw a bounding box on an image, and save it out as a new image. 我有一些代码,旨在简单地在图像上绘制边框,并将其另存为新图像。 However, when I run it I get a TypeError: Cannot handle this data type
from pillow. 但是,当我运行它时,我收到一个TypeError: Cannot handle this data type
从枕头TypeError: Cannot handle this data type
。
The code is 该代码是
# 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
gets created fine, but when I reach Image.fromarray(bb_image_np).save("test.jpg")
, I get the aforementioned type error. test_no_bb.jpg
得到很好的创建,但是当我到达Image.fromarray(bb_image_np).save("test.jpg")
,出现上述类型错误。
I have searched the web all over to no avail, and TensorFlow's documentation on this is lacking. 我到处都在网上搜索无济于事,并且缺少TensorFlow的相关文档。 The shape of the bb_image
is correct, and the output of bb
(the coordinates of the bounding box) is also correct so I am at a loss. bb_image
的形状正确,并且bb
的输出(边界框的坐标)也正确,所以我很茫然。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
Look at the doc of https://www.tensorflow.org/api_docs/python/tf/image/encode_jpeg , it returns encoded (ie, compressed, including necessary format headers etc..) image serialized into the tensor string. 查看https://www.tensorflow.org/api_docs/python/tf/image/encode_jpeg的文档,它返回序列化到张量字符串中的已编码 (即压缩,包括必需的格式标题等)图像。
First, you may need to get the actual data from the tensor, ie, something like this data = bb_img_jpeg.eval()
, this should end up having a Python string type (please check that). 首先,您可能需要从张量中获取实际数据,即类似data = bb_img_jpeg.eval()
,最终应该具有Python字符串类型(请检查)。 Then you write this to a file: 然后将其写入文件:
with open('test.jpg', 'wb') as f: f.write(data)
Note that I am not sure if the returned string data is string of bytes or chars, in the latter case you should convert it to bytes using data.encode(). 请注意,我不确定返回的字符串数据是字节还是字符的字符串,在后一种情况下,您应该使用data.encode()将其转换为字节。
Alternatively you may skip the jpeg encoding of TF and leave it to PIL, ie, img_bb = bb_image.eval()
, convert that to an array and save it as JPEG using PIL in a similar way you did in the first half of your example. 或者,您可以跳过TF的jpeg编码,然后将其留给PIL,即img_bb = bb_image.eval()
,将其转换为数组,然后使用PIL将其另存为JPEG,就像在示例上半部分所做的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.