基于yolov8和openpose人体骨骼关键点实现的摔倒姿态识别检测系统实现

news2024/9/21 11:33:09

【参考源码】

GitHub - HRonaldo/Openpose_YOLO

本项目参考上面框架进行全面改进,改进如下:

(1)将检测框架换成当前最流行框架yolov8,并封装成类实现模块化设计。关于yolov5优化项目可以访问:https://blog.csdn.net/FL1623863129/article/details/142411883

(2)调整部分文件夹结构,比如模型归为一类文件夹,这样方便查看模型

(3)检测流程简化,使得人体骨骼关键点实现的摔倒姿态识别代码十分简单,实际只要40多行代码就实现整个检测流程

【效果展示】

【特别注意】

本项目由于使用了3个模型,所以实时性能较差,建议采用GPU推理加快推理速度,同时尽量不要使用cv2.imshow去实时显示窗口,而是采用写出视频结果方式

【测试环境】

代码提供有requirments.txt,但是环境其实比较宽松,比如我的环境是

torch==1.9.0

opencv-python==4.8.0.76

【实现代码】

【原来代码】

detect.py

import argparse
import time
from pathlib import Path
from torch import from_numpy, jit
 
import cv2
import torch
import torch.backends.cudnn as cudnn
from numpy import random
import os
import datetime
from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, non_max_suppression, apply_classifier, scale_coords, xyxy2xywh, \
    strip_optimizer, set_logging, increment_path
from utils.plots import plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized
 
import runOpenpose
 
