【参考源码】
https://github.com/HRonaldo/Openpose_YOLO
本项目参考上面框架进行全面改进,改进如下:
(1)封装YOLOv5检测类,这样可以加强阅读便利性,模块设计方便嵌入其他框架,后面我会换成yolov8模型进行该项目移植,yolov8移植项目请访问:https://blog.csdn.net/FL1623863129/article/details/142413295
(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
from torch import from_numpy, jit
from Yolov5Manager 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("加载yolov5模型")
detector = Yolov5Manager()
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)
【完整源码下载地址】
https://download.csdn.net/download/FL1623863129/89777684
【延伸扩展】
运行runOpenpose.py
只跑了open pose 可以获得人体的关键点图,用于后续的.jit模型训练 人体的关键点图会保存在data/test中 pose.py中draw方法的最下面可以控制保存关键点图的位置
如果想要检测其他姿势: 1.收集图片,跑runOpenpose.py 文件获得人体的关键点图 2.对人体的关键点图根据自己想要的进行分类放在data/train 和 data/test 3.跑 action_detect/train.py