【发布时间】: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