将 OpenCV 程序转换为模块会大大降低帧率

Converting an OpenCV program into a Module reduces frame rate drastically

我使用 OpenCV 和 mediapipe 库编写了姿势估计代码。该程序运行良好,我的速度大约为 30-35 fps。当我试图将同一个程序转换为一个模块以便将来可以轻松地用于不同的项目时,新代码(模块)的 fps 急剧下降到 3-4 fps。 我的原始程序:

import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture(1)
pTime = 0
cTime = 0

mpDraw = mp.solutions.drawing_utils
mpPose = mp.solutions.pose
pose = mpPose.Pose()  
   
while True:
    success, img1 = cap.read()
    img = cv2.flip(img1, 1)  

    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = pose.process(imgRGB)

    if results.pose_landmarks:
        mpDraw.draw_landmarks(img, results.pose_landmarks, mpPose.POSE_CONNECTIONS)

        for id, lm in enumerate(results.pose_landmarks.landmark):
            h, w, c = img.shape
            cx, cy = int(lm.x*w), int(lm.y*h)
            cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
        
    cTime = time.time()
    fps = 1/(cTime - pTime)
    pTime = cTime

    cv2.putText(img, "FPS : " + str(int(fps)), (10, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 8), 2)

    cv2.imshow("Live Feed", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

我尝试将其转换为模块:

import cv2
import mediapipe as mp
import time

class poseDetector():
    def __init__(self, mode=False, upBody=False, smooth=True, detectionCon = 0.5, trackingCon=0.5):
        self.mode = mode
        self.upBody = upBody
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackingCon = trackingCon

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

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.pose.process(imgRGB)

        if self.results.pose_landmarks:
            if draw:
                 self.mpDraw.draw_landmarks(img, self.results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)
    return img

    def findPosition(self, img, draw=True):
        lmList = []
        if self.results.pose_landmarks:
            for id, lm in enumerate(self.results.pose_landmarks.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x*w), int(lm.y*h)
                lmList.append([id, cx, cy])
                if draw:
                    cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
        return lmList

def main():
    cap = cv2.VideoCapture(1)
    pTime = 0
    cTime = 0

    while True:
        success, img1 = cap.read()
        img = cv2.flip(img1, 1)

        detector = poseDetector()
        img = detector.findPose(img) 
        lmList = detector.findPosition(img)

        cTime = time.time()
        fps = 1/(cTime - pTime)
        pTime = cTime

        cv2.putText(img, "FPS : " + str(int(fps)), (10, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 8), 2)

        cv2.imshow("Live Feed", img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break 

if __name__ == '__main__':
    main()

根据我的说法,这两个代码应该以相同的方式工作,但它们不是。谁能告诉我哪里出错了?

您需要将 detector = poseDetector() 放在 while True::

之前
detector = poseDetector()

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

您的“模块”实现在主循环的每次迭代中创建一个新的 poseDetector 对象。
detector = poseDetector() 的每次执行都包含对 poseDetector.__init__ 的调用,后者调用 self.pose =self.mpPose.Pose...
开销很大...

while True:
    success, img1 = cap.read()
    img = cv2.flip(img1, 1)

    detector = poseDetector()
    ...

在您原来的(“非模块”)实现中,您只执行了一次 pose = mpPose.Pose()(在循环之前)。

pose = mpPose.Pose()  
   
while True:
    success, img1 = cap.read()
    ...

我在将 detector = poseDetector() 移出循环之前和之后测试了您的代码。
移动循环上方的线后,帧速率与“非模块”实现相同。