我无法解决错误TypeError:'str'对象不可调用

时间:2019-04-03 16:33:39

标签: python-3.x

TypeError:'str'对象是不可调用的。我无法在函数中传递字符串作为参数。我创建了一个函数insertOrUpdate()。然后我试图传递ID和Name这些是字符串类型。收到了typeError。

这是我的代码

   # -*- coding: utf-8 -*-
"""
Created on Fri Jan 18 00:25:50 2019

@author: Asus
"""

import cv2
import numpy as np
import pymysql

faceDetect = cv2.CascadeClassifier('D:\Face rec\haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)

def insertOrUpdate(ID, Name):
    connection = pymysql.connect(host="localhost", user="root", passwd="", database="mydata")
    cmd = "SELECT * FROM face_rec WHERE Id=" + str(ID)
    cursor = connection.cursor(cmd)
    isRecordExist = 0
    for row in cursor:
        isRecordExist = 1
    if(isRecordExist == 1):
        cmd = "UPDATE face_rec SET name=" + str(Name) + "WHERE Id=" + str(ID)
    else:
        cmd = "INSERT INTO face_rec(Id,name) Values(" + str(ID) + "," + str(Name) + ")"
    connection.execute(cmd)
    connection.commit()
    connection.close()

ID = input("enter your id:")
Name = input("enter your name:")
print(str(ID), str(Name))
print(type(ID), type(Name))
insertOrUpdate(ID, Name)
sampleNum = 0

while(cam.isOpened()):
    ret, img = cam.read()         
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceDetect.detectMultiScale(gray, 1.3,5 )
    for (x, y, w, h) in faces:
        sampleNum = sampleNum + 1
        cv2.imwrite("Dataset/user." + str(ID) + "." + str(sampleNum) + ".jpg", gray[y:y+h, x:x+w])
        cv2.rectangle(img, (x,y), (x + w,y + h), (255, 0, 0), 2)
        cv2.waitKey(100)
    cv2.imshow("Faces", img)
    cv2.waitKey(1)
    if(sampleNum > 20):
        break

cam.release()
cv2.destroyAllWindows()        
  

python

0 个答案:

没有答案