1

I am working on some code to recognises the face and speak someone's name. I am working this code in raspberry pi 3 with opencv 3.1 and python 2.7. This code works fine on windows but when I try it on raspberry, it gives an error:

Type error: 'int' object has no attriibute '__getitem__'

For the line:

for line 'if prediction[1]<100'

Full code:

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
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Sara
  • 11
  • 2
  • 2
    It looks like `model.predict()` returns an `int`, and it looks like you're expecting it to be a `list`: `prediction[1]` . Perhaps this is because the OpenCV changed this in different versions? Check the documentation for the version you're using. – Martin Tournoij Aug 14 '16 at 13:07
  • [It's a bug in `cv2`](/questions/34617279/opencvcant-get-predict-confidence-on-facerecognizer). –  Aug 14 '16 at 13:23
  • I degraded the version of opencv to 2.4.10 and now working on raspberry2 but it shows "Cannot connect to server socket err = No such file or directory Jack server is not running or cannot be started" and stops the loop before engine.runAndWait(). @Carpetsmoker – Sara Aug 23 '16 at 08:47

0 Answers0