简体   繁体   中英

How to create a dictionary with keys as integer and definition as a numpy array?

I have created a CNN model that can predict hand signs. I have 10 classes ie. 0-9. the output of model.predict_classes() is a numpy array

ex. prediction of 5 is [5].

I want to an integer to be returned so that I can continue my calculations.

cam = cv2.VideoCapture(0)
depth = 0
ps = 0
start_pros = 0
classifier = load_model('hand_gest.h5')
classifier.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
number = []



def occurrence(lt):
    for num, sublist in groupby(lst):
        if len(list(sublist)) == 25:
            return num

while(1):
    try:
        ret, frame = cam.read()
        frame = cv2.flip(frame,1)
        roi = frame[100:400,200:500]
        cv2.rectangle(frame,(200,100),(500,400),(0,255,0),2) 
        cv2.imshow('curFrame',frame)
        cv2.imshow('roi',roi)


        img = cv2.resize(roi,(100,100))
        img = np.reshape(img,[1,100,100,3]) 
        classes = classifier.predict_classes(img)
        number = number.append(classes)
        pred_num = occurrence(number)    

This is the error that occurs during execution.

AttributeError: 'NoneType' object has no attribute 'append'

It's not number = number.append(classes) , you need to just call append:

number.append(classes)

append modifies in-place the list and returns None , so in your code the second time the loop runs number will be None and thus the error happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM