2021-11-26 22:15:46 [显示全部楼层]
5966浏览
查看: 5966|回复: 7

[项目分享] Mind+Python+Mediapipe项目——AI健身之哑铃

[复制链接]

【项目背景】

    平时缺乏锻炼,爱好各种美食,没有管理好热量输入的人,身材也容易随着年龄的增长而发胖,身体健康也会更容易出现问题。

    健身锻炼是预防肥胖的有效手段,还能帮你强化体能,提高自身免疫力。很多人总是等到胖起来才想到要锻炼,但是这个时候,你的体能素质已经直线下降,很多运动是无法驾驭得了的,这个时候你更容易放弃健身,选择中途而废。

    不过,很多人出于各种生活、工作的原因,没有时间锻炼,无法在户外锻炼或者去健身房锻炼,只能利用琐碎时间在家锻炼。

    很多人之所以健身锻炼不久就放弃了,绝大多数就是因为太枯燥了,要是能够采取丰富多彩的训练方式,相信很多人一定可以坚持下来的。

【项目设计】

    本项目以使用哑铃健身为例。通过AI识别人体姿态,灯光响应完成度,增加锻炼趣味性,让健身者乐于锻炼。

【锻炼工具哑铃】

    哑铃是一种用于增强肌肉力量训练的简单器材。很多朋友在健身或者锻炼的时候,都会使用到哑铃。

    如:单臂弯举

    目标肌群:肱二头肌、肱肌和肱桡肌。动作要领:稳坐于长凳上,保持大腿与地面平行,低手抓握哑铃,肘部靠近大腿内侧,并高于膝盖位置,手肘靠在大腿内侧做弯举动作。如果哑铃重量比较重,切记不要用甩的方式,避免手臂受伤。

【Mediapipe+Pinpong库安装】

    MediaPipe 是一款由 Google Research 开发并开源的多媒体机器学习模型应用框架。在谷歌,一系列重要产品,如 YouTube、Google Lens、ARCore、Google Home 以及 Nest,都已深度整合了 MediaPipe。



Mind+Python+Mediapipe项目——AI健身之哑铃图1



安装过程请参考:https://mc.dfrobot.com.cn/thread-311477-1-1.html


【姿态识别测试】

    使用Mediapipe开源代码,对网络跳舞视频进行姿态识别测试。

    测试代码:
  1. <font face="微软雅黑">
  2. import cv2
  3. import mediapipe as mp
  4. mp_drawing = mp.solutions.drawing_utils
  5. mp_pose = mp.solutions.pose
  6. # For webcam input:
  7. cap = cv2.VideoCapture("wudao4.mp4")
  8. with mp_pose.Pose(
  9.     min_detection_confidence=0.5,
  10.     min_tracking_confidence=0.5) as pose:
  11.   while cap.isOpened():
  12.     success, image = cap.read()
  13.     if not success:
  14.       print("Ignoring empty camera frame.")
  15.       # If loading a video, use 'break' instead of 'continue'.
  16.       continue
  17.     # To improve performance, optionally mark the image as not writeable to
  18.     # pass by reference.
  19.     image.flags.writeable = False
  20.     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  21.     results = pose.process(image)
  22.     # Draw the pose annotation on the image.
  23.     image.flags.writeable = True
  24.     image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  25.     mp_drawing.draw_landmarks(
  26.         image,
  27.         results.pose_landmarks,
  28.         mp_pose.POSE_CONNECTIONS)
  29.     # Flip the image horizontally for a selfie-view display.
  30.     cv2.imshow('MediaPipe Pose', cv2.flip(image, 1))
  31.     if cv2.waitKey(5) & 0xFF == 27:
  32.       break
  33. cap.release()
  34. </font>
复制代码
测试视频

