如何修复TypeError:'NoneType'对象没有属性'__getitem__'

时间:2019-08-12 16:21:27

标签: python opencv tensorflow keras

我对使用tensorflow的opencv是陌生的,我有一个简单的手势识别笔记本,试图在我的jupyter上运行。运行内核后,Windows加载不到两分钟,然后出现错误

TypeError                                 Traceback (most recent call last)
<ipython-input-10-2396a75050b8> in <module>()


----> 8     roi=vidimg[0:224,0:224]



TypeError: 'NoneType' object has no attribute '__getitem__'

我该如何解决

使用MacOS

#full code 

import cv2

import numpy as np

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras.models import load_model

import os

handornot_model=load_model("handornot.h5")
hand_model=load_model("handgesture.h5")


def predict_image(image):
    return(hand_model.predict(image))


def predict_handornot(image):
    return(handornot_model.predict(image))




k=0

cap=cv2.VideoCapture(0)

while True:
    _,vidimg=cap.read()

    cv2.rectangle(vidimg,(0,0),(224,224),(225,0,0),4)

    roi=vidimg[0:224,0:224]

    cv2.imwrite("tempdata//{}.jpg".format(k),roi)
    img=cv2.imread("tempdata//{}.jpg".format(k))

    img=img[:,:,::-1]
    img2=img.reshape((1,img.shape[0],img.shape[1],img.shape[2]))

    handornot=predict_handornot(img2)
    result=str(np.argmax(handornot))

    if result=="0":
        writeonimg="Please show your hand"
    else:
        result2=predict_image(img2)
        writeonimg=str(np.argmax(result)+1)

    os.remove("tempdata//{}.jpg".format(k))
    k+=1

    cv2.putText(vidimg,writeonimg,(110,250),cv2.FONT_HERSHEY_COMPLEX,1, 
     (225,225,0),2)

    cv2.imshow("vidimg",vidimg)
    cv2.imshow("roi",roi)

    if cv2.waitKey(2) & 0xFF==ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

0 个答案:

没有答案