Python OpenCV实现姿态识别的详细代码

作者:SlowFeather 时间:2023-05-27 23:42:31 

前言

想要使用摄像头实现一个多人姿态识别

环境安装

下载并安装 Anaconda

官网连接 https://anaconda.cloud/installers

Python OpenCV实现姿态识别的详细代码

安装 Jupyter Notebook

检查Jupyter Notebook是否安装

Python OpenCV实现姿态识别的详细代码

Tip:这里涉及到一个切换Jupyter Notebook内核的问题,在我这篇文章中有提到
AnacondaNavigator Jupyter Notebook更换Python内核https://www.jb51.net/article/238496.htm

生成Jupyter Notebook项目目录

打开Anaconda Prompt切换到项目目录

Python OpenCV实现姿态识别的详细代码

输入Jupyter notebook在浏览器中打开 Jupyter Notebook

Python OpenCV实现姿态识别的详细代码

并创建新的记事本

Python OpenCV实现姿态识别的详细代码

下载训练库

图片以及训练库都在下方链接
https://github.com/quanhua92/human-pose-estimation-opencv
将图片和训练好的模型放到项目路径中
graph_opt.pb为训练好的模型

单张图片识别

导入库

import cv2 as cv
import os
import matplotlib.pyplot as plt

加载训练模型

net=cv.dnn.readNetFromTensorflow("graph_opt.pb")

初始化

inWidth=368
inHeight=368
thr=0.2

BODY_PARTS = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
              "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
              "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
              "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }

POSE_PAIRS = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
              ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
              ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
              ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
              ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]

载入图片

img = cv.imread("image.jpg")

显示图片

plt.imshow(img)

Python OpenCV实现姿态识别的详细代码

调整图片颜色

plt.imshow(cv.cvtColor(img,cv.COLOR_BGR2RGB))

Python OpenCV实现姿态识别的详细代码

姿态识别

def pose_estimation(frame):
   frameWidth=frame.shape[1]
   frameHeight=frame.shape[0]
   net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
   out = net.forward()
   out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements

assert(len(BODY_PARTS) == out.shape[1])
   points = []
   for i in range(len(BODY_PARTS)):
       # Slice heatmap of corresponging body's part.
       heatMap = out[0, i, :, :]

# Originally, we try to find all the local maximums. To simplify a sample
       # we just find a global one. However only a single pose at the same time
       # could be detected this way.
       _, conf, _, point = cv.minMaxLoc(heatMap)
       x = (frameWidth * point[0]) / out.shape[3]
       y = (frameHeight * point[1]) / out.shape[2]
       # Add a point if it's confidence is higher than threshold.
       points.append((int(x), int(y)) if conf > thr else None)

for pair in POSE_PAIRS:
       partFrom = pair[0]
       partTo = pair[1]
       assert(partFrom in BODY_PARTS)
       assert(partTo in BODY_PARTS)
       idFrom = BODY_PARTS[partFrom]
       idTo = BODY_PARTS[partTo]
# 绘制线条
       if points[idFrom] and points[idTo]:
           cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
           cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
           cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)

t, _ = net.getPerfProfile()
   freq = cv.getTickFrequency() / 1000
   cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
   return frame
# 处理图片
estimated_image=pose_estimation(img)
# 显示图片
plt.imshow(cv.cvtColor(estimated_image,cv.COLOR_BGR2RGB))

Python OpenCV实现姿态识别的详细代码

视频识别

Tip:与上面图片识别代码是衔接的

Python OpenCV实现姿态识别的详细代码

视频来自互联网,侵删

cap = cv.VideoCapture('testvideo.mp4')
cap.set(3,800)
cap.set(4,800)
if not cap.isOpened():
   cap=cv.VideoCapture(0)
   raise IOError("Cannot open vide")

while cv.waitKey(1) < 0:
   hasFrame,frame=cap.read()
   if not hasFrame:
       cv.waitKey()
       break

frameWidth=frame.shape[1]
   frameHeight=frame.shape[0]
   net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
   out = net.forward()
   out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements
   assert(len(BODY_PARTS) == out.shape[1])
   points = []
   for i in range(len(BODY_PARTS)):
       # Slice heatmap of corresponging body's part.
       heatMap = out[0, i, :, :]
       # Originally, we try to find all the local maximums. To simplify a sample
       # we just find a global one. However only a single pose at the same time
       # could be detected this way.
       _, conf, _, point = cv.minMaxLoc(heatMap)
       x = (frameWidth * point[0]) / out.shape[3]
       y = (frameHeight * point[1]) / out.shape[2]
       # Add a point if it's confidence is higher than threshold.
       points.append((int(x), int(y)) if conf > thr else None)
   for pair in POSE_PAIRS:
       partFrom = pair[0]
       partTo = pair[1]
       assert(partFrom in BODY_PARTS)
       assert(partTo in BODY_PARTS)
       idFrom = BODY_PARTS[partFrom]
       idTo = BODY_PARTS[partTo]
       if points[idFrom] and points[idTo]:
           cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
           cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
           cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)

