YOLOv8-pose+streamlit 实现人体关键点检测/姿态估计系统

人体关键点检测系统

一、安装与配置

1.1 安装 Streamlit

在命令行直接输入下方指令即可:

python 复制代码
pip install streamlit

1.2 配置文件

在本地的C:\Users\Administrator.streamlit这个位置新建==.streamlit==文件,复制下面的代码段即可

python 复制代码
[server]
port = 8501
enableCORS = false
 
[browser]
serverAddress = "localhost"
gatherUsageStats = false
 
[runner]
magicEnabled = false

1.3 运行Streamlit应用

运行streamlit演示项目:

python 复制代码
streamlit hello

看到如下图web文件代表安装成功。

1.4 找模板

可以上https://streamlit.io/官网,查找一下适合自己的模板,下载到本地后,运行如下指令,进行部署查看

python 复制代码
streamlit run streamli/bg_remove.py

我们使用的模板样式如下图所示:

二、人体关键点检测算法

2.1 关键点序号

以YOLOv8-pose人体姿态估计为例,在COCO数据集上身体的每一个关节具有一个序号,共17个点:

python 复制代码
COCO_keypoint_indexes = {
    0: 'nose',
    1: 'left_eye',
    2: 'right_eye',
    3: 'left_ear',
    4: 'right_ear',
    5: 'left_shoulder',
    6: 'right_shoulder',
    7: 'left_elbow',
    8: 'right_elbow',
    9: 'left_wrist',
    10: 'right_wrist',
    11: 'left_hip',
    12: 'right_hip',
    13: 'left_knee',
    14: 'right_knee',
    15: 'left_ankle',
    16: 'right_ankle'
}

如下图所示:

2.2 YOLOv8-pose图像推理

为了方便,将训练好的模型直接用YOLOv8的内置方法进行推理:model.predict

使用result.plot直接将推理出的目标及关键点画到原图像上去,得到keypoint_image文件。(这里对该方法进行了封装,以便后续调用使用)

python 复制代码
def predctImg(source):
    # Load a model
    model = YOLO(r'weights/yolov8x-pose-p6.pt', task='pose')
    # Perform prediction
    results = model.predict(source=source, save=False, show=False)
    for result in results:
        keypoint_image = result.plot()  # This method will create an image with keypoints drawn
    return keypoint_image

三、将YOLOv8-pose算法内置到streamlit中

3.1 整体结构

核心其实是围绕以下这个引用展开的,涉及了st中几个简单的API,具体内容还是在官网中都有。

python 复制代码
import streamlit as st
python 复制代码
import cv2
import streamlit as st
from PIL import Image
from io import BytesIO


st.set_page_config(layout="wide", page_title="Image Background Remover")

st.write("## :dog: Human Critical Point Detection :grin:")
st.sidebar.write("## Upload and download :gear:")


from ultralytics import YOLO
def predctImg(source):
    # Load a model
    model = YOLO(r'weights/yolov8x-pose-p6.pt', task='pose')

    # Perform prediction
    results = model.predict(source=source, save=False, show=False)
    for result in results:
        keypoint_image = result.plot()  # This method will create an image with keypoints drawn
    return keypoint_image

import numpy as np
def convert_image(img):
    buf = BytesIO()
    # 将Numpy矩阵转换成OpenCV图像
    img = Image.fromarray(np.uint8(img))
    img.save(buf, format="PNG")
    byte_im = buf.getvalue()
    return byte_im


def fix_image(upload):
    image = Image.open(upload)
    col1.write("Original Image :camera:")
    col1.image(image)

    fixed = predctImg(image)

    fixed = cv2.cvtColor(fixed,cv2.COLOR_BGR2RGB)

    col2.write("Fixed Image :wrench:")

    col2.image(fixed)
    st.sidebar.markdown("\n")
    st.sidebar.download_button("Download fixed image", convert_image(fixed), "fixed.png", "image/png")


col1, col2 = st.columns(2)
my_upload = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

if my_upload is not None:
    fix_image(upload=my_upload)
else:
    fix_image("D:\pythonCode\\two_wheel_vehicle_det\\ultralytics-main\\ultralytics-main\images\img.png")

3.2 常见问题

- RGB通道颠倒

看到图像颜色变得很奇怪时(RGB三通道颠倒),可以尝试改变一下三色通道

python 复制代码
fixed = cv2.cvtColor(fixed,cv2.COLOR_BGR2RGB)

- Numpy与OpenCV之间的转换

Numpy转OpenCV图像

python 复制代码
# 将Numpy矩阵转换成OpenCV图像
 img = Image.fromarray(np.uint8(img))

Numpy的优点:

  • 高效:Numpy内部使用C语言编写,执行速度快。
  • 功能丰富:提供了大量的数学函数和数组操作。
  • 易于集成:可以与Python的其他科学计算库无缝集成。

四、效果展示

左侧选择上传图片,上传后自动传输给后端YOLO文件进行目标检测和关键点检测推理,处理好图像之后将结果返回并在右侧进行展示,还可进行保存操作。

推理图像前后端版本:

人体关键点检测,视频推理版本

视频推理:

深度学习人体关键点检测(网页部署)

五、源码

复制代码
相关推荐
PyAIExplorer1 小时前
图像旋转:从原理到 OpenCV 实践
人工智能·opencv·计算机视觉
巴里巴气1 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19891 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金2 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen2 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm
前端付豪2 小时前
20、用 Python + API 打造终端天气预报工具(支持城市查询、天气图标、美化输出🧊
后端·python
前端付豪2 小时前
19、用 Python + OpenAI 构建一个命令行 AI 问答助手
后端·python
amazinging2 小时前
北京-4年功能测试2年空窗-报培训班学测开-第四十三天
python·学习
victory04312 小时前
SpiceMix enables integrative single-cell spatial modeling of cell identity 文章解读
人工智能·深度学习
wgyang20163 小时前
我的第一个LangFlow工作流——复读机
python