Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python int对象没有属性uu getitem___Python_Python 2.7_Opencv_Opencv3.1 - Fatal编程技术网

Python int对象没有属性uu getitem__

Python int对象没有属性uu getitem__,python,python-2.7,opencv,opencv3.1,Python,Python 2.7,Opencv,Opencv3.1,我正在编写一些代码来识别人脸和说出某人的名字。我正在使用raspberry pi 3和opencv 3.1以及python 2.7编写这段代码。这段代码在windows上运行良好,但当我在raspberry上尝试时,它给出了一个错误: Type error: 'int' object has no attriibute '__getitem__' 对于线路: for line 'if prediction[1]<100' for line'如果预测[1]它看起来像是model.pred

我正在编写一些代码来识别人脸和说出某人的名字。我正在使用raspberry pi 3和opencv 3.1以及python 2.7编写这段代码。这段代码在windows上运行良好,但当我在raspberry上尝试时,它给出了一个错误:

Type error: 'int' object has no attriibute '__getitem__'
对于线路:

for line 'if prediction[1]<100'

for line'如果预测[1]它看起来像是
model.predict()
返回一个
int
,并且它看起来像是一个
列表
预测[1]
。也许这是因为OpenCV在不同版本中改变了这一点?检查您正在使用的版本的文档..我将opencv的版本降级为2.4.10,现在正在使用raspberry2,但它显示“无法连接到服务器套接字错误=没有这样的文件或目录Jack服务器未运行或无法启动”,并在engine.runAndWait()之前停止循环@它看起来像是
model.predict()
返回一个
int
,看起来像是一个
列表
prediction[1]
。也许这是因为OpenCV在不同版本中改变了这一点?检查您正在使用的版本的文档..我将opencv的版本降级为2.4.10,现在正在使用raspberry2,但它显示“无法连接到服务器套接字错误=没有这样的文件或目录Jack服务器未运行或无法启动”,并在engine.runAndWait()之前停止循环@吸烟者
import cv2, sys, numpy, os, pyttsx, time,picamera
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'datasets'

engine = pyttsx.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-40)

print('Training...')
# Create a list of images and a list of corresponding names
(images, labels, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(datasets):
    for subdir in dirs:
        names[id] = subdir
        subjectpath = os.path.join(datasets, subdir)
        for filename in os.listdir(subjectpath):
            path = subjectpath + '/' + filename
            label = id
            images.append(cv2.imread(path, 0))
            labels.append(int(label))
        id += 1
(width, height) = (130, 100)

# Create a Numpy array from the two lists above
(images, labels) = [numpy.array(lis) for lis in [images, labels]]

model = cv2.createLBPHFaceRecognizer()
model.train(images, labels)

#use LBPHFace recognizer on camera frame
face_cascade = cv2.CascadeClassifier(haar_file)
camera = picamera.PiCamera()
camera.resolution = (320, 240)

def getFrame():
    jpegBuffer = io.BytesIO()
    camera.capture(jpegBuffer, format='jpeg')
    buff = numpy.fromstring(jpegBuffer.getvalue(), dtype=numpy.uint8)
    return cv2.imdecode(buff, 1)

while True:
    im = fetFrame()
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
        face = gray[y:y + h, x:x + w]
        face_resize = cv2.resize(face, (width, height))
        #Try to recognize the face
        prediction = model.predict(face_resize)
        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)

        if prediction[1]<100:
            cv2.putText(im,'%s - %.0f' % (names[prediction[0]],prediction[1]),(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
            engine.say('Hello')
            engine.say(names[prediction[0]],prediction[1])
            time.sleep(3)
        else:
          cv2.putText(im,'not recognized',(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
          engine.say('Hello, Newface')
          time.sleep(3)

    engine.runAndWait()

    cv2.imshow('OpenCV', im)
    key = cv2.waitKey(10)
    if key == 27:
        break