【手臂姿态测试】
    在“PoseModule.py”文件中定义类“poseDetector”状态检测,代码如下:
  1. <font face="微软雅黑">
  2. import math
  3. import mediapipe as mp
  4. import cv2
  5. class poseDetector():
  6.     def __init__(self, mode=False, upBody=False, smooth=True,
  7.                  detectionCon=0.5, trackCon=0.5):
  8.         self.mode = mode
  9.         self.upBody = upBody
  10.         self.smooth = smooth
  11.         self.detectionCon = detectionCon
  12.         self.trackCon = trackCon
  13.         self.mpDraw = mp.solutions.drawing_utils
  14.         self.mpPose = mp.solutions.pose
  15.         self.pose = self.mpPose.Pose(self.mode, self.upBody, self.smooth,
  16.                                      self.detectionCon, self.trackCon)
  17.     def findPose(self, img, draw=True):
  18.         imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  19.         self.results = self.pose.process(imgRGB)
  20.         if self.results.pose_landmarks:
  21.             if draw:
  22.                 self.mpDraw.draw_landmarks(img, self.results.pose_landmarks,
  23.                                            self.mpPose.POSE_CONNECTIONS)
  24.         return img
  25.     def findPosition(self, img, draw=True):
  26.         self.lmList = []
  27.         if self.results.pose_landmarks:
  28.             for id, lm in enumerate(self.results.pose_landmarks.landmark):
  29.                 h, w, c = img.shape
  30.                 # print(id, lm)
  31.                 cx, cy = int(lm.x * w), int(lm.y * h)
  32.                 self.lmList.append([id, cx, cy])
  33.                 if draw:
  34.                     cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
  35.         return self.lmList
  36.     def findAngle(self, img, p1, p2, p3, draw=True):
  37.         # Get the landmarks
  38.         x1, y1 = self.lmList[p1][1:]
  39.         x2, y2 = self.lmList[p2][1:]
  40.         x3, y3 = self.lmList[p3][1:]
  41.         # Calculate the Angle
  42.         angle = math.degrees(math.atan2(y3 - y2, x3 - x2) -
  43.                              math.atan2(y1 - y2, x1 - x2))
  44.         if angle < 0:
  45.             angle += 360
  46.         # print(angle)
  47.         # Draw
  48.         if draw:
  49.             cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 3)
  50.             cv2.line(img, (x3, y3), (x2, y2), (255, 255, 255), 3)
  51.             cv2.circle(img, (x1, y1), 10, (0, 0, 255), cv2.FILLED)
  52.             cv2.circle(img, (x1, y1), 15, (0, 0, 255), 2)
  53.             cv2.circle(img, (x2, y2), 10, (0, 0, 255), cv2.FILLED)
  54.             cv2.circle(img, (x2, y2), 15, (0, 0, 255), 2)
  55.             cv2.circle(img, (x3, y3), 10, (0, 0, 255), cv2.FILLED)
  56.             cv2.circle(img, (x3, y3), 15, (0, 0, 255), 2)
  57.             cv2.putText(img, str(int(angle)), (x2 - 50, y2 + 50),
  58.                         cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
  59.         return angle
  60. </font>
复制代码
    测试手臂姿态代码:
  1. <font face="微软雅黑">
  2. import numpy as np
  3. import time
  4. import PoseModule as pm
  5. import cv2
  6. cap = cv2.VideoCapture(0)
  7. detector = pm.poseDetector()
  8. count = 0
  9. dir = 0
  10. pTime = 0
  11. success=True
  12. while success:
  13.     success, img = cap.read()
  14.     img = cv2.resize(img, (640, 480))
  15.     img = detector.findPose(img, False)
  16.     lmList = detector.findPosition(img, False)
  17.    
  18.     if len(lmList) != 0:
  19.         # 右臂
  20.         angle = detector.findAngle(img, 12, 14, 16)
  21.         # 左臂
  22.         #angle = detector.findAngle(img, 11, 13, 15,False)
  23.         per = np.interp(angle, (210, 310), (0, 100))
  24.         light = int(np.interp(angle, (220, 310), (119, 0)))
  25.         # print(angle, per)
  26.         # 计算个数
  27.         
  28.         if per == 100:
  29.             
  30.             if dir == 0:
  31.                 count += 0.5
  32.                 dir = 1
  33.         if per == 0:
  34.             
  35.             if dir == 1:
  36.                 count += 0.5
  37.                 dir = 0
  38.         #print(count)
  39.         cv2.putText(img, str(int(count)), (45, 460), cv2.FONT_HERSHEY_PLAIN, 7,(255, 0, 0), 8)
  40.     cTime = time.time()
  41.     fps = 1 / (cTime - pTime)
  42.     pTime = cTime
  43.     cv2.putText(img, str(int(fps)), (50, 100), cv2.FONT_HERSHEY_PLAIN, 5,(255, 0, 0), 5)
  44.     cv2.imshow("Image", img)
  45.     cv2.waitKey(1)
  46. </font>
复制代码

Mind+Python+Mediapipe项目——AI健身之哑铃图2

【Pinpong库测试】


测试代码:
  1. <font face="微软雅黑">
  2. # -*- coding: utf-8 -*-
  3. #实验效果:控制WS2812单线RGB LED灯
  4. #接线:使用windows或linux电脑连接一块microbit主控板,ws2812灯接到D9口
  5. import time
  6. from pinpong.board import Board,Pin,NeoPixel
  7. NEOPIXEL_PIN = Pin.P0
  8. PIXELS_NUM = 120 #灯数
  9. Board("microbit").begin()  #初始化,选择板型和端口号,不输入端口号则进行自动识别
  10. #Board("microbit","COM36").begin()  #windows下指定端口初始化
  11. #Board("microbit","/dev/ttyACM0").begin()   #linux下指定端口初始化
  12. #Board("microbit","/dev/cu.usbmodem14101").begin()   #mac下指定端口初始化
  13. np = NeoPixel(Pin(NEOPIXEL_PIN), PIXELS_NUM)
  14. while True:
  15.   np[0] = (0, 255 ,0) #设置第一个灯RGB亮度
  16.   np[1] = (255, 0, 0) #设置第二个灯RGB亮度
  17.   np[2] = (0, 0, 255) #设置第三个灯RGB亮度
  18.   np[3] = (255, 0, 255) #设置第四个灯RGB亮度
  19.   time.sleep(1)
  20.   np[1] = (0, 255, 0)
  21.   np[2] = (255, 0, 0)
  22.   np[3] = (255, 255, 0)
  23.   np[0] = (0, 0, 255)
  24.   time.sleep(1)
  25.   np.rainbow(0,PIXELS_NUM,0,0x0000FF)
  26.   time.sleep(1)
  27.   for i in range(PIXELS_NUM):
  28.     np.rotate(1)
  29.     time.sleep(1)
  30.   for i in range(PIXELS_NUM):
  31.     np.shift(1)
  32.     time.sleep(1)
  33.   np.clear()
  34.   time.sleep(1)
  35.   np.range_color(0,PIXELS_NUM,0xFF0000)
  36.   time.sleep(1)
  37.   np.range_color(0,PIXELS_NUM,0x00FF00)
  38.   time.sleep(1)  
  39.   np.range_color(0,PIXELS_NUM,0x0000FF)
  40.   time.sleep(1)
  41.   
  42.   np.clear()
  43.   time.sleep(1)
  44.   
  45. </font>
复制代码


Mind+Python+Mediapipe项目——AI健身之哑铃图3




【完整程序】

  1. <font face="微软雅黑">
  2. import numpy as np
  3. import time
  4. import PoseModule as pm
  5. import cv2
  6. import time
  7. from pinpong.board import Board,Pin,NeoPixel
  8. NEOPIXEL_PIN = Pin.P0
  9. PIXELS_NUM = 120 #灯数
  10. Board("microbit").begin()  #初始化
  11. cap = cv2.VideoCapture(0)
  12. detector = pm.poseDetector()
  13. count = 0
  14. dir = 0
  15. pTime = 0
  16. npX = NeoPixel(Pin(NEOPIXEL_PIN), PIXELS_NUM)
  17. success=True
  18. npX.clear()
  19. time.sleep(1)
  20. while success:
  21.     success, img = cap.read()
  22.     img = cv2.resize(img, (640, 480))
  23.     img = detector.findPose(img, False)
  24.     lmList = detector.findPosition(img, False)
  25.    
  26.     if len(lmList) != 0:
  27.         # 右臂
  28.         angle = detector.findAngle(img, 12, 14, 16)
  29.         # 左臂
  30.         #angle = detector.findAngle(img, 11, 13, 15,False)
  31.         per = np.interp(angle, (210, 310), (0, 100))
  32.         light = int(np.interp(angle, (220, 310), (119, 0)))
  33.         # print(angle, per)
  34.         # 计算个数
  35.         
  36.         if per == 100:
  37.             
  38.             if dir == 0:
  39.                 count += 0.5
  40.                 dir = 1
  41.         if per == 0:
  42.             
  43.             if dir == 1:
  44.                 count += 0.5
  45.                 dir = 0
  46.         #print(count)
  47.         #npX.range_color(light,119,0x000000)
  48.         #npX.range_color(0,light,0xFF0000)
  49.         print(light)
  50.         npX.range_color(light,119,0x000000)
  51.         npX.rainbow(0,light,0,0x0000FF)
  52.         cv2.putText(img, str(int(count)), (45, 460), cv2.FONT_HERSHEY_PLAIN, 7,(255, 0, 0), 8)
  53.     cTime = time.time()
  54.     fps = 1 / (cTime - pTime)
  55.     pTime = cTime
  56.     cv2.putText(img, str(int(fps)), (50, 100), cv2.FONT_HERSHEY_PLAIN, 5,(255, 0, 0), 5)
  57.     cv2.imshow("Image", img)
  58.     cv2.waitKey(1)
  59. </font>
复制代码

【演示视频】

张欣  学徒

发表于 2022-12-10 20:36:41

你好,不太会用这个,出错的比较多,能加个联系方式沟通吗
回复

使用道具 举报

谭周强  中级技师

发表于 2023-1-30 14:54:52

老师 我这边出现报错 可以看一下吗
MonJanuary-202301305403..png
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-1-31 08:12:06

厉害厉害
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-1-31 08:14:14

赞!!!
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-1-31 08:19:02

厉害厉害
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-1-31 08:46:54

这个很不错哦
回复

使用道具 举报

曾剑波  初级技匠 来自手机

发表于 2023-10-11 21:43:01

这个可以来学习一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail