OpenPoseVideo.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import cv2
  2. import time
  3. import numpy as np
  4. import argparse
  5. parser = argparse.ArgumentParser(description='Run keypoint detection')
  6. parser.add_argument("--device", default="cpu", help="Device to inference on")
  7. parser.add_argument("--video_file", default="sample_video.mp4", help="Input Video")
  8. args = parser.parse_args()
  9. MODE = "MPI"
  10. if MODE == "COCO":
  11. protoFile = "pose/coco/pose_deploy_linevec.prototxt"
  12. weightsFile = "pose/coco/pose_iter_440000.caffemodel"
  13. nPoints = 18
  14. POSE_PAIRS = [ [1,0],[1,2],[1,5],[2,3],[3,4],[5,6],[6,7],[1,8],[8,9],[9,10],[1,11],[11,12],[12,13],[0,14],[0,15],[14,16],[15,17]]
  15. elif MODE == "MPI" :
  16. protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
  17. weightsFile = "pose/mpi/pose_iter_160000.caffemodel"
  18. nPoints = 15
  19. POSE_PAIRS = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,14], [14,8], [8,9], [9,10], [14,11], [11,12], [12,13] ]
  20. inWidth = 368
  21. inHeight = 368
  22. threshold = 0.1
  23. input_source = args.video_file
  24. cap = cv2.VideoCapture(input_source)
  25. hasFrame, frame = cap.read()
  26. vid_writer = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame.shape[1],frame.shape[0]))
  27. net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
  28. if args.device == "cpu":
  29. net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU)
  30. print("Using CPU device")
  31. elif args.device == "gpu":
  32. net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
  33. net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
  34. print("Using GPU device")
  35. while cv2.waitKey(1) < 0:
  36. t = time.time()
  37. hasFrame, frame = cap.read()
  38. frameCopy = np.copy(frame)
  39. if not hasFrame:
  40. cv2.waitKey()
  41. break
  42. frameWidth = frame.shape[1]
  43. frameHeight = frame.shape[0]
  44. inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
  45. (0, 0, 0), swapRB=False, crop=False)
  46. net.setInput(inpBlob)
  47. output = net.forward()
  48. H = output.shape[2]
  49. W = output.shape[3]
  50. # Empty list to store the detected keypoints
  51. points = []
  52. for i in range(nPoints):
  53. # confidence map of corresponding body's part.
  54. probMap = output[0, i, :, :]
  55. # Find global maxima of the probMap.
  56. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
  57. # Scale the point to fit on the original image
  58. x = (frameWidth * point[0]) / W
  59. y = (frameHeight * point[1]) / H
  60. if prob > threshold :
  61. cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
  62. cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)
  63. # Add the point to the list if the probability is greater than the threshold
  64. points.append((int(x), int(y)))
  65. else :
  66. points.append(None)
  67. # Draw Skeleton
  68. for pair in POSE_PAIRS:
  69. partA = pair[0]
  70. partB = pair[1]
  71. if points[partA] and points[partB]:
  72. cv2.line(frame, points[partA], points[partB], (0, 255, 255), 3, lineType=cv2.LINE_AA)
  73. cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
  74. cv2.circle(frame, points[partB], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
  75. cv2.putText(frame, "time taken = {:.3f} sec".format(time.time() - t), (50, 50), cv2.FONT_HERSHEY_COMPLEX, .8, (255, 50, 0), 2, lineType=cv2.LINE_AA)
  76. cv2.imshow('Output-Skeleton', frame)
  77. vid_writer.write(frame)
  78. vid_writer.release()