单人的姿态检测|tensorflow singlepose

人工智能32

单人姿态检测-图片

单人的姿态检测|tensorflow singlepose

特此声明非本人图片,如果侵权,联系我,我会删掉。

安装所用的包

!pip install tensorflow==2.4.1 tensorflow-gpu==2.4.1 tensorflow-hub opencv-python matplotlib

导入下面包

tensorflow_hub: 加载模型

CV2: 利用openCV的包,画点,直线,或者其它图片和视频相关的东西

import tensorflow as tf
import tensorflow_hub as hub
import cv2
from matplotlib import pyplot as plt
import numpy as np

加载和执行模型, 返回17个姿态的结果

def movenet(input_image):
    """Runs detection on an input image.

    Args:
      input_image: A [1, height, width, 3] tensor represents the input image
        pixels. Note that the height/width should already be resized and match the
        expected input resolution of the model before passing into this function.

    Returns:
      A [1, 1, 17, 3] float numpy array representing the predicted keypoint
      coordinates and scores.

"""
    # Download the model from TF Hub.

    model = hub.load("https://tfhub.dev/google/movenet/singlepose/lightning/4")
    model = model.signatures['serving_default']

    # SavedModel format expects tensor type of int32.

    input_image = tf.cast(input_image, dtype=tf.int32)
    # Run model inference.

    outputs = model(input_image)
    # Output is a [1, 1, 17, 3] tensor.

    keypoints_with_scores = outputs['output_0'].numpy()

    keypoints_with_scores = keypoints_with_scores.reshape((1, 17, 3))

    return keypoints_with_scores

可以访问Tensorflow的官网,下载singlepose和Multipose相关的模型或查看例子程序

https://tfhub.dev/s?module-type=image-pose-detection

单人的姿态检测|tensorflow singlepose

画17个姿态的点

def draw_keypoints(frame, keypoints, confidence_threshold):
    y, x, c = frame.shape
    shaped = np.squeeze(np.multiply(keypoints, [y, x, 1]))
    print("shaped in draw_keypoints:", shaped)
    for kp in shaped:
        ky, kx, kp_conf = kp
        if kp_conf > confidence_threshold:
            cv2.circle(frame, (int(kx), int(ky)), 6, (0, 255, 0), -1)

画姿态点之间的直线

下面的值告诉我们如何连接人体姿态点。例如,第一个值 (0, 1): 'm' 告诉我们鼻子如何连接到左眼,最后一个值 (14, 16): 'c' 告诉我们右膝如何连接连接到右脚踝。

下面是17个人体姿态点顺序,从0到16。

nose, left eye, right eye, left ear, right ear, left shoulder, right shoulder, left elbow, right elbow, left wrist, right wrist, left hip, right hip, left knee, right knee, left ankle, right ankle

EDGES = {
    (0, 1): 'm',
    (0, 2): 'c',
    (1, 3): 'm',
    (2, 4): 'c',
    (0, 5): 'm',
    (0, 6): 'c',
    (5, 7): 'm',
    (7, 9): 'm',
    (6, 8): 'c',
    (8, 10): 'c',
    (5, 6): 'y',
    (5, 11): 'm',
    (6, 12): 'c',
    (11, 12): 'y',
    (11, 13): 'm',
    (13, 15): 'm',
    (12, 14): 'c',
    (14, 16): 'c'
}

函数draw_connections是17个姿态之间怎么连接

def draw_connections(frame, keypoints, edges, confidence_threshold):
    print('frame', frame)
    y, x, c = frame.shape
    shaped = np.squeeze(np.multiply(keypoints, [y, x, 1]))

    for edge, color in edges.items():
        p1, p2 = edge
        y1, x1, c1 = shaped[p1]
        y2, x2, c2 = shaped[p2]

        if (c1 > confidence_threshold) & (c2 > confidence_threshold):
            cv2.line(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 4)

画人的每一个姿态点和线

def loop_through_people(frame, keypoints_with_scores, edges, confidence_threshold):
    for person in keypoints_with_scores:
        draw_connections(frame, person, edges, confidence_threshold)
        draw_keypoints(frame, person, confidence_threshold)

加载自己的图片

image_path = 'fitness_pic.jpg'

image = tf.io.read_file(image_path)
image = tf.compat.v1.image.decode_jpeg(image)

把它转成(192,192)大小

注意:

  1. 高度和宽度是32个倍数。

  2. 高度和宽度的比例要尽可能接近原图片的比例。

  3. 高度和宽度不能大于256. 例如,应调整 720p 图像(即 720x1280 (HxW))的大小并填充为 160x256 图像。

我们这个例子简单点,大小就是的(192, 192)

# Resize and pad the image to keep the aspect ratio and fit the expected size.

input_size = 192
input_image = tf.expand_dims(image, axis=0)
input_image = tf.image.resize_with_pad(input_image, input_size, input_size)