t, _ = net.getPerfProfile()
   freq = cv.getTickFrequency() / 1000
   cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
   cv.imshow('Video Tutorial',frame)

实时摄像头识别

Tip:与上面图片识别代码是衔接的

Python OpenCV实现姿态识别的详细代码


cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FPS,10)
cap.set(3,800)
cap.set(4,800)
if not cap.isOpened():
   cap=cv.VideoCapture(0)
   raise IOError("Cannot open vide")

while cv.waitKey(1) < 0:
   hasFrame,frame=cap.read()
   if not hasFrame:
       cv.waitKey()
       break

frameWidth=frame.shape[1]
   frameHeight=frame.shape[0]
   net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
   out = net.forward()
   out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements
   assert(len(BODY_PARTS) == out.shape[1])
   points = []
   for i in range(len(BODY_PARTS)):
       # Slice heatmap of corresponging body's part.
       heatMap = out[0, i, :, :]
       # Originally, we try to find all the local maximums. To simplify a sample
       # we just find a global one. However only a single pose at the same time
       # could be detected this way.
       _, conf, _, point = cv.minMaxLoc(heatMap)
       x = (frameWidth * point[0]) / out.shape[3]
       y = (frameHeight * point[1]) / out.shape[2]
       # Add a point if it's confidence is higher than threshold.
       points.append((int(x), int(y)) if conf > thr else None)
   for pair in POSE_PAIRS:
       partFrom = pair[0]
       partTo = pair[1]
       assert(partFrom in BODY_PARTS)
       assert(partTo in BODY_PARTS)
       idFrom = BODY_PARTS[partFrom]
       idTo = BODY_PARTS[partTo]
       if points[idFrom] and points[idTo]:
           cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
           cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
           cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)

t, _ = net.getPerfProfile()
   freq = cv.getTickFrequency() / 1000
   cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
   cv.imshow('Video Tutorial',frame)

参考

DeepLearning_by_PhDScholar
Human Pose Estimation using opencv | python | OpenPose | stepwise implementation for beginners
https://www.youtube.com/watch?v=9jQGsUidKHs

来源:https://blog.csdn.net/a71468293a/article/details/123011891

标签:Python,OpenCV,姿态识别
0
投稿

猜你喜欢

  • Pycharm运行加载文本出现错误的解决方法

    2021-02-01 09:07:18
  • numpy中的transpose函数中具体使用方法

    2023-04-28 23:46:44
  • jquery eval解析JSON中的注意点介绍

    2024-04-19 10:01:07
  • Go语言每天必学之switch语句

    2024-04-26 17:33:59
  • 解决python有时候import不了当前的包问题

    2022-11-28 18:52:21
  • Python中绑定与未绑定的类方法用法分析

    2021-08-06 22:56:54
  • 基于Python实现的恋爱对话小程序详解

    2023-01-14 11:08:38
  • Python离线安装openpyxl模块的步骤

    2021-08-10 16:04:04
  • 学习Python3 Dlib19.7进行人脸面部识别

    2022-03-18 00:01:30
  • pyqt实现.ui文件批量转换为对应.py文件脚本

    2022-01-22 03:32:26
  • python正则表达式 匹配反斜杠的操作方法

    2023-09-04 22:18:52
  • 2020史上最全IDEA插件总结(推荐收藏)

    2023-11-13 07:13:41
  • SQL server 管理事务和数据库介绍

    2024-01-21 18:54:32
  • CSS应用的必要步骤:样式重设

    2008-06-11 13:29:00
  • 关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)

    2022-08-31 22:22:50
  • apache+php+mysql安装配置方法小结

    2023-11-11 23:29:31
  • PyCharm中鼠标悬停在函数上时显示函数和帮助的解决方法

    2023-08-10 19:20:02
  • Python实战之基于OpenCV的美颜挂件制作

    2022-08-30 20:46:30
  • php导出excel格式数据问题

    2023-07-13 22:46:06
  • 用tensorflow搭建CNN的方法

    2021-05-29 08:51:17
  • asp之家 网络编程 m.aspxhome.com