先来看一下main.py的代码
- import cv2
- import numpy as np
- import PoseModule as pm
- cap = cv2.VideoCapture('10.mp4')
- detector = pm.poseDetector()
- count = 0
- dir = 0
- while True:
- success, img = cap.read()
- img = cv2.resize(img, (400, 300))
- img = detector.findPose(img, False)
- lmList = detector.findPosition(img, False)
- if len(lmList) != 0:
- angle = detector.findAngle(img, 11, 23, 25)
- per = np.interp(angle, (240, 280), (0, 100))
- bar = np.interp(angle, (250, 280), (280, 100))
- if per == 100:
- if dir == 0:
- count += 0.5
- dir = 1
- if per == 0:
- if dir == 1:
- count += 0.5
- dir = 0
- cv2.rectangle(img, (335, 100), (355, 280), (255, 0, 0), 2)
- cv2.rectangle(img, (335, int(bar)), (355, 280), (255, 0, 0), cv2.FILLED)
- cv2.putText(img, f'{int(per)}%', (300, 75), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
- cv2.putText(img, str(int(count)), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
- cv2.imshow('img', img)
- if cv2.waitKey(1)& 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
不难发现,明明我们的功能需求变复杂了,但主函数却没有变得多复杂,这时可以看到我们的调用部分多了一行import PoseModule as pm —— 我们将一部分功能函数放入了自己创建的类中然后直接调用,使得运行部分的代码不会过于冗长,不过因为类的编写大体基于初级版代码且篇幅较长,这里仅将PoseModule的内容通过截图呈现,不做过多解释。(如果需要发文解释,欢迎评论,热度高的话我有空再出一篇~)
呈现完代码,那么我们开始进行解释:
先是一些调用和视频的调用(为了方便效果的展示,这里从b站上录屏了仰卧起坐过程(侵权删),存为mp4文件后使用)
- import cv2
- import numpy as np
- import PoseModule as pm
- cap = cv2.VideoCapture('10.mp4')
- detector = pm.poseDetector()
- count = 0
- dir = 0
然后是就是主体功能:
- while True:
- success, img = cap.read()
- img = cv2.resize(img, (400, 300))
- img = detector.findPose(img, False)
- lmList = detector.findPosition(img, False)
- if len(lmList) != 0:
- angle = detector.findAngle(img, 11, 23, 25)
- per = np.interp(angle, (240, 280), (0, 100))
- bar = np.interp(angle, (250, 280), (280, 100))
- if per == 100:
- if dir == 0:
- count += 0.5
- dir = 1
- if per == 0:
- if dir == 1:
- count += 0.5
- dir = 0
- cv2.rectangle(img, (335, 100), (355, 280), (255, 0, 0), 2)
- cv2.rectangle(img, (335, int(bar)), (355, 280), (255, 0, 0), cv2.FILLED)
- cv2.putText(img, f'{int(per)}%', (300, 75), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
- cv2.putText(img, str(int(count)), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
- cv2.imshow('img', img)
- if cv2.waitKey(1)& 0xFF == ord('q'):
- break
其中部分代码初级版已经解释过,故此处只解释我们补充的函数:
per = np.interp(angle, (240, 280), (0, 100))
将计算机读取视频画面后监测计算出的角度值的范围转换成0~100
bar = np.interp(angle, (250, 280), (280, 100))
将计算机读取视频画面后监测计算出的角度值的范围转换成100~280
- if per == 100:
- if dir == 0:
- count += 0.5
- dir = 1
- if per == 0:
- if dir == 1:
- count += 0.5
- dir = 0
此处是对角度的值和增减方向进行判断后计数(即起落一次count加1)
- cv2.rectangle(img, (335, 100), (355, 280), (255, 0, 0), 2)
- cv2.rectangle(img, (335, int(bar)), (355, 280), (255, 0, 0), cv2.FILLED)
- cv2.putText(img, f'{int(per)}%', (300, 75), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
- cv2.putText(img, str(int(count)), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
在显示的画面上绘制count所对应的值和仰卧起坐的准确程度(按百分比算)
写到这里,我们的代码就完成了!下面就是效果的展示。
(没做动图 意思意思着看啦思密达 以及码字不易关注收藏一下叭)文章来源:https://www.toymoban.com/news/detail-853654.html
文章来源地址https://www.toymoban.com/news/detail-853654.html
到了这里,关于基于python使用OpenCV和MediaPipe通过人体姿态检测实现对标准的仰卧起坐数量的计量(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!