自己在家锻炼时,我们很难知道自己的动作是否标准。本文作者用Python写了一个可以检测俯卧撑动作是否标准的程序,一起来看看他是怎么做的。

译者 | 章雨铭

出品 | CSDN

在新加坡军队中,有一种测试叫做IPPT(个人身体素质测试)。这个测试的困难不在于它对体力的要求有多高,而在于用来计算做俯卧撑和仰卧起坐次数的电子机器。

和大多数人一样,我的俯卧撑动作总是不达标(根据机器的意见)。此外,由于缺乏参照机器标准的练习,许多NSMen(已经完成两年强制性服役的人)在IPPT测试中都难以取得好成绩。

因此,我决定使用mediapipe和OpenCV创建一个程序,跟踪我们的俯卧撑动作,确保我们每一个俯卧撑动作都达标。

由mediapipe姿势模块检测到的肢体关节

import cv2import mediapipe as mpimport mathclass poseDetector() :def __init__(self, mode=False, complexity=1, smooth_landmarks=True,enable_segmentation=False, smooth_segmentation=True,detectionCon=0.5, trackCon=0.5):self.mode = mode self.complexity = complexityself.smooth_landmarks = smooth_landmarksself.enable_segmentation = enable_segmentationself.smooth_segmentation = smooth_segmentationself.detectionCon = detectionConself.trackCon = trackConself.mpDraw = mp.solutions.drawing_utilsself.mpPose = mp.solutions.poseself.pose = self.mpPose.Pose(self.mode, self.complexity, self.smooth_landmarks,self.enable_segmentation, self.smooth_segmentation,self.detectionCon, self.trackCon)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 imgdef findPosition(self, img, draw=True):self.lmList = []if self.results.pose_landmarks:for id, lm in enumerate(self.results.pose_landmarks.landmark):#finding height, width of the image printedh, w, c = img.shape#Determining the pixels of the landmarkscx, 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 findAngle(self, img, p1, p2, p3, draw=True):   #Get the landmarksx1, y1 = self.lmList[p1][1:]x2, y2 = self.lmList[p2][1:]x3, y3 = self.lmList[p3][1:]#Calculate Angleangle = math.degrees(math.atan2(y3-y2, x3-x2) - math.atan2(y1-y2, x1-x2))if angle < 0:angle += 360if angle > 180:angle = 360 - angleelif angle > 180:angle = 360 - angle# print(angle)#Drawif draw:cv2.line(img, (x1, y1), (x2, y2), (255,255,255), 3)cv2.line(img, (x3, y3), (x2, y2), (255,255,255), 3)cv2.circle(img, (x1, y1), 5, (0,0,255), cv2.FILLED)cv2.circle(img, (x1, y1), 15, (0,0,255), 2)cv2.circle(img, (x2, y2), 5, (0,0,255), cv2.FILLED)cv2.circle(img, (x2, y2), 15, (0,0,255), 2)cv2.circle(img, (x3, y3), 5, (0,0,255), cv2.FILLED)cv2.circle(img, (x3, y3), 15, (0,0,255), 2)cv2.putText(img, str(int(angle)), (x2-50, y2+50), cv2.FONT_HERSHEY_PLAIN, 2, (0,0,255), 2)return angledef main():detector = poseDetector()cap = cv2.VideoCapture(0)while cap.isOpened():ret, img = cap.read() #ret is just the return variable, not much in there that we will use. if ret:    img = detector.findPose(img)cv2.imshow('Pose Detection', img)if cv2.waitKey(10) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()if __name__ == "__main__":main()

以上是这个程序的代码。

上面的代码来源于PoseModule.py,有以下几个功能:

  • 激活mediapipe的姿势检测模块。

  • 检测人体。

  • 根据模型找到人体上不同肢体关节的位置。(肢体显示在上面的图片中)。

  • 查找关节之间的角度(取决于你选择的关节)。对于我的俯卧撑程序,我选择找到肘部、肩部和臀部的角度,因为这些对俯卧撑动作的标准至关重要。