运行模型推理。得出的keypoints_with_scores是[1, 17, 3].

第一个维度是批次维度,始终等于 1。
第二个维度表示预测的边界框/关键点位置和分数。前 17 * 3 个元素是关键点位置和分数,格式为:[y_0, x_0, s_0, y_1, x_1, s_1, ..., y_16, x_16, s_16],其中 y_i, x_i, s_i 是 yx 坐标 (归一化到图像帧,例如[0.0, 1.0]中的范围)和相应的第i个关节的置信度分数。 17个关键点关节的顺序为:[鼻子、左眼、右眼、左耳、右耳、左肩、右肩、左肘、右肘、左腕、右腕、左髋、右髋、左膝、右膝、左脚踝、右脚踝]。

# Run model inference.

keypoints_with_scores = movenet(input_image)

显示原始图片和标记了每个姿态点的图片

display_image = tf.cast(tf.image.resize_with_pad(image, 1280, 1280), dtype = tf.int32)
display_image = np.array(display_image)
origin_image = np.copy(display_image)

loop_through_people(display_image, keypoints_with_scores, EDGES, 0.1)

plt.subplot(1, 2, 1)
plt.imshow(origin_image)
plt.subplot(1, 2, 2)
plt.imshow(display_image)
plt.show()

单人的姿态检测|tensorflow singlepose

完整代码

import tensorflow as tf
import tensorflow_hub as hub
import cv2
from matplotlib import pyplot as plt
import numpy as np

def movenet(input_image):
    """Runs detection on an input image.

    Args:
      input_image: A [1, height, width, 3] tensor represents the input image
        pixels. Note that the height/width should already be resized and match the
        expected input resolution of the model before passing into this function.

    Returns:
      A [1, 1, 17, 3] float numpy array representing the predicted keypoint
      coordinates and scores.

"""
    # Download the model from TF Hub.

    model = hub.load("https://tfhub.dev/google/movenet/singlepose/lightning/4")
    model = model.signatures['serving_default']

    # SavedModel format expects tensor type of int32.

    input_image = tf.cast(input_image, dtype=tf.int32)
    # Run model inference.

    outputs = model(input_image)
    # Output is a [1, 1, 17, 3] tensor.

    keypoints_with_scores = outputs['output_0'].numpy()

    keypoints_with_scores = keypoints_with_scores.reshape((1, 17, 3))

    return keypoints_with_scores

def draw_keypoints(frame, keypoints, confidence_threshold):
    y, x, c = frame.shape
    shaped = np.squeeze(np.multiply(keypoints, [y, x, 1]))
    print("shaped in draw_keypoints:", shaped)
    for kp in shaped:
        ky, kx, kp_conf = kp
        if kp_conf > confidence_threshold:
            cv2.circle(frame, (int(kx), int(ky)), 6, (0, 255, 0), -1)

EDGES = {
    (0, 1): 'm',
    (0, 2): 'c',
    (1, 3): 'm',
    (2, 4): 'c',
    (0, 5): 'm',
    (0, 6): 'c',
    (5, 7): 'm',
    (7, 9): 'm',
    (6, 8): 'c',
    (8, 10): 'c',
    (5, 6): 'y',
    (5, 11): 'm',
    (6, 12): 'c',
    (11, 12): 'y',
    (11, 13): 'm',
    (13, 15): 'm',
    (12, 14): 'c',
    (14, 16): 'c'
}

def draw_connections(frame, keypoints, edges, confidence_threshold):
    print('frame', frame)
    y, x, c = frame.shape
    shaped = np.squeeze(np.multiply(keypoints, [y, x, 1]))

    for edge, color in edges.items():
        p1, p2 = edge
        y1, x1, c1 = shaped[p1]
        y2, x2, c2 = shaped[p2]

        if (c1 > confidence_threshold) & (c2 > confidence_threshold):
            cv2.line(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 4)

def loop_through_people(frame, keypoints_with_scores, edges, confidence_threshold):
    for person in keypoints_with_scores:
        draw_connections(frame, person, edges, confidence_threshold)
        draw_keypoints(frame, person, confidence_threshold)

image_path = 'C:/Users/Harry/Desktop/fitness.jpeg'

image = tf.io.read_file(image_path)
# image = tf.compat.v1.image.decode_image(image)
image = tf.compat.v1.image.decode_jpeg(image)

# Resize and pad the image to keep the aspect ratio and fit the expected size.

input_size = 192
input_image = tf.expand_dims(image, axis=0)
input_image = tf.image.resize_with_pad(input_image, input_size, input_size)

# Run model inference.

keypoints_with_scores = movenet(input_image)

display_image = tf.cast(tf.image.resize_with_pad(image, 1280, 1280), dtype = tf.int32)
display_image = np.array(display_image)
origin_image = np.copy(display_image)

loop_through_people(display_image, keypoints_with_scores, EDGES, 0.1)

