openpose.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # To use Inference Engine backend, specify location of plugins:
  2. # source /opt/intel/computer_vision_sdk/bin/setupvars.sh
  3. import cv2 as cv
  4. import numpy as np
  5. import argparse
  6. parser = argparse.ArgumentParser(
  7. description='This script is used to demonstrate OpenPose human pose estimation network '
  8. 'from https://github.com/CMU-Perceptual-Computing-Lab/openpose project using OpenCV. '
  9. 'The sample and model are simplified and could be used for a single person on the frame.')
  10. parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera')
  11. parser.add_argument('--proto', help='Path to .prototxt')
  12. parser.add_argument('--model', help='Path to .caffemodel')
  13. parser.add_argument('--dataset', help='Specify what kind of model was trained. '
  14. 'It could be (COCO, MPI, HAND) depends on dataset.')
  15. parser.add_argument('--thr', default=0.1, type=float, help='Threshold value for pose parts heat map')
  16. parser.add_argument('--width', default=368, type=int, help='Resize input to specific width.')
  17. parser.add_argument('--height', default=368, type=int, help='Resize input to specific height.')
  18. parser.add_argument('--scale', default=0.003922, type=float, help='Scale for blob.')
  19. args = parser.parse_args()
  20. if args.dataset == 'COCO':
  21. BODY_PARTS = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
  22. "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
  23. "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
  24. "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }
  25. POSE_PAIRS = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
  26. ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
  27. ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
  28. ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
  29. ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]
  30. elif args.dataset == 'MPI':
  31. BODY_PARTS = { "Head": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
  32. "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
  33. "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "Chest": 14,
  34. "Background": 15 }
  35. POSE_PAIRS = [ ["Head", "Neck"], ["Neck", "RShoulder"], ["RShoulder", "RElbow"],
  36. ["RElbow", "RWrist"], ["Neck", "LShoulder"], ["LShoulder", "LElbow"],
  37. ["LElbow", "LWrist"], ["Neck", "Chest"], ["Chest", "RHip"], ["RHip", "RKnee"],
  38. ["RKnee", "RAnkle"], ["Chest", "LHip"], ["LHip", "LKnee"], ["LKnee", "LAnkle"] ]
  39. elif args.dataset == 'HAND':
  40. BODY_PARTS = { "Wrist": 0,
  41. "ThumbMetacarpal": 1, "ThumbProximal": 2, "ThumbMiddle": 3, "ThumbDistal": 4,
  42. "IndexFingerMetacarpal": 5, "IndexFingerProximal": 6, "IndexFingerMiddle": 7, "IndexFingerDistal": 8,
  43. "MiddleFingerMetacarpal": 9, "MiddleFingerProximal": 10, "MiddleFingerMiddle": 11, "MiddleFingerDistal": 12,
  44. "RingFingerMetacarpal": 13, "RingFingerProximal": 14, "RingFingerMiddle": 15, "RingFingerDistal": 16,
  45. "LittleFingerMetacarpal": 17, "LittleFingerProximal": 18, "LittleFingerMiddle": 19, "LittleFingerDistal": 20,
  46. }
  47. POSE_PAIRS = [ ["Wrist", "ThumbMetacarpal"], ["ThumbMetacarpal", "ThumbProximal"],
  48. ["ThumbProximal", "ThumbMiddle"], ["ThumbMiddle", "ThumbDistal"],
  49. ["Wrist", "IndexFingerMetacarpal"], ["IndexFingerMetacarpal", "IndexFingerProximal"],
  50. ["IndexFingerProximal", "IndexFingerMiddle"], ["IndexFingerMiddle", "IndexFingerDistal"],
  51. ["Wrist", "MiddleFingerMetacarpal"], ["MiddleFingerMetacarpal", "MiddleFingerProximal"],
  52. ["MiddleFingerProximal", "MiddleFingerMiddle"], ["MiddleFingerMiddle", "MiddleFingerDistal"],
  53. ["Wrist", "RingFingerMetacarpal"], ["RingFingerMetacarpal", "RingFingerProximal"],
  54. ["RingFingerProximal", "RingFingerMiddle"], ["RingFingerMiddle", "RingFingerDistal"],
  55. ["Wrist", "LittleFingerMetacarpal"], ["LittleFingerMetacarpal", "LittleFingerProximal"],
  56. ["LittleFingerProximal", "LittleFingerMiddle"], ["LittleFingerMiddle", "LittleFingerDistal"] ]
  57. else:
  58. raise(Exception("you need to specify either 'COCO', 'MPI', or 'Hand' in args.dataset"))
  59. inWidth = args.width
  60. inHeight = args.height
  61. inScale = args.scale
  62. net = cv.dnn.readNet(cv.samples.findFile(args.proto), cv.samples.findFile(args.model))
  63. cap = cv.VideoCapture(args.input if args.input else 0)
  64. while cv.waitKey(1) < 0:
  65. hasFrame, frame = cap.read()
  66. if not hasFrame:
  67. cv.waitKey()
  68. break
  69. frameWidth = frame.shape[1]
  70. frameHeight = frame.shape[0]
  71. inp = cv.dnn.blobFromImage(frame, inScale, (inWidth, inHeight),
  72. (0, 0, 0), swapRB=False, crop=False)
  73. net.setInput(inp)
  74. out = net.forward()
  75. assert(len(BODY_PARTS) <= out.shape[1])
  76. points = []
  77. for i in range(len(BODY_PARTS)):
  78. # Slice heatmap of corresponding body's part.
  79. heatMap = out[0, i, :, :]
  80. # Originally, we try to find all the local maximums. To simplify a sample
  81. # we just find a global one. However only a single pose at the same time
  82. # could be detected this way.
  83. _, conf, _, point = cv.minMaxLoc(heatMap)
  84. x = (frameWidth * point[0]) / out.shape[3]
  85. y = (frameHeight * point[1]) / out.shape[2]
  86. # Add a point if it's confidence is higher than threshold.
  87. points.append((int(x), int(y)) if conf > args.thr else None)
  88. for pair in POSE_PAIRS:
  89. partFrom = pair[0]
  90. partTo = pair[1]
  91. assert(partFrom in BODY_PARTS)
  92. assert(partTo in BODY_PARTS)
  93. idFrom = BODY_PARTS[partFrom]
  94. idTo = BODY_PARTS[partTo]
  95. if points[idFrom] and points[idTo]:
  96. cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
  97. cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
  98. cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
  99. t, _ = net.getPerfProfile()
  100. freq = cv.getTickFrequency() / 1000
  101. cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
  102. cv.imshow('OpenPose using OpenCV', frame)