OpenPoseImage.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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("--image_file", default="single.jpeg", help="Input image")
  8. args = parser.parse_args()
  9. MODE = "COCO"
  10. if MODE is "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 is "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. frame = cv2.imread(args.image_file)
  21. frameCopy = np.copy(frame)
  22. frameWidth = frame.shape[1]
  23. frameHeight = frame.shape[0]
  24. threshold = 0.1
  25. net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
  26. if args.device == "cpu":
  27. net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU)
  28. print("Using CPU device")
  29. elif args.device == "gpu":
  30. net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
  31. net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
  32. print("Using GPU device")
  33. t = time.time()
  34. # input image dimensions for the network
  35. inWidth = 368
  36. inHeight = 368
  37. inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
  38. (0, 0, 0), swapRB=False, crop=False)
  39. net.setInput(inpBlob)
  40. output = net.forward()
  41. print("time taken by network : {:.3f}".format(time.time() - t))
  42. H = output.shape[2]
  43. W = output.shape[3]
  44. # Empty list to store the detected keypoints
  45. points = []
  46. for i in range(nPoints):
  47. # confidence map of corresponding body's part.
  48. probMap = output[0, i, :, :]
  49. # Find global maxima of the probMap.
  50. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
  51. # Scale the point to fit on the original image
  52. x = (frameWidth * point[0]) / W
  53. y = (frameHeight * point[1]) / H
  54. if prob > threshold :
  55. cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
  56. cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)
  57. # Add the point to the list if the probability is greater than the threshold
  58. points.append((int(x), int(y)))
  59. else :
  60. points.append(None)
  61. # Draw Skeleton
  62. for pair in POSE_PAIRS:
  63. partA = pair[0]
  64. partB = pair[1]
  65. if points[partA] and points[partB]:
  66. cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2)
  67. cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
  68. cv2.imshow('Output-Keypoints', frameCopy)
  69. cv2.imshow('Output-Skeleton', frame)
  70. cv2.imwrite('Output-Keypoints.jpg', frameCopy)
  71. cv2.imwrite('Output-Skeleton.jpg', frame)
  72. print("Total time taken : {:.3f}".format(time.time() - t))
  73. cv2.waitKey(0)