findPose() 缺少 1 个必需的位置参数:'self'

问题描述

import cv2 

import time

import mediapipe as mp

def main():

    cap = cv2.VideoCapture(0)
    pTime = 0  # PrevIoUs time
    global img
    detector = poseDetector

    while True:
        success,img = cap.read()

        detector.findPose()
        cTime = time.time()
        print(cTime,pTime)
        fps = 1 / (cTime - pTime)
        pTime = cTime

        cv2.putText(img,str(int(fps)),(70,50),cv2.FONT_HERShey_SCRIPT_COMPLEX,3,(255,0),3)

        cv2.imshow("Image",img)
        cv2.waitKey(1)


class poseDetector():

    def __init__(self,mode=True,complexity=1,smooth=True,detectionCon=0.5,trackCon=0.5):
        self.mode = mode
        self.complexity = complexity
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackCon = trackCon

        self.mpDraw = mp.solutions.drawing_utils
        self.mpPose = mp.solutions.pose
        self.pose = self.mpPose.Pose(self.mode,self.complexity,self.smooth,self.detectionCon,self.trackCon)

    def findPose(self,draw=True):
        imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        results = self.pose.process(imgRGB)
        if results.pose_landmarks and draw:
            self.mpDraw.draw_landmarks(img,results.pose_landmarks,self.mpPose.POSE_CONNECTIONS)


if __name__ == "__main__":
    global img
    main()

我在 Pycharm 中收到此消息 早些时候,当我没有将 img 声明为全局时,它曾经是 img 作为缺少的参数。 我已经搜索了很多它说pycharm除了列表,元组之外没有其他参数...... 代码来源来自Advanced computer vision。时间线是 1 小时 18 分钟。你能帮我吗

解决方法

由于poseDetector()是一个类,为了使检测器成为这个类的一个实例,它需要在全局img之后对def main()进行小的更改,如下所示:

detector = poseDetector()