You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.4 KiB

  1. import cv2
  2. import time
  3. import numpy as np
  4. MODE = "MPI"
  5. if MODE is "COCO":
  6. protoFile = "pose/coco/pose_deploy_linevec.prototxt"
  7. weightsFile = "pose/coco/pose_iter_440000.caffemodel"
  8. nPoints = 18
  9. 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]]
  10. elif MODE is "MPI" :
  11. protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
  12. weightsFile = "pose/mpi/pose_iter_160000.caffemodel"
  13. nPoints = 15
  14. 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] ]
  15. frame = cv2.imread("single.jpeg")
  16. frameCopy = np.copy(frame)
  17. frameWidth = frame.shape[1]
  18. frameHeight = frame.shape[0]
  19. threshold = 0.2
  20. net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
  21. t = time.time()
  22. # input image dimensions for the network
  23. inWidth = 368
  24. inHeight = 368
  25. inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
  26. (0, 0, 0), swapRB=False, crop=False)
  27. net.setInput(inpBlob)
  28. output = net.forward()
  29. print("time taken by network : {:.3f}".format(time.time() - t))
  30. H = output.shape[2]
  31. W = output.shape[3]
  32. # Empty list to store the detected keypoints
  33. points = []
  34. for i in range(nPoints):
  35. # confidence map of corresponding body's part.
  36. probMap = output[0, i, :, :]
  37. # Find global maxima of the probMap.
  38. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
  39. # Scale the point to fit on the original image
  40. x = (frameWidth * point[0]) / W
  41. y = (frameHeight * point[1]) / H
  42. if prob > threshold :
  43. cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
  44. cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)
  45. # Add the point to the list if the probability is greater than the threshold
  46. points.append((int(x), int(y)))
  47. else :
  48. points.append(None)
  49. # Draw Skeleton
  50. for pair in POSE_PAIRS:
  51. partA = pair[0]
  52. partB = pair[1]
  53. if points[partA] and points[partB]:
  54. cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2)
  55. cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
  56. cv2.imshow('Output-Keypoints', cv2.resize(frameCopy,(900,900)))
  57. cv2.imshow('Output-Skeleton', cv2.resize(frame,(600,900)))
  58. cv2.imwrite('Output-Keypoints.jpg', frameCopy)
  59. cv2.imwrite('Output-Skeleton.jpg', frame)
  60. print("Total time taken : {:.3f}".format(time.time() - t))
  61. cv2.waitKey(0)