接下来是实际的俯卧撑计数的代码。我们使用PoseModule并确定一个俯卧撑合格与否的标准。

import cv2import mediapipe as mpimport numpy as npimport PoseModule as pmcap = cv2.VideoCapture(0)detector = pm.poseDetector()count = 0direction = 0form = 0feedback = "Fix Form"while cap.isOpened():ret, img = cap.read() #640 x 480#Determine dimensions of video - Help with creation of box in Line 43width  = cap.get(3)  # float `width`height = cap.get(4)  # float `height`# print(width, height)img = detector.findPose(img, False)lmList = detector.findPosition(img, False)# print(lmList)if len(lmList) != 0:elbow = detector.findAngle(img, 11, 13, 15)shoulder = detector.findAngle(img, 13, 11, 23)hip = detector.findAngle(img, 11, 23,25)#Percentage of success of pushupper = np.interp(elbow, (90, 160), (0, 100))#Bar to show Pushup progressbar = np.interp(elbow, (90, 160), (380, 50))#Check to ensure right form before starting the programif elbow > 160 and shoulder > 40 and hip > 160:form = 1#Check for full range of motion for the pushupif form == 1:if per == 0:if elbow <= 90 and hip > 160:feedback = "Up"if direction == 0:count += 0.5direction = 1else:feedback = "Fix Form"if per == 100:if elbow > 160 and shoulder > 40 and hip > 160:feedback = "Down"if direction == 1:count += 0.5direction = 0else:feedback = "Fix Form"# form = 0print(count)#Draw Barif form == 1:cv2.rectangle(img, (580, 50), (600, 380), (0, 255, 0), 3)cv2.rectangle(img, (580, int(bar)), (600, 380), (0, 255, 0), cv2.FILLED)cv2.putText(img, f'{int(per)}%', (565, 430), cv2.FONT_HERSHEY_PLAIN, 2,(255, 0, 0), 2)#Pushup countercv2.rectangle(img, (0, 380), (100, 480), (0, 255, 0), cv2.FILLED)cv2.putText(img, str(int(count)), (25, 455), cv2.FONT_HERSHEY_PLAIN, 5,(255, 0, 0), 5)#Feedback cv2.rectangle(img, (500, 0), (640, 40), (255, 255, 255), cv2.FILLED)cv2.putText(img, feedback, (500, 40 ), cv2.FONT_HERSHEY_PLAIN, 2,(0, 255, 0), 2)cv2.imshow('Pushup counter', img)if cv2.waitKey(10) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()

有个需要注意的地方在第17-21行。确定从相机捕捉到的图像的分辨率,并在绘制俯卧撑计数的矩形时调整像素值,等等。(第68-82行)。

我们完成了!一个能确保动作标准的俯卧撑计数软件。没有完全俯下?不算数! 膝盖放在了地上?不算数!

快乐的做俯卧撑吧!

原文链接:

https://aryanvij02.medium.com/push-ups-with-python-mediapipe-open-a544bd9b4351

GitHub 地址:

https://github.com/aryanvij02/PushUpCounter