plt.subplot(1, 2, 1)
plt.imshow(origin_image)
plt.subplot(1, 2, 2)
plt.imshow(display_image)
plt.show()

参考资料

https://tfhub.dev/google/movenet/singlepose/lightning/4

Original: https://blog.csdn.net/keeppractice/article/details/125774021
Author: 茫茫人海一粒沙
Title: 单人的姿态检测|tensorflow singlepose



相关阅读

Title: 安装Anaconda/Python3.9/Tensorflow

Title: 安装Anaconda/Python3.9/Tensorflow

安装Anaconda/Python3.9/Tensorflow

· 安装Anaconda

官网安装,开梯子
单人的姿态检测|tensorflow singlepose
Download即可。打开下载好的安装包,按照提示,一路【Next】
单人的姿态检测|tensorflow singlepose
选择安装路径
单人的姿态检测|tensorflow singlepose
官方建议不要自动配置环境变量,无论是自动还是手动配置。一般情况下,默认选中以下选项(我刚安装在这里,所以无法选中。)

[En]

It is not officially recommended to configure environment variables automatically, either automatically or manually. Generally, the following option is checked by default (I have just installed it here, so I can't check it. )

点击【Install】,等待安装完成。继续按提示操作至安装界面结束。

; · 配置Anaconda环境变量

添加如下Path环境变量
单人的姿态检测|tensorflow singlepose
cmd查看Anaconda版本
单人的姿态检测|tensorflow singlepose
证明Anaconda已经装好。

· 安装Tensorflow

开始菜单打开Anaconda Prompt
单人的姿态检测|tensorflow singlepose
配置清华镜像源,从这里开始要关掉代理了。

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

Anaconda创建Python环境,可在官网查看Tensorflow支持的Python版本:
单人的姿态检测|tensorflow singlepose
我的Anaconda给我装了Python3.9,于是

conda create -n tensorflow python=3.9

询问[Y/N]时选择Y。完成时给出如下提示
单人的姿态检测|tensorflow singlepose
查看目前有哪些环境,通过

conda env list

conda info --env

可以看到tensorflow环境已经创建。星号为当前所在环境(基础环境base)。通过 activate tensorflow进入tensorflow环境:
单人的姿态检测|tensorflow singlepose
开始实际安装Tensorflow。查看当前可使用的Tensorflow版本,通过

conda search --full --name tensorflow

单人的姿态检测|tensorflow singlepose
从1.1.0到2.6.0都有。可指定版本安装,这里安装默认版本

pip install --upgrade --ignore-installed tensorflow

无报错结束应该是装好了。打开Python环境,导入tensorflow包进行测试
单人的姿态检测|tensorflow singlepose
确定tensorflow已经装好(这里应该是Cuda版本低了,以后再调)。 tf.__version__查看版本为2.7。
最后如果想退出tensorflow环境

conda deactivate

同时,conda控制台是默认打开base环境的,如果想管理这一设置

conda config --set auto_activate_base false / true

参考博客如下

https://blog.csdn.net/zaxcac/article/details/122422684
https://blog.csdn.net/weixin_42412254/article/details/107569830
https://blog.csdn.net/woniuyc/article/details/121984874
https://blog.csdn.net/weixin_44717083/article/details/121555146
https://blog.csdn.net/java_pythons/article/details/114875018
https://blog.csdn.net/qq_38463737/article/details/109492394
https://blog.csdn.net/qq_37924224/article/details/117712061

Original: https://blog.csdn.net/qq_45755158/article/details/122644140
Author: Miska_Muska
Title: 安装Anaconda/Python3.9/Tensorflow

Original: https://blog.csdn.net/qq_45755158/article/details/122644140
Author: Miska_Muska
Title: 安装Anaconda/Python3.9/Tensorflow

相关文章
数据处理中的归一化与反归一化 人工智能

数据处理中的归一化与反归一化

一、定义 数据归一化(标准化)是数据预处理的一项基础工作, 不同评价指标往往具有不同的量纲和量纲单位,为避免影响数据分析结果、消除指标之间的量纲影响,须对数据进行标准化处理。 数据的归一化(norma...
基于片内Flash的提示音播放程序 人工智能

基于片内Flash的提示音播放程序

目录 一、使用DAC输出周期2khz的正弦波 * 1、获取正弦信号 2、获取数据 3、得到对应的C语言文件 二、使用DAC输出数字音频歌曲数据转换为模拟音频波形输出 三、总结 四、参考链接 一、使用D...
项目实战——文档扫描OCR识别 人工智能

项目实战——文档扫描OCR识别

扫描全能王的实现,maybe 目录 一、文档扫描 1、引入所需要的库 2、图像的读取与预处理 读取图像 图像reszie, 图像灰度化、滤波、边缘检测。 3、轮廓检测 4、透视与二值变换 二、文字识别...