miguelote
miguelote

Reputation: 51

Evaluation function inside numpy indexing array with PIL images

I'm working on image segmentation using PIL where I'm using a nested iteration to index the image, but it runs very slow.

def evalPixel((r,g,b), sess):
    pixel = [float(r)/255, float(g)/255, float(b)/255]
    test = sess.run(y, feed_dict={x: [pixel]})
    return test[0][0]

...
...

# sess = sesion loaded from TensorFlow
rgb = Image.open("face.jpg")
height, width = rgb.size

for y in range(height):
    for x in range(width):
        if (evalPixel(rgb.getpixel((x,y)), sess) < 0.6 ):
            rgb.putpixel((x,y), 0)

toimage(im).show()

I want to do something like this, using advanced indexing of numpy

im = np.array(rgb)
im[ evalPixel(im, sess) < 0.6 ] = 0

But, it fails with "ValueError: too many values to unpack". How can I do that?

Upvotes: 0

Views: 216

Answers (2)

physicalattraction
physicalattraction

Reputation: 6858

Your function evalPixel takes as first argument a tuple, but your numpy array does not contain (and cannot contain) tuples. You have to rewrite that function to be able to work with numpy arrays.

I tried to make a working example for you, but the code you're sharing contains a lot of unknown variables (you left out too much) and it is not clear to me what the evalPixel function should do.

Upvotes: 0

Naveen Arun
Naveen Arun

Reputation: 329

Try using the following:

im = np.array(rgb)
im = [[evalPixel(x,sess) < 0.6 for x in row] for row in im]

By using constructors to generate rows and columns, it's possible to avoid accidentally applying a function with a single argument (in this case, a tuple) to an entire row or column.

Upvotes: 1

Related Questions