健身也内卷?这届网友用 Python 掌握了做标准俯卧撑的秘诀相关推荐

  1. 问:新来的同事都自愿996,这是内卷还是努力啊?

    有同学问:新来的同事都自愿996,这是内卷还是努力啊? 回答一 作者:流浪的蛤蟆 来源:知乎 为国家的前途努力,并且放弃自己的利益,叫做奉献. 为自己的前途努力,叫做努力 为其他人或者事物,比如说为公 ...

  2. 女研究生做“思维导图”与男友吵架!堪称吵架届的“内卷之王”....

    来源:募格学术(ID:mugexueshu) 研究生吵起架来的"职业病"有哪些? 近日,湖南长沙一女研究生因为"画思维导图与男友吵架"的视频火了. 网友们纷纷表 ...

  3. 本科毕业出国率下降,考研or保研?条条大路通「内卷」

    点击上方,选择星标或置顶,不定期资源大放送! 阅读大概需要10分钟 Follow小博主,每天更新前沿干货 来源:知乎 转载自:新智元 [导读]近日,华科大计算机本科生毕业报告引发了讨论.一边是只有5% ...

  4. 坐在隔壁的00后同事,让我看到了职场“反内卷”的希望

    作者 | 婉君 来源 | 猎聘(ID:liepinwang) 周末跟朋友吃饭,聊起近况,朋友十分感慨:这批刚进入职场的00后,真是太不一样了. "当年我刚工作的时候,领导让加班,我会回复两个 ...

  5. 内卷加速 | 本科毕业出国率下降,考研or保研?

    点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 仅作学术分享,不代表本公众号立场,侵权联系删除 转载于:新智元 AI博士笔记系列推荐 周志华<机器学习> ...

  6. 跳槽?内卷?2022金三银四下程序员的自我修养

    该不该跳槽? 首先,第一个问题就是我该不该跳槽? 我们跳槽的原因有很多,比如黑心老板.996.拖欠工资.倒挂等等,这些都是我们选择跳槽的理由,一般这些时候我们都会有明确的跳槽意向,这些意向来自于我们对 ...

  7. 论机器学习领域的内卷

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 编辑:陈萍等,来源:机器之心 机器学习内卷了吗? 「没有博士学位,在机器 ...

  8. 2022秋招大战:算法岗挤破头,JAVA开发也被迫内卷

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 来源丨新智元 编辑丨极市平台 导读 秋招的你,选择什么岗位了?鉴于今 ...

  9. 太卷了!人大附中「内卷」到了美国?华裔家长抗议中国学生持F1签证抢占美国IMO名额...

      视学算法报道   来源:weibo 编辑:yaxin [新智元导读]近日,一封华裔家长的抗议公开信引发热议.他/她在信中抗议持F1签证的中国留学生入选美国IMO国家队. 国内的严重「内卷」慢慢卷到 ...

最新文章

  1. Paddle广播 (broadcasting)
  2. AJAX跨域访问解决方案
  3. R语言使用ggplot2可视化堆叠条形图,并在堆叠条形图上显示数据值实战
  4. 设置tomcat管理员的用户名和密码
  5. 探讨增强现实(AR)基于模型的追踪技术
  6. python 类方法装饰器_python类装饰器即__call__方法
  7. 合并排序的非递归实现(自底向上设计)
  8. OpenGL ES之GLKit的使用功能和API说明
  9. 启动MySQL报错:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
  10. 深入解读 MySQL 底层原理,让性能“飞起来”的方法总结
  11. 《深入理解分布式事务》第一章 事务的基本概念
  12. OpenShift 4 之Service Mesh教程(5)- 断路器Circuit Breaker
  13. 怎么管理Websphere应用服务器?
  14. 金融危机下如何获得工作和跳槽机会-网络系统工程师的最终归宿(二)
  15. 《Ray Tracing in One Weekend》——Chapter 6: Antialiasing
  16. 双机热备、双机互备与 双机双工的区别
  17. 证书科普 | 国内主流BIM证书,原来差距这么大
  18. winNTsetup安装器安装系统教程
  19. pip:Could not fetch URL ***: There was a problem confirming the ssl certificate: HTTPSConnectionPool
  20. 题目描述: 某城市有一个火车站,铁轨铺设如图所示。 有n节车厢从A方向驶入车站,按进站顺序编号1~n。 现让这些火车按照某种特定的顺序进入B方向的铁轨并驶出车站。 为了重组车厢,可以借助中转站C。

热门文章

  1. leetcode--回文数--python
  2. 35.2. Subversion 版本控制
  3. Redis集群两种配置方式
  4. 《Android应用开发攻略》——1.3 从命令行创建 “Hello, World”应用程序
  5. 《CCNP TSHOOT 300-135认证考试指南》——2.2节故障检测与排除及网络维护工具箱
  6. PHP引擎php.ini 和fastcti优化
  7. 字符串转换成NSDate类型的 为nil解决方法
  8. grunt-connect-proxy解决开发时跨域问题
  9. Android中设置TextView的颜色setTextColor
  10. Python中的注释(转)