Python 显示带有边框的图像时出现问题

Python 显示带有边框的图像时出现问题,python,tensorflow,pillow,Python,Tensorflow,Pillow,我有一些代码,可以简单地在图像上绘制一个边界框,并将其保存为新图像。然而,当我运行它时,我得到一个TypeError:无法处理来自枕头的这个数据类型 代码是 # 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_proce

我有一些代码,可以简单地在图像上绘制一个边界框,并将其保存为新图像。然而,当我运行它时,我得到一个
TypeError:无法处理来自枕头的这个数据类型

代码是

# 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
#测试处理后的数据文件是否可以正确读取并具有正确的边界框
导入tensorflow作为tf
将numpy作为np导入
从PIL导入图像
def read_processed_数据(文件名,num_show):
“”“读入已处理的数据文件并显示
给定数量的图像以及边界框。
"""
将open(filename,'r')作为f:
i=0
当我展示:
对于f中的行:
filename=line.rstrip()
下一行=f.readline()
num\u faces=int(next\u line.rstrip())
面数=0
#当面数<面数时:
bb_line=f.readline().rstrip()
y1,x1,y2,x2=bb_线拆分(',')
方框=[y1,x1,y2,x2]
box=tf.cast(box,tf.float32)
返回框,文件名
使用tf.Session()作为sess:
bb,fn=读取已处理的数据(“processed.txt”,1)
image=tf.image.decode\u图像(tf.read\u文件(fn))
img=image.eval()
打印(图像形状)
img_show=np.asarray(img)
Image.fromarray(img_show).save(“test_no_bb.jpg”)#
bb\u image=tf.image.draw\u边界框(img,bb)
打印(bb_图像.形状)
bb_image=tf.cast(bb_image,tf.uint8)
bb_img_jpeg=tf.image.encode_jpeg(bb_图像)
bb\u image\u np=np.asarray(bb\u img\u jpeg)
Image.fromarray(bb_Image_np).save(“test.jpg”)#不起作用
test\u no\u bb.jpg
创建得很好,但是当我到达
Image.fromarray(bb\u Image\u np).save(“test.jpg”)
时,我得到了前面提到的类型错误

我在网上到处搜索都没有结果,TensorFlow的文档也没有。
bb_图像的形状
是正确的,
bb
(边界框的坐标)的输出也是正确的,因此我不知所措

非常感谢您的帮助。

请查看的文档,它返回序列化为张量字符串的编码(即压缩,包括必要的格式标题等)图像

首先,您可能需要从张量中获取实际数据,例如,类似这样的
data=bb\u img\u jpeg.eval()
,最终应该是Python字符串类型(请检查)。然后将其写入文件:


以open('test.jpg','wb')作为f:
f、 写入(数据)

注意,我不确定返回的字符串数据是字节字符串还是字符字符串,在后一种情况下,应该使用data.encode()将其转换为字节

或者,您可以跳过TF的jpeg编码,将其留给PIL,即,
img_bb=bb_image.eval()
,将其转换为数组,并使用PIL将其保存为jpeg,方法与您在示例前半部分中所做的类似