简体   繁体   中英

Cant properly orginize self method in a class TypeError:create_bool(): incompatible function arguments. The following argument types are supported:

Return Error when Im tried to make a class.

When I tried as here https://github.com/google/mediapipe/blob/master/docs/solutions/face_mesh.md#python-solution-api . everething is perfect

There are some problem with self. method. But I could not undestand where exactly

import cv2
import mediapipe as mp
import time

class FaceMeshDetector:

    def __init__(self, static_mode=False, maxFaces=2, minDetectionCon=0.5, minTrackCon=0.5):
        self.static_mode = static_mode
        self.maxFaces = maxFaces
        self.minDetectionCon = minDetectionCon
        self.minTrackCon = minTrackCon

        self.mpDraw = mp.solutions.drawing_utils
        self.mpFaceMesh = mp.solutions.face_mesh
        self.faceMesh = self.mpFaceMesh.FaceMesh(self.static_mode, self.maxFaces, self.minDetectionCon,
                                                 self.minTrackCon)
        self.drawSpec = self.mpDraw.DrawingSpec(thickness=1, circle_radius=1)

    def findFaceMesh(self, img, draw=True):
        self.imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.faceMesh.process(self.imgRGB)
        faces = []
        if self.results.multi_face_landmarks:
            for faceLms in self.results.multi_face_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, faceLms, self.mpFaceMesh.FACEMESH_CONTOURS, self.drawSpec,
                                               self.drawSpec)

                face = []
                for id, lm in enumerate(faceLms.landmark):
                    # print(lm)
                    ih, iw, ic = img.shape
                    x, y = int(lm.x * iw), int(lm.y * ih)
                    # cv2.putText(img, str(id), (x, y), cv2.FONT_HERSHEY_PLAIN, 0.7, (0, 255, 0), 1)
                    # print(id, x, y)
                    face.append([x, y])
                faces.append(face)
        return img, faces


def main():
    cap = cv2.VideoCapture(0)
    pTime = 0
    detector = FaceMeshDetector()
    while True:
        success, img = cap.read()
        img, faces = detector.findFaceMesh(img)
        if len(faces) != 0:
            print(faces[0])
        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime
        cv2.putText(img, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 3)
        cv2.imshow("Image", img)
        cv2.waitKey(1)


if __name__ == '__main__':
    main()

Full traceback

Traceback (most recent call last): File "C:\Users\Roman\PycharmProjects\pythonProject\FaceMeshModule.py", line 59, in main() File "C:\Users\Roman\PycharmProjects\pythonProject\FaceMeshModule.py", line 44, in main detector = FaceMeshDetector() File "C:\Users\Roman\PycharmProjects\pythonProject\FaceMeshModule.py", line 16, in init self.minTrackCon) File "C:\Users\Roman\PycharmProjects\pythonProject\venv\lib\site-packages\mediapipe\python\solutions\face_mesh.py", line 107, in init outputs=['multi_face_landmarks']) File "C:\Users\Roman\PycharmProjects\pythonProject\venv\lib\site-packages\mediapipe\python\solution_base.py", line 291, in init for name, data in (side_inputs or {}).items() File "C:\Users\Roman\PycharmProjects\pythonProject\venv\lib\site-packages\mediapipe\python\solution_base.py", line 291, in for name, data in (side_inputs or {}).items() File "C:\Users\Roman\PycharmProjects\pythonProject\venv\lib\site-packages\mediapipe\python\solution_base.py", line 592, in make_packet retur n getattr(packet_creator, 'create ' + packet_data_type.value)(data) TypeError: create_bool(): incompatible function arguments. The following argument types are supported: 1. (arg0: bool) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.5

You have a parameter in the wrong place. Use named parameters or add a value for "refine_landmarks".

See signature of FaceMesh:

def __init__(self,
               static_image_mode=False,
               max_num_faces=1,
               refine_landmarks=False,
               min_detection_confidence=0.5,
               min_tracking_confidence=0.5):

Or add the missing parameter:

Change

self.faceMesh = self.mpFaceMesh.FaceMesh(self.static_mode, self.maxFaces, self.minDetectionCon, self.minTrackCon)

to

self.faceMesh = self.mpFaceMesh.FaceMesh(self.static_mode, self.maxFaces, False, self.minDetectionCon, self.minTrackCon)

Finally done and work

def __init__(self,
                 static_image_mode=False,
                 max_num_faces=2,
                 refine_landmarks=False,
                 min_detection_confidence=0.5,
                 min_tracking_confidence=0.5):
        self.static_mode = static_image_mode
        self.maxFaces = max_num_faces
        self.refine_landmarks = refine_landmarks
        self.minDetectionCon = min_detection_confidence
        self.minTrackCon = min_tracking_confidence
    
        self.mpDraw = mp.solutions.drawing_utils
        self.mpFaceMesh = mp.solutions.face_mesh
        self.faceMesh = self.mpFaceMesh.FaceMesh(self.static_mode, 
                                                 self.maxFaces, 
                                                 self.refine_landmarks, 
                                                 self.minDetectionCon,
                                                 self.minTrackCon)
        self.drawSpec = self.mpDraw.DrawingSpec(thickness=1, circle_radius=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM