Reputation: 593
I am working on a style transfer task, my model returns a tensor. recently I was saving that image using torchvision.utils
torchvision.utils.save_image(genarated_image, result_path)
now I have passed the same image to streamlit.
def image_input():
content_file = st.sidebar.file_uploader("Choose a Content Image", type=["png", "jpg", "jpeg"])
if content_file is not None:
content = Image.open(content_file)
content = np.array(content) # pil to cv
content = cv2.cvtColor(content, cv2.COLOR_RGB2BGR)
else:
st.warning("Upload an Image OR Untick the Upload Button)")
st.stop()
WIDTH = st.sidebar.select_slider('QUALITY (May reduce the speed)', list(range(150, 501, 50)), value=200)
content = imutils.resize(content, width=WIDTH)
generated = genarate_image(content)
st.sidebar.image(content, width=300, channels='BGR')
st.image(generated, channels='BGR', clamp=True)
But now streamlit giving me this error.
TypeError: a bytes-like object is required, not 'Tensor'
is there a way to convert tensor into a "bytes-like object" ?
Upvotes: 1
Views: 881
Reputation: 593
It can be solved by transforming tonsor to PIL Image.
from torchvision import transforms
def trans_tensor_to_pil(tensor_img):
pil_image = transforms.ToPILImage()(tensor_img.squeeze_(0))
return pil_image
Upvotes: 1