TypeError: unsupported operand type(s) for -: 'tuple' and 'int'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spriya
    New Member
    • Sep 2016
    • 1

    TypeError: unsupported operand type(s) for -: 'tuple' and 'int'

    Traceback (most recent call last):
    File "main.py", line 55, in <module>
    res=names[recognizer.pred ict(gray[y:y+h, x:x+w])-1]
    TypeError: unsupported operand type(s) for -: 'tuple' and 'int'


    This is my error . and the code is

    import cv2
    import numpy as np
    import os
    import serial
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    cascadePath = "haarcascade_fr ontalface_defau lt.xml"
    faceCascade = cv2.CascadeClas sifier(cascadeP ath)
    recognizer=cv2. face.createLBPH FaceRecognizer( )
    #Loading each image and cropping the face preparing the dataset
    # for filename in os.listdir('Dat aset'):
    # im=cv2.imread(' Dataset/'+filename,0)
    # print filename
    # faces = faceCascade.det ectMultiScale(i m, 1.3, 5)
    # for (x,y,w,h) in faces:
    # cv2.imwrite('Da taset/'+filename,im[y:y+h, x:x+w])

    images=[]
    labels=[]
    for filename in os.listdir('Dat aset'):
    im=cv2.imread(' Dataset/'+filename,0)
    images.append(i m)
    labels.append(i nt(filename.spl it('.')[0][0]))
    #print filename
    names_file=open ('labels.txt')
    names=names_fil e.read().split( '\n')
    recognizer.trai n(images,np.arr ay(labels))
    print 'Training Done . . . '
    font = cv2.FONT_HERSHE Y_SIMPLEX
    cap=cv2.VideoCa pture(1)
    lastRes=''
    count=0
    while(1):
    _,frame=cap.rea d()
    gray=cv2.cvtCol or(frame,cv2.CO LOR_BGR2GRAY)
    faces = faceCascade.det ectMultiScale(g ray, 1.3, 5)
    count+=1
    for (x,y,w,h) in faces:
    cv2.rectangle(f rame,(x,y),(x+w ,y+h),(255,0,0) ,2)
    if count>20:
    res=names[recognizer.pred ict(gray[y:y+h, x:x+w])-1]
    if res!=lastRes :
    lastRes=res
    print lastRes
    ser.write(lastR es)
    count=0
    break


    cv2.imshow('fra me',frame)
    k = 0xFF & cv2.waitKey(10)
    if k == 27:
    break
    cap.release()
    ser.close()
    cv2.destroyAllW indows()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    The error message should be self explanatory. You are trying to do arithmetic on a tuple and an integer (you can only do arithmetic on types that are the same as a string + is different from an integer +). You will have to go through these calcs one at a time to see where the error is
    Code:
    gray[y:y+h, x:x+w])-1]

    Comment

    Working...