如何修复TypeError:“NoneType”对象没有属性“\uu getitem”

2024-09-29 23:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的opencv与tensorflow,我有一个简单的手势识别笔记本,试图运行在我的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()

Tags: imageimportimgmodeltensorflownploadcv2

热门问题