Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

how to save image_data with bounding boxes as '.jpg' in object_detection task #340

Closed
erinliu0428 opened this issue Mar 6, 2018 · 8 comments

Comments

@erinliu0428
Copy link

In the object_detection task, we use
data['image_with_ground_truth'] = tc.object_detector.util.draw_bounding_boxes(data['image'], data['annotations'])
to create a turicreate.Image type data which contains image data with bounding boxes. So how to save the turicreate.Image type data as '.jpg' or '.png' ? The file '~/turicreate/data_structures/image.py ' doesn't have a function like this

@erinliu0428
Copy link
Author

erinliu0428 commented Mar 6, 2018

I know already.We can use
img = data['image_with_ground_truth']
img._to_pil_image.save('test.jpg')

@gustavla
Copy link
Collaborator

gustavla commented Mar 6, 2018

Hi @erinliu0428! This is definitely missing from tc.Image (along with other basic functionality that we hope to add). I think what you are describing works, but it is not part of the official API, so it could break at any new version without warning. A more API-stable approach would be

from PIL import Image
Image.fromarray(img.pixel_data).save('test.jpg')

I will leave this open and mark it as a feature request, since there should be an even simpler way.

@erinliu0428
Copy link
Author

@gustavla Thank you for your attention !
And I have another basic need.After a object_detection model created, I want to predict some new pics. I need the new pics are parted from a video directly without be saved in a path.So if there is a way we can load video directly, or after loading video and parting images by other toolkits convert this type of image to tc.Image? By the way,I'm curious about how tc.Image._image_data is generated. I thought if two pics are same size ,the shape of ._image_data should be the same .But it turns out wrong.

@srikris
Copy link
Contributor

srikris commented Mar 6, 2018

That's actually a great feature request. I filed it at #342 and #343 to track this.

@afranklin afranklin changed the title how to save image_data with bounding boxes as '.jpg' in obeject_detection task how to save image_data with bounding boxes as '.jpg' in object_detection task Mar 6, 2018
shantanuchhabra added a commit that referenced this issue Jun 18, 2018
* added save functionality to the src/unity/python/turicreate/data_structures/image.py Image class

* sharing code in save by calling _to_pil_image
@shantanuchhabra shantanuchhabra self-assigned this Jun 18, 2018
@shantanuchhabra
Copy link
Collaborator

Fixed by #709

@baizhenmao95
Copy link

@znation Thank you very much for your reply in #88 for "save the images at full size"; but when I tried the code:
from PIL import Image Image.fromarray(img.pixel_data).save('test.jpg')
There was an AttributeError: 'SArray' object has no attribute 'pixel_data'
I also tried the code:
img = data['image_with_ground_truth'] img._to_pil_image.save('test.jpg')
but there was also the AttributeError: 'SArray' object has no attribute '_to_pil_image'

Is there any other things that I should do in advance?
Thank you!

@znation
Copy link
Contributor

znation commented Jul 12, 2018

Hi @baizhenmao95, in the code examples there, img refers to a single Image, whereas it appears in your code, img refers to an SArray containing images. To operate on each one, you could pull them out individually, like:

from PIL import Image
# note the [0] - this extracts just the first image from the SArray
Image.fromarray(img[0].pixel_data).save('test.jpg')

Or, you could run a lambda function over the SFrame to save all images in parallel. For this, you would need another SArray containing the desired filename (let's assume it's called "filename"):

from PIL import Image
# make a temporary SFrame to associate images with filenames
temp_sf = tc.SFrame()
temp_sf["img"] = img
temp_sf["filename"] = filename
temp_sf.apply(lambda row: Image.fromarray(row["img"].pixel_data).save(row["filename"]))

@baizhenmao95
Copy link

Wow, that's cool! Thank you very much! I'll try it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants