handPoseVideo.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import cv2
  2. import time
  3. import numpy as np
  4. protoFile = "hand/pose_deploy.prototxt"
  5. weightsFile = "hand/pose_iter_102000.caffemodel"
  6. nPoints = 22
  7. POSE_PAIRS = [ [0,1],[1,2],[2,3],[3,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[10,11],[11,12],[0,13],[13,14],[14,15],[15,16],[0,17],[17,18],[18,19],[19,20] ]
  8. threshold = 0.2
  9. input_source = "asl.mp4"
  10. cap = cv2.VideoCapture(input_source)
  11. hasFrame, frame = cap.read()
  12. frameWidth = frame.shape[1]
  13. frameHeight = frame.shape[0]
  14. aspect_ratio = frameWidth/frameHeight
  15. inHeight = 368
  16. inWidth = int(((aspect_ratio*inHeight)*8)//8)
  17. vid_writer = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 15, (frame.shape[1],frame.shape[0]))
  18. net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
  19. k = 0
  20. while 1:
  21. k+=1
  22. t = time.time()
  23. hasFrame, frame = cap.read()
  24. frameCopy = np.copy(frame)
  25. if not hasFrame:
  26. cv2.waitKey()
  27. break
  28. inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
  29. (0, 0, 0), swapRB=False, crop=False)
  30. net.setInput(inpBlob)
  31. output = net.forward()
  32. print("forward = {}".format(time.time() - t))
  33. # Empty list to store the detected keypoints
  34. points = []
  35. for i in range(nPoints):
  36. # confidence map of corresponding body's part.
  37. probMap = output[0, i, :, :]
  38. probMap = cv2.resize(probMap, (frameWidth, frameHeight))
  39. # Find global maxima of the probMap.
  40. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
  41. if prob > threshold :
  42. cv2.circle(frameCopy, (int(point[0]), int(point[1])), 6, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
  43. cv2.putText(frameCopy, "{}".format(i), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_SIMPLEX, .8, (0, 0, 255), 2, lineType=cv2.LINE_AA)
  44. # Add the point to the list if the probability is greater than the threshold
  45. points.append((int(point[0]), int(point[1])))
  46. else :
  47. points.append(None)
  48. # Draw Skeleton
  49. for pair in POSE_PAIRS:
  50. partA = pair[0]
  51. partB = pair[1]
  52. if points[partA] and points[partB]:
  53. cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2, lineType=cv2.LINE_AA)
  54. cv2.circle(frame, points[partA], 5, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
  55. cv2.circle(frame, points[partB], 5, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
  56. print("Time Taken for frame = {}".format(time.time() - t))
  57. # cv2.putText(frame, "time taken = {:.2f} sec".format(time.time() - t), (50, 50), cv2.FONT_HERSHEY_COMPLEX, .8, (255, 50, 0), 2, lineType=cv2.LINE_AA)
  58. # cv2.putText(frame, "Hand Pose using OpenCV", (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 50, 0), 2, lineType=cv2.LINE_AA)
  59. cv2.imshow('Output-Skeleton', frame)
  60. # cv2.imwrite("video_output/{:03d}.jpg".format(k), frame)
  61. key = cv2.waitKey(1)
  62. if key == 27:
  63. break
  64. print("total = {}".format(time.time() - t))
  65. vid_writer.write(frame)
  66. vid_writer.release()