Skip to content

Commit c86fa32

Browse files
committed
Chapter 2 Pose Estimation
1 parent c4d5409 commit c86fa32

File tree

7 files changed

+68
-17
lines changed

7 files changed

+68
-17
lines changed

Chapter 2 Pose Estimation/PoseModule.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,44 @@
66

77
# 姿势检测类
88
class 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
# 主函数
84115
def 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

22.9 MB
Binary file not shown.

Chapter 2 Pose Estimation/PoseVideos/video download link.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ index,video download link
22
1,https://www.pexels.com/zh-cn/video/4608975/
33
2,https://www.pexels.com/zh-cn/video/8224597/
44
3,https://www.pexels.com/zh-cn/video/7989824/
5-
4,https://www.pexels.com/zh-cn/video/6603299/
5+
4,https://www.pexels.com/zh-cn/video/6603299/
6+
5,https://www.pexels.com/zh-cn/video/5752496/

Chapter 2 Pose Estimation/ProjectExample.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import cv2
22
import time
33
import PoseModule as pm
4-
cap = cv2.VideoCapture('PoseVideos/9.mp4')
4+
cap = cv2.VideoCapture(
5+
"E:\\Advance Computer Vision with Python\\Chapter 2 Pose Estimation\\PoseVideos\\5.mp4"
6+
)
57
pTime = 0
68
detector = pm.poseDetector()
79
while True:
@@ -16,5 +18,6 @@
1618
pTime = cTime
1719
cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3,
1820
(255, 0, 0), 3)
21+
cv2.namedWindow("Image", cv2.WINDOW_NORMAL) # 创建可调整大小的窗口
1922
cv2.imshow("Image", img)
20-
cv2.waitKey(1)
23+
cv2.waitKey(1)

Chapter 2 Pose Estimation/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44

55
`pose = mpPose.Pose()` 创建一个姿势检测对象,用于处理图像并检测人体姿势
66

7-
`results = pose.process(imgRGB)` 处理图像,检测姿势
7+
`results = pose.process(imgRGB)` 处理图像,检测姿势
8+
9+
# 注意事项
10+
11+
用vscode执行代码时,请让终端在`Advance Computer Vision with Python`文件夹下(即vs打开这个整体的文件夹),以及视频路径请使用**绝对路径**,否则,将可能出现一些莫名其妙的报错
12+
13+
比如你让终端在`Chapter 2 Pose Estimation`文件夹下运行,opencv的GUI就要报错,**日怪得很**
3.16 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
E:\\Advance Computer Vision with Python\\Chapter 2 Pose Estimation\\PoseVideos\\3.mp4

0 commit comments

Comments
 (0)