import YOLO
 
 
def detect(save_img=False):
    global ip
    source, weights, view_img, save_txt, imgsz = opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size
    webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith(
        ('rtsp://', 'rtmp://', 'http://'))
 
    # Directories
    save_dir = Path(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))  # increment run
    (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir
 
    # 加载摔倒检测的模型
    print("加载摔倒检测的模型开始")
    net = jit.load(r'action_detect/checkPoint/openpose.jit')
    action_net = jit.load(r'action_detect/checkPoint/action.jit')
    print("加载摔倒检测的模型结束")
    # Initialize
    set_logging()
    # 获取设备
    device = select_device(opt.device)
    # 如果设备为gpu,使用Float16
    half = device.type != 'cpu'  # half precision only supported on CUDA
    # Load model
    # 加载Float32模型,确保用户设定的输入图片分辨率能整除32(如果不能则调整为能整除并删除)
    #model = attempt_load(weights, map_location=device)  # load FP32 model
 
    model=YOLO.Predictor(weights,device,opt.classes,opt.conf_thres)
    imgsz = check_img_size(imgsz, s=32)  # !!!check img_size
    # 设置Float16
    if half:
        model.model.half()  # to FP16
 
    # Set Dataloader
    # 通过不同的输入源来设置不同的数据加载方式
    vid_path, vid_writer = None, None
    if webcam:
        view_img = True
        cudnn.benchmark = True  # set True to speed up constant image size inference
        dataset = LoadStreams(source, img_size=imgsz)
    else:
        save_img = True
        # 如果检测视频的时候想显示出来,可以再这里加一行 view_img = True
        view_img = True
        dataset = LoadImages(source, img_size=imgsz)
 
    # Get names and colors
    # 获取类别的名字
    names = model.module.names if hasattr(model, 'module') else model.model.names
    # 设置画框的颜色
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
 
    # Run inference
    t0 = time.time()
    # 进行一次向前推理,测试程序是否正常
    img = torch.zeros((1, 3, imgsz, imgsz), device=device)  # init img
    _ = model.model(img.half() if half else img) if device.type != 'cpu' else None  # run once
    """
        path 图片/视频路径
        img 进行resize+pad之后的图片
        img0 原size图片
        cap 当读取图片时为None,读取视频时为视频源
        """
    for path, img, im0s, vid_cap in dataset:
        img = torch.from_numpy(img).to(device)
        # 图片也设置为Float16
        img = img.half() if half else img.float()  # uint8 to fp16/32
        img /= 255.0  # 0 - 255 to 0.0 - 1.0
        # 没有batch_size 的话则在最前面增加一个轴
        if img.ndimension() == 3:
            img = img.unsqueeze(0)
 
        # Inference
        t1 = time_synchronized()
 
        pred=model.predict(source=img)
        t2 = time_synchronized()
 
        # Process detections
        # 对每一张图片作处理
        for i, det in enumerate(pred):  # detections per image
            # 如果输入源是webcam 则batch_size 不为1,取出dataset中的一张图片
            if webcam:  # batch_size >= 1
                p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
            else:
                p, s, im0, frame = path, '', im0s, getattr(dataset, 'frame', 0)
            # print(det)
 
            boxList = []  # 框的一个list 给openpose 使用
            p = Path(p)  # to Path
            # 设置保存图片/视频的路径
            save_path = str(save_dir / p.name)  # img.jpg
            # 设置保存框最薄txt文件的路径
            txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}')  # img.txt
            # 设置打印信息(图片长宽)
            s += '%gx%g ' % img.shape[2:]  # print string
            gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
            if len(det):
                # Rescale boxes from img_size to im0 size
                # 调整预测框的坐标:基于resize+pad的图片的坐标 -->基于原size图片的坐标
                # 此时坐标格式为xyxy
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
 
                # Print results
                # 打印检测到的类别数量
                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    s += f'{n} {names[int(c)]}s, '  # add to string
 
                # Write results
                # 保存预测结果
                for *xyxy, conf, cls in reversed(det):
                    if save_txt:  # Write to file
                        # 将xyxy(左上角+右下角)格式转为xywh(中心点+宽长)格式,并除上w,h做归一化,转化为列表再保存
                        xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywh
                        line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh)  # label format
                        with open(txt_path + '.txt', 'a') as f:
                            f.write(('%g ' * len(line)).rstrip() % line + '\n')
                    # 在原图上画框
                    if save_img or view_img:  # Add bbox to image
                        label = f'{names[int(cls)]} {conf:.2f}'
                        plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
                        boxList.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])
 
                runOpenpose.run_demo(net, action_net, [im0], 256, False, boxList)  # 人体姿态检测 将图片和yolov5检测人体的框也传给openpose
            # Print time (inference + NMS)
            # 打印向前传播+nms时间
            print(f'{s}Done. ({t2 - t1:.3f}s)')
 
            if save_img:
                if dataset.mode == 'image':
                    imageName = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) + ".jpg"
 
                    cv2.imwrite(save_path+imageName, im0)
                else:  # 'video'
                    if vid_path != save_path:  # new video
                        vid_path = save_path
                        if isinstance(vid_writer, cv2.VideoWriter):
                            vid_writer.release()  # release previous video writer
                        fourcc = 'mp4v'  # output video codec
                        fps = vid_cap.get(cv2.CAP_PROP_FPS)
                        w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                        h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                        vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (w, h))
                    vid_writer.write(im0)
                print('保存图片')
 
    # 打印总时间
    print(f'Done. ({time.time() - t0:.3f}s)')
    cv2.destroyAllWindows()
 
 
 
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # 选用训练的权重,可用根目录下的yolov5s.pt,也可用runs/train/exp/weights/best.pt
    parser.add_argument('--weights', type=str, default='models/hub/yolov8s.pt', help='model.pt path(s)')
    # 检测数据,可以是图片/视频路径,也可以是'0'(电脑自带摄像头),也可以是rtsp等视频流
    parser.add_argument('--source', type=str, default='0',
                        help='source')  # file/folder, 0 for webcam
    # 网络输入图片大小
    parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
    # 置信度阈值,检测到的对象属于特定类(狗,猫,香蕉,汽车等)的概率
    parser.add_argument('--conf-thres', type=float, default=0.5, help='object confidence threshold')
    # 做nms的iou阈值
    parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
    # 检测的设备,cpu;0(表示一个gpu设备cuda:0);0,1,2,3(多个gpu设备)。值为空时,训练时默认使用计算机自带的显卡或CPU
    parser.add_argument('--device', default='gpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    # 是否展示检测之后的图片/视频,默认False
    parser.add_argument('--view-img', action='store_true', help='display results')
    # 是否将检测的框坐标以txt文件形式保存,默认False
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    # 是否将检测的labels以txt文件形式保存,默认False
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    # 设置只保留某一部分类别,如0或者0 2 3
    parser.add_argument('--classes', nargs='+', default=[0,67], type=int,
                        help='filter by class: --class 0, or --class 0 2 3')
    # 进行nms是否也去除不同类别之间的框,默认False
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    # 推理的时候进行多尺度,翻转等操作(TTA)推理
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
    parser.add_argument('--update', action='store_true', help='update all models')
    # 检测结果所存放的路径,默认为runs/detect
    parser.add_argument('--project', default='runs/detect', help='save results to project/name')
    # 检测结果所在文件夹的名称,默认为exp
    parser.add_argument('--name', default='exp', help='save results to project/name')
    # 若现有的project/name存在,则不进行递增
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    opt = parser.parse_args()
    print(opt)
    with torch.no_grad():
        detect()

我的代码main_video.py

# -*- coding: utf-8 -*-
# Copyright (C) 2024/9/21 FIRC. All Rights Reserved
# @Time    : 2024/9/21 上午8:29
# @Author  : FIRC
# @File    : main.py
# @Software: PyCharm-2024.2
# @ Function Description:
import torch
import numpy as np
from torch import from_numpy, jit
from Yolov8Detector import *
import runOpenpose
import cv2


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 加载摔倒检测的模型
print("加载摔倒检测的模型开始")
net = jit.load(r'weights/openpose.jit', map_location=device)
action_net = jit.load(r'weights/action.jit', map_location=device)
print("加载yolov8模型")
detector = Yolov8Detector()
print('所有模型加载完成!')
video_file = "video/tt.mp4"
save_file = 'result.mp4'
cap = cv2.VideoCapture(video_file)

# 获取视频帧速率 FPS
frame_fps = int(cap.get(cv2.CAP_PROP_FPS))
# 获取视频帧宽度和高度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("video fps={},width={},height={}".format(frame_fps, frame_width, frame_height))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(save_file, fourcc, frame_fps, (frame_width, frame_height))
count = 0

while True:
    ret, frame = cap.read()
    if not ret:
        print('read over!')
        break
    count += 1
    result_lists = detector.inference_image(frame)
    for box in result_lists:
        x = box[4] - box[2]
        y = box[3] - box[1]
        if x / y >= 0.8:  # 比例>0.8 可能会是摔倒
            print('进行人体姿态检测')
            box_list = [res[2:6] for res in result_lists]
            runOpenpose.run_demo(net, action_net, [frame], 256, False, box_list)  # 人体姿态检测 将图片和yolov5检测人体的框也传给openpose
            break
    #frame = detector.draw_image(frame,result_list=result_lists)
    #out.write(frame)
    print('detected frame {}'.format(count))
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
out.release()
cap.release()
cv2.destroyAllWindows()

如果您想把人画上去,请打开注释   

第52行 #frame = detector.draw_image(frame,result_list=result_lists)

如果您保存视频,请打开注释

第53行    #out.write(frame)

【完整源码下载地址】

【延伸扩展】

运行runOpenpose.py
只跑了open pose 可以获得人体的关键点图,用于后续的.jit模型训练 人体的关键点图会保存在data/test中 pose.py中draw方法的最下面可以控制保存关键点图的位置

如果想要检测其他姿势: 1.收集图片,跑runOpenpose.py 文件获得人体的关键点图 2.对人体的关键点图根据自己想要的进行分类放在data/train 和 data/test 3.跑 action_detect/train.py

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2152212.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【华为杯研赛赛题】2024年中国研究生数学建模竞赛赛题已出

2024年中国研究生数学建模竞赛所有赛题已出! A题 B题 C题 D题 E题 F题

【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL77

编写乘法器求解算法表达式 描述 编写一个4bit乘法器模块,并例化该乘法器求解c12*a5*b,其中输入信号a,b为4bit无符号数,c为输出。注意请不要直接使用*符号实现乘法功能。 模块的信号接口图如下: 要求使用Verilog HDL语言实现以上…

使用 Elasticsearch Reindex API 迁移数据

使用 Elasticsearch Reindex API 迁移数据 在 Elasticsearch 中,随着需求的变化,可能需要对索引进行重建或更新。这通常涉及创建新索引、迁移数据等步骤。本文介绍如何使用 Reindex API 将旧索引中的数据迁移到新索引中 一、步骤概述 创建新索引&#…

OpenCV_距离变换的图像分割和Watershed算法详解

在学习watershed算法的时候,书写代码总会出现一些错误: 上述代码运行报错,显示OpenCV(4.10.0) Error: Assertion failed (src.type() CV_8UC3 && dst.type() CV_32SC1) in cv::watershed 查找资料:目前已解决 这个错…

idea 编辑器常用插件集合

SequenceDiagram 用于生成时序图的插件,支持一键生成功能。 使用:选择某个具体的方法,点击右键菜单,选择“Sequence Diagram” 便可生成相应的时序图 例子: 效果: Code Iris Code Iris可以根据代码自动…

c++day3 手动封装一个顺序表(SeqList),分文件编译实现

要求: 有私有成员:顺序表数组的起始地址 ptr、 顺序表的总长度:size、顺序表的实际长度:len 成员函数:初始化 init(int n) 判空:empty 判满:full 尾插:push_back 插入:insert&…

优数:助力更高效的边缘计算

在数字化时代的浪潮中,数据已成为企业最宝贵的资产之一。随着物联网(IoT)设备的激增和5G技术的兴起,我们正迅速步入一个新时代,在这个时代中,数据不仅在量上爆炸性增长,更在速度和实时性上提出了…

Hadoop里面MapReduce的序列化与Java序列化比较

什么是序列化? jvm中的一个对象,不是类,假如你想把一个对象,保存到磁盘上,必须序列化,你把文件中的对象进行恢复,是不是的反序列化。 假如你想把对象发送给另一个服务器,需要通过网…

线性dp 总结详解

就是感觉之前 dp 的 blog 太乱了整理一下。 LIS(最长上升子序列) 例题 给定一个整数序列&#xff0c;找到它的所有严格递增子序列中最长的序列&#xff0c;输出其长度。 思路 拿到题目&#xff0c;大家第一时间想到的应该是的暴力(dp)做法&#xff1a; #include <bits/s…

基于Windows系统以tomcat为案例,讲解如何新增自启动服务,定时重启服务。

文章目录 引言I 设置服务自启动的常规操作II 安装多个tomcat服务,并设置自启动。III 定时重启服务引言 为了同一个版本安装多个tomcat服务,并设置自启动。使用Windows的任务计划程序来创建一个定时任务,用于重启Tomcat服务。I 设置服务自启动的常规操作 运行窗口输入control…

2024双11有哪些值得入手的好物?2024年双十一好物推荐

随着2024年双十一购物狂欢节的临近&#xff0c;消费者们正摩拳擦掌&#xff0c;准备迎接这场年度最大的网购盛会。面对琳琅满目的促销信息和令人眼花缭乱的商品&#xff0c;如何在海量商品中精准锁定那些真正值得购买的好物&#xff0c;成为每位精明买家的首要任务。本文旨在为…

牛啊,GitHub 代理加速图文教程

大家好&#xff0c;众所周知&#xff0c;GitHub 在国内访问速度堪忧&#xff0c;经常出现访问不了的情况&#xff0c;如果我们去 clone 代码&#xff0c;网速非常差。今天教大家如何给 GitHub 进行加速。 要用到我开发的开源项目 Cloudflare Workers Proxy&#xff0c;它是一个…

视频压缩篇:适用于 Windows 的 10 款最佳视频压缩器

视频压缩器现在对许多想要减小视频大小的视频编辑者来说非常有名。但是&#xff0c;并非所有可以在网上找到的视频压缩器都能产生最佳输出。因此&#xff0c;我们搜索了可以无损压缩视频的最出色的视频压缩器应用程序。本文列出了您可以在离线、在线和手机上使用的十大最佳视频…

2024华为杯研赛D题保姆级教程思路分析+教程

2024年中国研究生数学建模竞赛D题保姆级教程思路分析 D题&#xff1a;大数据驱动的地理综合问题&#xff08;数学分析&#xff0c;统计学&#xff09; 关键词&#xff1a;地理、气候、统计&#xff08;细致到此题&#xff1a;统计指标、统计模型、统计结果解释&#xff09; …

无线领夹麦克风哪个降噪好?一文搞懂麦克风什么牌子的音质效果好

对于视频拍摄、直播来说&#xff0c;一款好的拾音麦克风是不可或缺的。作为一位数码博主&#xff0c;也是会经常拍摄视频讲解&#xff0c;早期没有使用麦克风时&#xff0c;声音不够清晰&#xff0c;而且周围环境音也会同时被收录&#xff0c;导致整个音频的音质效果极差&#…

【多线程】CAS的原理及应用,看这篇文章就够啦

&#x1f490;个人主页&#xff1a;初晴~ &#x1f4da;相关专栏&#xff1a;多线程 / javaEE初阶 一、CAS概述 CAS&#xff08;Compare and Swap&#xff09;&#xff0c;中文译为 “比较并交换” &#xff0c;是一种无锁算法中常用的原子操作。CAS通常用于实现线程之间的同…

力扣之1459.矩形面积

1. 1459.矩形面积 1.1 题干 表: Points ---------------------- | Column Name | Type | ---------------------- | id | int | | x_value | int | | y_value | int | ---------------------- id 是该表中具有唯一值的列。 每个点都用二维坐标 (x_value, y_value) 表示。 编…

【力扣每日一题——2374. 边积分最高的节点】python

2374. 边积分最高的节点 给你一个有向图&#xff0c;图中有 n 个节点&#xff0c;节点编号从 0 到 n - 1 &#xff0c;其中每个节点都 恰有一条 出边。 图由一个下标从 0 开始、长度为 n 的整数数组 edges 表示&#xff0c;其中 edges[i] 表示存在一条从节点 i 到节点 edges[…

大模型训练实战经验总结

在当今AI技术飞速发展的背景下&#xff0c;定制化大模型的自主训练已成为满足特定行业需求、保障数据安全、提升模型应用效能的关键途径。本文将深度剖析这一过程的核心价值与实践智慧&#xff0c;从数据隐私保护、模型透明度增强&#xff0c;到数据预处理的精细操作&#xff0…

记录一次fs配置导致串线的问题

概述 freeswitch是一款简单好用的VOIP开源软交换平台。 fs在实际的使用过程中也会经常碰到莫名其妙的问题&#xff0c;大部分都是配置问题。 环境 CentOS 7.9 freeswitch 1.10.7 docker 26.1.1 问题描述 组网方案如下。其中的fs-reg是注册服务器&#xff0c;fs1和fs2是…