opencv openpose

举报
风吹稻花香 发表于 2021/06/05 00:28:04 2021/06/05
【摘要】   cpu比较卡,躺着好像不能检测  # To use Inference Engine backend, specify location of plugins:# export LD_LIBRARY_PATH=/opt/intel/deeplearning_deploymenttoolkit/deployment_tools/external/mk...

 

cpu比较卡,躺着好像不能检测 


  
  1. # To use Inference Engine backend, specify location of plugins:
  2. # export LD_LIBRARY_PATH=/opt/intel/deeplearning_deploymenttoolkit/deployment_tools/external/mklml_lnx/lib:$LD_LIBRARY_PATH
  3. import cv2 as cv
  4. import numpy as np
  5. import argparse
  6. import time
  7. parser = argparse.ArgumentParser(
  8. description='This script is used to demonstrate OpenPose human pose estimation network '
  9. 'from https://github.com/CMU-Perceptual-Computing-Lab/openpose project using OpenCV. '
  10. 'The sample and model are simplified and could be used for a single person on the frame.')
  11. parser.add_argument('--input', default='pbug3_450x420.avi', help='Path to video. Skip to capture frames from camera')
  12. parser.add_argument('--ouput', default='outpose.avi', help='Path to output video')
  13. parser.add_argument('--proto', default='pose_deploy_linevec.prototxt', help='Path to .prototxt')
  14. parser.add_argument('--model', default='pose_iter_440000.caffemodel', help='Path to .caffemodel')
  15. parser.add_argument('--dataset', default='COCO', help='Specify what kind of model was trained. '
  16. 'It could be (COCO, MPI) depends on dataset.')
  17. parser.add_argument('--thr', default=0.1, type=float, help='Threshold value for pose parts heat map')
  18. parser.add_argument('--width', default=368, type=int, help='Resize input to specific width.')
  19. parser.add_argument('--height', default=368, type=int, help='Resize input to specific height.')
  20. args = parser.parse_args()
  21. if args.dataset == 'COCO':
  22. BODY_PARTS = {"Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
  23. "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
  24. "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
  25. "LEye": 15, "REar": 16, "LEar": 17, "Background": 18}
  26. POSE_PAIRS = [["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
  27. ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
  28. ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
  29. ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
  30. ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"]]
  31. else:
  32. assert (args.dataset == 'MPI')
  33. BODY_PARTS = {"Head": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
  34. "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
  35. "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "Chest": 14,
  36. "Background": 15}
  37. POSE_PAIRS = [["Head", "Neck"], ["Neck", "RShoulder"], ["RShoulder", "RElbow"],
  38. ["RElbow", "RWrist"], ["Neck", "LShoulder"], ["LShoulder", "LElbow"],
  39. ["LElbow", "LWrist"], ["Neck", "Chest"], ["Chest", "RHip"], ["RHip", "RKnee"],
  40. ["RKnee", "RAnkle"], ["Chest", "LHip"], ["LHip", "LKnee"], ["LKnee", "LAnkle"]]
  41. # visualize
  42. colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0],
  43. [0, 255, 0], \
  44. [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255],
  45. [85, 0, 255], \
  46. [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
  47. inWidth = args.width
  48. inHeight = args.height
  49. net = cv.dnn.readNetFromCaffe(args.proto, args.model)
  50. cap = cv.VideoCapture(0)
  51. # 视频总帧数
  52. frameNum = cap.get(cv.CAP_PROP_FRAME_COUNT)
  53. # vedio writer
  54. # fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
  55. fourcc = cv.VideoWriter_fourcc(*'XVID')
  56. # 保存size必须和输出size设定为一致,否则无法写入保存文件
  57. w = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
  58. h = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
  59. size = (w, h)
  60. poseout = cv.VideoWriter(args.ouput, fourcc, 20.0, size)
  61. # create a black use numpy,size is:512*512
  62. poseFrame = np.zeros((h, w, 3), np.uint8)
  63. # fill the image with white
  64. poseFrame.fill(0)
  65. # cv.namedWindow('OpenPose')
  66. print('Start...')
  67. print('共计{}帧图像.'.format(frameNum))
  68. # 计时开始
  69. start_time = time.time()
  70. j = 0
  71. while True:
  72. hasFrame, frame = cap.read()
  73. if not hasFrame:
  74. # cv.waitKey()
  75. break
  76. frameWidth = frame.shape[1]
  77. frameHeight = frame.shape[0]
  78. inp = cv.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
  79. (0, 0, 0), swapRB=False, crop=False)
  80. net.setInput(inp)
  81. out = net.forward()
  82. # assert(len(BODY_PARTS) == out.shape[1])
  83. # reset
  84. points = []
  85. poseFrame.fill(0)
  86. for i in range(len(BODY_PARTS)):
  87. # Slice heatmap of corresponging body's part.
  88. heatMap = out[0, i, :, :]
  89. # Originally, we try to find all the local maximums. To simplify a sample
  90. # we just find a global one. However only a single pose at the same time
  91. # could be detected this way.
  92. _, conf, _, point = cv.minMaxLoc(heatMap)
  93. x = (frameWidth * point[0]) / out.shape[3]
  94. y = (frameHeight * point[1]) / out.shape[2]
  95. # Add a point if it's confidence is higher than threshold.
  96. points.append((int(x), int(y)) if conf > args.thr else None)
  97. # *************** points ***********
  98. # print(points)
  99. i = 0
  100. for pair in POSE_PAIRS:
  101. partFrom = pair[0]
  102. partTo = pair[1]
  103. # assert(partFrom in BODY_PARTS)
  104. # assert(partTo in BODY_PARTS)
  105. idFrom = BODY_PARTS[partFrom]
  106. idTo = BODY_PARTS[partTo]
  107. if points[idFrom] and points[idTo]:
  108. cv.line(frame, points[idFrom], points[idTo], colors[i], 3)
  109. cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
  110. cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
  111. i += 1
  112. # t, _ = net.getPerfProfile()
  113. # freq = cv.getTickFrequency() / 1000
  114. # cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
  115. # neck = points[BODY_PARTS['Neck']]
  116. # left_wrist = points[BODY_PARTS['LWrist']]
  117. # right_wrist = points[BODY_PARTS['RWrist']]
  118. # print(neck, left_wrist, right_wrist)
  119. # if neck and left_wrist and right_wrist and left_wrist[1] < neck[1] and right_wrist[1] < neck[1]:
  120. # cv.putText(frame, 'HANDS UP!', (10, 100), cv.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 2)
  121. cv.imshow('OpenPose', frame)
  122. cv.waitKey(1)
  123. # poseout.write(poseFrame)
  124. # cv.imwrite('outpose/pose_%d.jpg'%(j), frame)
  125. # j += 1
  126. # if j % 20 == 0:
  127. # # 记录时间
  128. # end_time = time.time()
  129. # print('已处理{}/{}帧图像, 用时{:.4f}s, 平均每帧用时{:.4f}s'.format(j, int(frameNum), end_time - start_time,
  130. # (end_time - start_time) / j))
  131. # 计时结束
  132. end_time = time.time()
  133. print('共处理{}帧图像,用时{:.4f}s'.format(j, end_time - start_time))
  134. cap.release()
  135. poseout.release()
  136. cv.destroyAllWindows()

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/115035831

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。