目录

0、项目介绍

1、效果展示

2、项目搭建

3、项目代码讲解与介绍

Basics.py

PoseModule.py

Example.py

人体姿态图​编辑

4、项目资源

5、项目总结


0、项目介绍

mediapipe中有人体姿态检测的功能,今天我们就将实现最最基础的人体姿态估计项目,它的应用还是有很多的,比如:AI锻炼检测标准、老人跌倒检测等,这些方面其实已经有了很多的参考资料了,当然在我知道的当中用yolo的倒是挺多的。那么今天我们将会通过人物跳舞的视频进行一个姿态的检测。

1、效果展示

可以看见GIF图片中人物跳舞视频检测到的人体姿态骨架。(窗口大小的问题,膝盖下的点没有检测到)

2、项目搭建

如上图,你完全按这个模式照搬过去,完整的视频已经被我拆分好了,大家有兴趣的可以从我的GitHub中获得完整视频与拆分好的视频。

3、项目代码讲解与介绍

Basics.py

import cv2import mediapipe as mpimport timempDraw = mp.solutions.drawing_utilsmpPose = mp.solutions.posepose = mpPose.Pose()cap = cv2.VideoCapture('Pose_videos/02.mp4')pTime = 0while True:    success, img = cap.read()    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)    results = pose.process(imgRGB)    # print(results.pose_landmarks)    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            print(id, lm)            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, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3,                (255, 0, 0), 3)    cv2.imshow("Image", img)    k=cv2.waitKey(1)    if k==27:        break

PoseModule.py

import cv2import mediapipe as mpimport timeclass poseDetector():    def __init__(self, mode=False, upBody=False, smooth=True,                 detectionCon=0.5, trackCon=0.5):        self.mode = mode        self.upBody = upBody        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.upBody, self.smooth)    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):        self.lmList = []        if self.results.pose_landmarks:            for id, lm in enumerate(self.results.pose_landmarks.landmark):                h, w, c = img.shape                # print(id, lm)                cx, cy = int(lm.x * w), int(lm.y * h)                self.lmList.append([id, cx, cy])                if draw:                    cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)        return self.lmListdef main():    cap = cv2.VideoCapture('Pose_videos/02.mp4')    pTime = 0    detector = poseDetector()    while True:        success, img = cap.read()        img = detector.findPose(img)        lmList = detector.findPosition(img, draw=False)        if len(lmList) != 0:            print(lmList[14])            cv2.circle(img, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED)        cTime = time.time()        fps = 1 / (cTime - pTime)        pTime = cTime        cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3,                    (255, 0, 0), 3)        cv2.imshow("Image", img)        k=cv2.waitKey(1)        if k==27:            breakif __name__ == "__main__":    main()

此模块参照与cvzone中的cvzone.PoseModule模块,大家以后也要学习一下这种制作模块的思想,对大家做项目时是很有帮助的。

Example.py

import cv2import timeimport PoseModule as pmcap = cv2.VideoCapture('Pose_videos/02.mp4')pTime = 0detector = pm.poseDetector()while True:    success, img = cap.read()    img = detector.findPose(img)    lmList = detector.findPosition(img, draw=False)    if len(lmList) !=0:        print(lmList[14])        cv2.circle(img, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED)    cTime = time.time()    fps = 1 / (cTime - pTime)    pTime = cTime    cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3,                (255, 0, 0), 3)    cv2.imshow("Image", img)    k = cv2.waitKey(1)    if k == 27:        break

可以看到,在以后做项目时就可以从模块当中copy代码,实现会变得更加的方便。

人体姿态图

此为人体姿态各点的对应图,如果你想要检测某一点的信息,则需要查看此图。

(此图来源于Pose | mediapipe)

4、项目资源

GitHub:18 Human Posture Recognition

5、项目总结

本次项目是按照mediapipe提供的人体姿态估计的功能实现的项目,非常的基础和简单,后面如果我有更好的点子会继续更新这部分内容。