当前位置:   article > 正文

python摔倒检测,跌倒检测openpose站立行为检测_python pose库

python pose库

python摔倒检测,跌倒检测openpose站立行为检测

  1. import cv2
  2. import numpy as np
  3. from torch import from_numpy, jit
  4. from modules.keypoints import extract_keypoints, group_keypoints
  5. from modules.pose import Pose
  6. from action_detect.detect import action_detect
  7. import os
  8. from math import ceil, floor
  9. os.environ["PYTORCH_JIT"] = "0"
  10. class ImageReader(object):
  11. def __init__(self, file_names):
  12. self.file_names = file_names
  13. self.max_idx = len(file_names)
  14. def __iter__(self):
  15. self.idx = 0
  16. return self
  17. def __next__(self):
  18. if self.idx == self.max_idx:
  19. raise StopIteration
  20. img = cv2.imread(self.file_names[self.idx], cv2.IMREAD_COLOR)
  21. if img.size == 0:
  22. raise IOError('Image {} cannot be read'.format(self.file_names[self.idx]))
  23. self.idx = self.idx + 1
  24. return img
  25. class VideoReader(object):
  26. def __init__(self, file_name,code_name):
  27. self.file_name = file_name
  28. self.code_name = str(code_name)
  29. try: # OpenCV needs int to read from webcam
  30. self.file_name = int(file_name)
  31. except ValueError:
  32. pass
  33. def __iter__(self):
  34. self.cap = cv2.VideoCapture(self.file_name)#读入已有视频检测
  35. #self.cap = cv2.VideoCapture(0)#调用笔记本内置摄像头检测
  36. if not self.cap.isOpened():
  37. raise IOError('Video {} cannot be opened'.format(self.file_name))
  38. return self
  39. def __next__(self):
  40. was_read, img = self.cap.read()
  41. if not was_read:
  42. raise StopIteration
  43. # print(self.cap.get(7),self.cap.get(5))
  44. cv2.putText(img,self.code_name, (5,35),
  45. cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
  46. return img
  47. def normalize(img, img_mean, img_scale):
  48. img = np.array(img, dtype=np.float32)
  49. img = (img - img_mean) * img_scale
  50. return img
  51. def pad_width(img, stride, pad_value, min_dims):
  52. h, w, _ = img.shape
  53. h = min(min_dims[0], h)
  54. min_dims[0] = ceil(min_dims[0] / float(stride)) * stride
  55. min_dims[1] = max(min_dims[1], w)
  56. min_dims[1] = ceil(min_dims[1] / float(stride)) * stride
  57. pad = []
  58. pad.append(int(floor((min_dims[0] - h) / 2.0)))
  59. pad.append(int(floor((min_dims[1] - w) / 2.0)))
  60. pad.append(int(min_dims[0] - h - pad[0]))
  61. pad.append(int(min_dims[1] - w - pad[1]))
  62. padded_img = cv2.copyMakeBorder(img, pad[0], pad[2], pad[1], pad[3],
  63. cv2.BORDER_CONSTANT, value=pad_value)
  64. return padded_img, pad
  65. def infer_fast(net, img, net_input_height_size, stride, upsample_ratio, cpu,
  66. pad_value=(0, 0, 0), img_mean=(128, 128, 128), img_scale=1/256):
  67. height, width, _ = img.shape
  68. scale = net_input_height_size / height
  69. scaled_img = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
  70. scaled_img = normalize(scaled_img, img_mean, img_scale)
  71. min_dims = [net_input_height_size, max(scaled_img.shape[1], net_input_height_size)]
  72. padded_img, pad = pad_width(scaled_img, stride, pad_value, min_dims)
  73. tensor_img = from_numpy(padded_img).permute(2, 0, 1).unsqueeze(0).float()
  74. if not cpu:
  75. #tensor_img = tensor_img.cuda()
  76. pass
  77. stages_output = net(tensor_img)
  78. # print(stages_output)
  79. stage2_heatmaps = stages_output[-2]
  80. heatmaps = np.transpose(stage2_heatmaps.squeeze().cpu().data.numpy(), (1, 2, 0))
  81. heatmaps = cv2.resize(heatmaps, (0, 0), fx=upsample_ratio, fy=upsample_ratio, interpolation=cv2.INTER_CUBIC)
  82. stage2_pafs = stages_output[-1]
  83. pafs = np.transpose(stage2_pafs.squeeze().cpu().data.numpy(), (1, 2, 0))
  84. pafs = cv2.resize(pafs, (0, 0), fx=upsample_ratio, fy=upsample_ratio, interpolation=cv2.INTER_CUBIC)
  85. return heatmaps, pafs, scale, pad

python基于openpose跌倒检测可生成视频调用摄像头_哔哩哔哩_bilibili

项目下载:

https://download.csdn.net/download/babyai996/87463578

0基础部署该项目视频教程:

python摔倒检测,跌倒检测openpose站立行为检测视频教程-深度学习文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/211645
推荐阅读
相关标签
  

闽ICP备14008679号