关于python:Python玩人工智能你的仰卧起坐达标了吗

3次阅读

共计 3750 个字符,预计需要花费 10 分钟才能阅读完成。

介绍

Google 出了一个开源的、跨平台的、可定制化的机器学习解决方案工具包,给在线流媒体(当然也能够用于一般的视频、图像等)提供了机器学习解决方案。感兴趣的同学能够关上这个网址理解详情:https://mediapipe.dev/。它提 …

筹备工作

  1. 装置 Python3.8.x
  2. 创立一个 Python 我的项目,倡议应用 virtualenv 创立一个我的项目专有的 Python 环境
  3. 安装包:Opencv-Python、mediapipe、numpy

开始编程

  1. 创立一个人体姿态辨认模块,在这个模块中,咱们应用 mediapipe 模块来实现人体姿态辨认,并获取姿态数据。
import cv2
import mediapipe as mp
import math


class PoseDetector():
    '''人体姿态检测类'''

    def __init__(self,
                 static_image_mode=False,
                 upper_body_only=False,
                 smooth_landmarks=True,
                 min_detection_confidence=0.5,
                 min_tracking_confidence=0.5):
        '''
        初始化
        :param static_image_mode: 是否是动态图片,默认为否
        :param upper_body_only: 是否是上半身,默认为否
        :param smooth_landmarks: 设置为 True 缩小抖动
        :param min_detection_confidence: 人员检测模型的最小置信度值,默认为 0.5
        :param min_tracking_confidence: 姿态可信标记的最小置信度值,默认为 0.5
        '''
        self.static_image_mode = static_image_mode
        self.upper_body_only = upper_body_only
        self.smooth_landmarks = smooth_landmarks
        self.min_detection_confidence = min_detection_confidence
        self.min_tracking_confidence = min_tracking_confidence
        # 创立一个 Pose 对象用于检测人体姿态
        self.pose = mp.solutions.pose.Pose(self.static_image_mode, self.upper_body_only, self.smooth_landmarks,
                                           self.min_detection_confidence, self.min_tracking_confidence)

    def find_pose(self, img, draw=True):
        '''
        检测姿态办法
        :param img: 一帧图像
        :param draw: 是否画出人体姿态节点和连贯图
        :return: 解决过的图像
        '''
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        # pose.process(imgRGB) 会辨认这帧图片中的人体姿态数据,保留到 self.results 中
        self.results = self.pose.process(imgRGB)
        if self.results.pose_landmarks:
            if draw:
                mp.solutions.drawing_utils.draw_landmarks(img, self.results.pose_landmarks,
                                                          mp.solutions.pose.POSE_CONNECTIONS)
        return img

    def find_positions(self, img):
        '''
        获取人体姿态数据
        :param img: 一帧图像
        :param draw: 是否画出人体姿态节点和连贯图
        :return: 人体姿态数据列表
        '''
        # 人体姿态数据列表,每个成员由 3 个数字组成:id, x, y
        # id 代表人体的某个关节点,x 和 y 代表坐标地位数据
        self.lmslist = []
        if self.results.pose_landmarks:
            for id, lm in enumerate(self.results.pose_landmarks.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                self.lmslist.append([id, cx, cy])

        return self.lmslist

    def find_angle(self, img, p1, p2, p3, draw=True):
        '''
        获取人体姿态中 3 个点 p1-p2-p3 的角度
        :param img: 一帧图像
        :param p1: 第 1 个点
        :param p2: 第 2 个点
        :param p3: 第 3 个点
        :param draw: 是否画出 3 个点的连贯图
        :return: 角度
        '''
        x1, y1 = self.lmslist[p1][1], self.lmslist[p1][2]
        x2, y2 = self.lmslist[p2][1], self.lmslist[p2][2]
        x3, y3 = self.lmslist[p3][1], self.lmslist[p3][2]

        # 应用三角函数公式获取 3 个点 p1-p2-p3,以 p2 为角的角度值,0-180 度之间
        angle = int(math.degrees(math.atan2(y1 - y2, x1 - x2) - math.atan2(y3 - y2, x3 - x2)))
        if angle < 0:
            angle = angle + 360
        if angle > 180:
            angle = 360 - angle

        if draw:
            cv2.circle(img, (x1, y1), 20, (0, 255, 255), cv2.FILLED)
            cv2.circle(img, (x2, y2), 30, (255, 0, 255), cv2.FILLED)
            cv2.circle(img, (x3, y3), 20, (0, 255, 255), cv2.FILLED)
            cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255, 3))
            cv2.line(img, (x2, y2), (x3, y3), (255, 255, 255, 3))
            cv2.putText(img, str(angle), (x2 - 50, y2 + 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 2)

        return angle
  1. 编写 situps.py,在这个程序中咱们将应用 opencv 读取视频文件,当然你也能够应用摄像头间接拍摄。调用人体姿态辨认模块进行视频中人体姿态辨认并获取姿态数据,通过人体姿态中 3 个地位点的数据:肩膀(11)、臀部(23)、膝盖(25),计算这 3 个点的角度,判断仰卧起坐是否达标。

# 导入 opencv 工具包
import cv2
# 导入 numpy
import numpy as np
# 导入姿态识别器
from poseutil import PoseDetector

# 关上视频文件
cap = cv2.VideoCapture('videos/situp.mp4')
# 姿态识别器
detector = PoseDetector()

# 方向与个数
dir = 0  # 0 为躺下,1 为坐起
count = 0

while True:
    # 读取摄像头,img 为每帧图片
    success, img = cap.read()
    if success:
        h, w, c = img.shape
        # 辨认姿态
        img = detector.find_pose(img, draw=True)
        # 获取姿态数据
        positions = detector.find_positions(img)

        if positions:
            # 获取仰卧起坐的角度
            angle = detector.find_angle(img, 11, 23, 25)
            # 进度条长度
            bar = np.interp(angle, (50, 130), (w // 2 - 100, w // 2 + 100))
            cv2.rectangle(img, (w // 2 - 100, h - 150), (int(bar), h - 100), (0, 255, 0), cv2.FILLED)
            # 角度小于 55 度认为坐起
            if angle <= 55:
                if dir == 0:
                    count = count + 0.5
                    dir = 1
            # 角度大于 120 度认为躺下
            if angle >= 120:
                if dir == 1:
                    count = count + 0.5
                    dir = 0
            cv2.putText(img, str(int(count)), (w // 2, h // 2), cv2.FONT_HERSHEY_SIMPLEX, 10, (255, 255, 255), 20, cv2.LINE_AA)

        # 关上一个 Image 窗口显示视频图片
        cv2.imshow('Image', img)
        
    else:
        # 视频完结退出
        break

    # 如果按下 q 键,程序退出
    key = cv2.waitKey(1)
    if key == ord('q'):
        break

# 敞开摄像头
cap.release()
# 关闭程序窗口
cv2.destroyAllWindows()

运行测试

总体感觉 mediapipe 识别率还是很不错的,在应用 CPU 的状况下基本上能达到 25 帧 / 秒。咱们能够通过调整仰卧起坐的判断角度来使这个程序更合乎你本人的仰卧起坐检测。

正文完
 0