【问题标题】:findPose() missing 1 required positional argument: 'self' [closed]findPose() 缺少 1 个必需的位置参数:'self' [关闭]
【发布时间】:2021-09-14 07:21:45
【问题描述】:
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, 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 分钟。你能帮帮我吗

【问题讨论】:

  • 您需要实例化postDetector,即detector = poseDetector(),而不是在main方法内完成的detector = poseDetector
  • 同样在Python 3中,class poseDetector():中不需要写空括号

标签: python pycharm cv2 mediapipe


【解决方案1】:

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

detector = poseDetector()

【讨论】:

  • 非常感谢它确实有效
猜你喜欢
  • 1970-01-01
  • 2021-12-15
  • 2023-02-06
  • 1970-01-01
  • 2021-09-15
  • 2013-07-06
  • 2022-01-05
  • 2017-03-28
相关资源
最近更新 更多