运行程序时键入错误

时间:2018-07-30 04:47:19

标签: python oop

我正在尝试创建一个程序,该程序可以检测面部并在直播中预测其性别。但是我在运行程序时收到此错误消息

Traceback (most recent call last):
  File "text.py", line 54, in <module>
    cus_ana.prediction()
  File "text.py", line 31, in prediction
    gender = gender_classifier()
TypeError: __call__() missing 1 required positional argument: 'inputs'

这是到目前为止的代码

class CusAnalytics():

    def __init__(self, cascade, gender):
        self.cascade = cascade
        self.gender = gender

    def gender_classifier(self):
        classifier = self.gender
        gender = classifier.predict(np.expand_dims(cv2.resize(cropped, (198, 198)), axis=0))
        gender = np.where(gender.flatten() < 0.5, "Female", "Male")

    def prediction(self):
        cam = cv2.VideoCapture(0)

        while True:
            ret, frame = cam.read()
            frame = cv2.flip(frame, 1)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = self.cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(150, 150))

            for (x, y, w, h) in faces:
                cropped = cv2.resize(frame[y:y+h, x:x+w], (198,198))

                gender = gender_classifier()

                if gender[0] == 'Male':
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
                    cv2.rectangle(frame, (x-1, y+h), (x+w+1, y+h+50), (255, 0, 0), -1)
                    cv2.rectangle(frame, (x-1, y), (x+w+1, y-50), (255, 0, 0), -1)

                else:
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
                    cv2.rectangle(frame, (x-1, y+h), (x+w+1, y+h+50), (0, 0, 255), -1)
                    cv2.rectangle(frame, (x-1, y), (x+w+1, y-50), (0, 0, 255), -1)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

            cv2.imshow('Stream', frame)

cascadePath = "../../../MODEL/TRAINED MODELS/FACE_DETECTION/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
gender_classifier_path = '../../../MODEL/TRAINED MODELS/GENDER_CLASSIFICATION/best_gender_classifier.h5'
gender_classifier = load_model(gender_classifier_path)

cus_ana = CusAnalytics(faceCascade, gender_classifier)
cus_ana.prediction()

无需执行OOPS,代码即可正常工作。我是面向对象编程的新手,正在尝试学习该项目。

提前谢谢

0 个答案:

没有答案