You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于OpenCV与MediaPipe的俯卧撑计数器摄像头窗口无法打开求助

俯卧撑计数器摄像头窗口无法打开问题排查

运行基于OpenCV和MediaPipe的Python俯卧撑计数器项目时,摄像头指示灯已亮起,但摄像头窗口始终无法打开,以下是问题排查和修复方案:

原代码

import cv2
import mediapipe as md

md_drawing=md.solutions.drawing_utils
md_drawing_styles=md.solutions.drawing_styles
md_pose=md.solutions.pose

count = 0

position=None

cap=cv2.VideoCapture(0)

with md_pose.Pose(
    min_detection_confidence=0.7,
    min_tracking_confidence=0.7) as pose:
    while cap.isOpened():
        success,image=cap.read()
        if not success:
            print("empty camera")
            break

            image=cv2.cvtColor(cv2.flip(image,1),cv2.COLOR_BGR2RGB)
            result=pose.process(image)
            
            imlist=[]

            if result.pose_landmarks:
                md_drawing.draw_landmarks(
                    image,result.pose_landmarks,md_pose.POSE_CONNECTIONS)
                for id,im in enumerate(result.pose_landmarks.landmark):
                    h,w,_=image.shape
                    X,Y=int(im.x*w),int(im.y*h)
                    imlist.append([id,X,Y])
            #print(imlist)


            if Len(imlist) != 0:
                if((imlist[12][2]-imlisit[14][2]) >= 15 and (imlist[11][2]-imlist[13][2])>=15):
                    position="down"
                if((imlist[12][2]-imlisit[14][2]) <= 5 and (imlist[11][2]-imlist[13][2])<=5) and position == "down":
                    position="up"
                    count+=1
                    print(count)

            cv2.imshow("Push-up counter",cv2.flip(image,1))
            key=cv2.waitKey(1)
            if key==ord('q'):
                break

cap.release()

问题分析与修复

  • 缩进错误:if not success:分支中,break语句后的所有代码都被错误缩进,导致这些代码永远不会执行(包括窗口显示的cv2.imshow)。需要将break后的代码移出该分支,与if not success:同级。
  • 拼写错误:
    • 检查列表长度的Len应为小写的len(Python内置函数区分大小写);
    • 多处出现的imlisit是拼写错误,需修正为imlist。
  • 窗口销毁遗漏:代码末尾缺少cv2.destroyAllWindows(),可能导致窗口无法正常释放或显示。

修正后的完整代码

import cv2
import mediapipe as md

md_drawing=md.solutions.drawing_utils
md_drawing_styles=md.solutions.drawing_styles
md_pose=md.solutions.pose

count = 0
position=None

cap=cv2.VideoCapture(0)

with md_pose.Pose(
    min_detection_confidence=0.7,
    min_tracking_confidence=0.7) as pose:
    while cap.isOpened():
        success,image=cap.read()
        if not success:
            print("empty camera")
            break

        # 修正缩进:移出if not success分支
        image=cv2.cvtColor(cv2.flip(image,1),cv2.COLOR_BGR2RGB)
        result=pose.process(image)
        
        imlist=[]

        if result.pose_landmarks:
            md_drawing.draw_landmarks(
                image,result.pose_landmarks,md_pose.POSE_CONNECTIONS)
            for id,im in enumerate(result.pose_landmarks.landmark):
                h,w,_=image.shape
                X,Y=int(im.x*w),int(im.y*h)
                imlist.append([id,X,Y])

        # 修正拼写错误:Len -> len,imlisit -> imlist
        if len(imlist) != 0:
            if((imlist[12][2]-imlist[14][2]) >= 15 and (imlist[11][2]-imlist[13][2])>=15):
                position="down"
            if((imlist[12][2]-imlist[14][2]) <= 5 and (imlist[11][2]-imlist[13][2])<=5) and position == "down":
                position="up"
                count+=1
                print(count)

        cv2.imshow("Push-up counter",cv2.flip(image,1))
        key=cv2.waitKey(1)
        if key==ord('q'):
            break

cap.release()
cv2.destroyAllWindows()  # 添加窗口销毁语句

内容的提问来源于stack exchange,提问作者Mohit Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 20:31:05