66
77# 姿势检测类
88class poseDetector :
9- def __init__ (
10- self , mode = False , upBody = False , smooth = True , detectionCon = 0.5 , trackCon = 0.5
11- ):
12- # 初始化参数
13- self .mode = mode # 静态图像模式
14- self .upBody = upBody # 是否只检测上半身
15- self .smooth = smooth # 平滑处理
16- self .detectionCon = detectionCon # 检测置信度
17- self .trackCon = trackCon # 跟踪置信度
18- self .mpDraw = mp .solutions .drawing_utils # Mediapipe 绘图工具
19- self .mpPose = mp .solutions .pose # Mediapipe 姿势检测模块
9+ # 以下为老版本代码,部分参数已经弃用
10+ # def __init__(
11+ # self, mode=False, upBody=False, smooth=True, detectionCon=0.5, trackCon=0.5
12+ # ):
13+ # # 初始化参数
14+ # self.mode = mode # 静态图像模式
15+ # self.upBody = upBody # 是否只检测上半身
16+ # self.smooth = smooth # 平滑处理
17+ # self.detectionCon = detectionCon # 检测置信度
18+ # self.trackCon = trackCon # 跟踪置信度
19+ # self.mpDraw = mp.solutions.drawing_utils # Mediapipe 绘图工具
20+ # self.mpPose = mp.solutions.pose # Mediapipe 姿势检测模块
21+ # self.pose = self.mpPose.Pose(
22+ # self.mode, self.upBody, self.smooth, self.detectionCon, self.trackCon
23+ # )
24+
25+ def __init__ (self , static_image_mode = False , model_complexity = 1 , enable_segmentation = False , min_detection_confidence = 0.5 , min_tracking_confidence = 0.5 ):
26+ # 初始化姿势检测器的参数
27+ self .static_image_mode = static_image_mode # 是否处理静态图像,False 表示处理视频流
28+ self .model_complexity = model_complexity # 模型复杂度,0、1 或 2,越高越准确但更慢
29+ self .enable_segmentation = enable_segmentation # 是否启用分割功能
30+ self .min_detection_confidence = min_detection_confidence # 姿势检测的最小置信度
31+ self .min_tracking_confidence = min_tracking_confidence # 姿势跟踪的最小置信度
32+
33+ # 设置 MediaPipe 工具
34+ self .mpDraw = mp .solutions .drawing_utils # MediaPipe 绘图工具
35+ self .mpPose = mp .solutions .pose # MediaPipe 姿势估计模块
36+
37+ # 创建姿势估计对象
2038 self .pose = self .mpPose .Pose (
21- self .mode , self .upBody , self .smooth , self .detectionCon , self .trackCon
39+ static_image_mode = self .static_image_mode , # 静态图像模式设置
40+ model_complexity = self .model_complexity , # 模型复杂度设置
41+ enable_segmentation = self .enable_segmentation , # 分割功能设置
42+ min_detection_confidence = self .min_detection_confidence , # 检测置信度阈值
43+ min_tracking_confidence = self .min_tracking_confidence # 跟踪置信度阈值
2244 )
2345
46+
2447 def findPose (self , img , draw = True ):
2548 # 将图像从 BGR 转换为 RGB
2649 imgRGB = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
@@ -52,12 +75,20 @@ def findAngle(self, img, p1, p2, p3, draw=True):
5275 x1 , y1 = self .lmList [p1 ][1 :]
5376 x2 , y2 = self .lmList [p2 ][1 :]
5477 x3 , y3 = self .lmList [p3 ][1 :]
78+
79+ # x1, y1 = self.lmList[p1][1], self.lmList[p1][2]
80+ # x2, y2 = self.lmList[p2][1], self.lmList[p2][2]
81+ # x3, y3 = self.lmList[p3][1], self.lmList[p3][2]
82+
5583 # 计算角度
5684 angle = math .degrees (
5785 math .atan2 (y3 - y2 , x3 - x2 ) - math .atan2 (y1 - y2 , x1 - x2 )
5886 )
87+ # 两个向量 (x1-x2, y1-y2), (x3-x2, y3-y2)
88+ # 使用 atan2 计算每个向量与 x 轴的角度
5989 if angle < 0 :
6090 angle += 360
91+
6192 # 绘制
6293 if draw :
6394 cv2 .line (img , (x1 , y1 ), (x2 , y2 ), (255 , 255 , 255 ), 3 )
@@ -82,12 +113,20 @@ def findAngle(self, img, p1, p2, p3, draw=True):
82113
83114# 主函数
84115def main ():
85- cap = cv2 .VideoCapture ("PoseVideos/1.mp4" ) # 打开视频文件
116+ cap = cv2 .VideoCapture (
117+ "E:\\ Advance Computer Vision with Python\\ Chapter 2 Pose Estimation\\ PoseVideos\\ 5.mp4"
118+ ) # 打开视频文件
86119 pTime = 0 # 前一帧时间
87120 detector = poseDetector () # 创建姿势检测器
88121 while True :
89122 success , img = cap .read () # 读取视频帧
123+ if not cap .isOpened ():
124+ print ("Error: Could not open video." )
125+ return
90126 img = detector .findPose (img ) # 检测姿势
127+ if not success :
128+ print ("Failed to read frame" )
129+ break
91130 lmList = detector .findPosition (img , draw = False ) # 获取关键点位置
92131 if len (lmList ) != 0 :
93132 print (lmList [14 ]) # 打印关键点信息
@@ -98,6 +137,7 @@ def main():
98137 cv2 .putText (
99138 img , str (int (fps )), (70 , 50 ), cv2 .FONT_HERSHEY_PLAIN , 3 , (255 , 0 , 0 ), 3
100139 )
140+ cv2 .namedWindow ("Image" , cv2 .WINDOW_NORMAL ) # 创建可调整大小的窗口
101141 cv2 .imshow ("Image" , img ) # 显示图像
102142 cv2 .waitKey (1 ) # 等待按键以显示下一帧
103143
0 commit comments