TypeError: list indices must be integers or slices, not tuple - when converting RGB to gray scale

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

TypeError: list indices must be integers or slices, not tuple - when converting RGB to gray scale

jeff.pape
This post has NOT been accepted by the mailing list yet.
Hi Python Experts:

I would appreciate some help with the python error I am encountering.
Honestly, I do not understand what the issue is and my google searches have not produced any useful results.

I would be in your debt.

Thank you in advance for the help.

I am making progress on the traffic sign classifier project, but i am stuck; i could use some python help.

i found 5 additional traffic signs.
I can read the signs in as png or jpg fine.

However, the images are stored internally in a slightly different format than the original image data.

I see that the png images have RGBA - i.e. additional alpha channel. I am not sure if that is causing an issue.

When i use my method to convert them to gray scale - and correct the intensity, the notebook complains:

def convert_to_grayscale_and_correct_intensity(X, y = None):
    # convert rgb to ycbcr - but only need y = gray scale
    X = 0.299 * X[:, :, :, 0] + 0.587 * X[:, :, :, 1] + 0.114 * X[:, :, :, 2]
    # scale features to be in [0, 1]
    X = (X / 255).astype(np.float32)
   
    # apply localized histogram localization
    for index in range(X.shape[0]):
        X[index] = skimage.exposure.equalize_adapthist(X[index])

    # add single gray scale channel
    X = X.reshape(X.shape + (1,))
    return X, y


X_my_signs_gray, y_my_signs_gray = convert_to_grayscale_and_correct_intensity(X_my_signs, y_my_signs)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-46a667a87ad7> in <module>()
----> 1 X_my_signs_gray, y_my_signs_gray = convert_to_grayscale_and_correct_intensity(X_my_signs, y_my_signs)

<ipython-input-17-71b7d381f7cb> in convert_to_grayscale_and_correct_intensity(X, y)
      4 def convert_to_grayscale_and_correct_intensity(X, y = None):
      5     # convert rgb to ycbcr - but only need y = gray scale
----> 6     X = 0.299 * X[:, :, :, 0] + 0.587 * X[:, :, :, 1] + 0.114 * X[:, :, :, 2]
      7     # scale features to be in [0, 1]
      8     X = (X / 255).astype(np.float32)

TypeError: list indices must be integers or slices, not tuple


=====
how is how I import my additional sign image  data
import scipy

from scipy import misc

my_priority_road_file = 'my-traffic-signs-data/priority.png'
my_roundabout_file = 'my-traffic-signs-data/roundabout.png'
my_slippery_road_file = 'my-traffic-signs-data/slippery_road.png'
my_speed_limit_50_file = 'my-traffic-signs-data/speed_limit_50.png'
my_speed_limit_60_file = 'my-traffic-signs-data/speed_limit_60.png’

my_speed_limit_50 = scipy.misc.imread(my_speed_limit_50_file)
my_speed_limit_50_label = 2
my_speed_limit_60 = scipy.misc.imread(my_speed_limit_60_file)
my_speed_limit_60_label = 3
my_priority_road = scipy.misc.imread(my_priority_road_file)
my_priority_road_label = 12
my_slippery_road = scipy.misc.imread(my_slippery_road_file)
my_slippery_road_label = 23
my_roundabout = scipy.misc.imread(my_roundabout_file)
my_roundabout_label = 40
X_my_signs = [my_speed_limit_50, my_speed_limit_60, my_priority_road, my_slippery_road, my_roundabout]
y_my_signs = [my_speed_limit_50_label, my_speed_limit_60_label, my_priority_road_label, my_slippery_road_label, my_roundabout_label]

# here i try to make the data structures lists, but this does not help.
def listit(t):
    return list(map(listit, t)) if isinstance(t, (list, tuple)) else t

my_speed_limit_50_image = listit(my_speed_limit_50)
my_speed_limit_60_image = listit(my_speed_limit_60)
my_priority_road_image = listit(my_priority_road)
my_slippery_road_image = listit(my_slippery_road)
my_roundabout_image = listit(my_roundabout)

X_my_signs = [my_speed_limit_50_image, my_speed_limit_60_image, my_priority_road_image, my_slippery_road_image, my_roundabout_image]
y_my_signs = [my_speed_limit_50_label, my_speed_limit_60_label, my_priority_road_label, my_slippery_road_label, my_roundabout_label]

===
the original sign data is imported this way and the method: convert_to_grayscale_and_correct_intensity process these structures (X_train) happily:

training_file = 'traffic-signs-data/train.p'
testing_file = 'traffic-signs-data/test.p'
validation_file= 'traffic-signs-data/valid.p'

with open(training_file, mode='rb') as f:
    train = pickle.load(f)
with open(validation_file, mode='rb') as f:
    valid = pickle.load(f)
with open(testing_file, mode='rb') as f:
    test = pickle.load(f)

X_train, y_train, sizes_train, coords_train = train['features'], train['labels'], train['sizes'], train['coords']    
X_valid, y_valid, sizes_valid, coords_valid = valid['features'], valid['labels'], valid['sizes'], valid['coords']    
X_test,  y_test,  sizes_test,  coords_test  = test['features'], test['labels'], test['sizes'], test['coords']    
Loading...