How to extract Image objects from SFrame?

User 3244 | 2/24/2016, 8:39:33 PM

I'm working on integrating graph lab create with a python-flask web app and the problem is that I cannot extract graph lab image objects from an SFrame to display in jpg/png format in the browser. I am using a pre-trained data set that allows me to show images in ipython or in the browser with the graph lab server, but I would like to display those images in my own application. How are images stored as objects in GLC? Should I use a library like Pillow to help convert these image objects into pixel data or jpeg? Also, how are Images stored in an SFrame with graph lab create?

THANK YOU!

Comments

User 2593 | 2/24/2016, 9:34:44 PM

Hi @shai_wilson,

GLC images are saved as their own data structure. One quick workaround is to cast images as an array object, that you can then display as an image with any other software. To do that, assuming the images in your sframe are under column name 'image', then:

from array import array sf['img_array'] = sf['image'].astype(array) This will create a new column with array representation of the image. You can use those instead for plotting.

Let me know if this works for you.

Thanks, Charlie


User 3244 | 2/25/2016, 11:00:56 PM

Hi @cloofa !! Thank you so much for these tips, seriously live saving. I have a column with the pixel data and I am using the GLC method ( rsltimgsarray = gl.SArray.pixelarraytoimage(imgsarray, 32, 32, 3, allow_rounding = True)) to transform the pixel data back to a GL.image object so that I can send it to a Pillow photo utility script.

I have an error: raise TypeError("arraytoimg expects SArray of arrays as input SArray") TypeError: arraytoimg expects SArray of arrays as input SArray

Here is my code: imagearray = np.array(catneighbors) for i, v in enumerate(image_array): print '\n'
b = v['imagearray'] imgsarray = gl.SArray(b) print imgsarray print '\n' rsltimgsarray = gl.SArray.pixelarraytoimage(imgsarray, 32, 32, 3, allowrounding = True) print type(rsltimgsarray)

The image array is a numpy array object! Am I doing this right?

THANK YOU!!


User 2593 | 2/26/2016, 12:57:30 AM

Hi @shai_wilson,

You can use this method as such:

`In [6]: sf Out[6]: Columns: __id int image Image features array arr array

Rows: 2099

Data: +------+------------------------+-------------------------------+ | __id | image | features | +------+------------------------+-------------------------------+ | 19 | Height: 256 Width: 256 | [0.0, 2.71719360352, 0.291... | | 29 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | | 32 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | | 71 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | | 87 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | | 104 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 3.11743330... | | 140 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | | 154 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | | 243 | Height: 256 Width: 256 | [0.0, 0.0, 0.740101158619,... | | 252 | Height: 256 Width: 256 | [0.0, 0.0, 0.0, 0.0, 0.0, ... | +------+------------------------+-------------------------------+ +-------------------------------+ | arr | +-------------------------------+ | [196.0, 183.0, 174.0, 195.... | | [254.0, 255.0, 255.0, 253.... | | [252.0, 253.0, 255.0, 251.... | | [226.0, 218.0, 195.0, 226.... | | [153.0, 166.0, 208.0, 68.0... | | [255.0, 255.0, 255.0, 255.... | | [78.0, 68.0, 56.0, 83.0, 7... | | [185.0, 154.0, 110.0, 186.... | | [140.0, 135.0, 129.0, 147.... | | [153.0, 136.0, 110.0, 156.... | +-------------------------------+ [2099 rows x 4 columns] Note: Only the head of the SFrame is printed. You can use printrows(numrows=m, num_columns=n) to print more rows and columns.

In [7]: sf['image1'] = sf['arr'].pixelarraytoimage( 28, 28, 1, allowrounding = True)`

Just make sure you are using Graphlab Create 1.8.3. There were a bug with this method that may cause the operation to fail sometimes.

Let me know if it works.

Thanks,

charlie