Guest User

Untitled

a guest
Sep 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import cv2, os
  4. import csv
  5.  
  6. input_source = "/home/stephen/Desktop/me.MP4"
  7. cap = cv2.VideoCapture(input_source)
  8. frame_number = 0
  9. font, scale, colorText, thick = cv2.FONT_HERSHEY_SIMPLEX, .5, (234,234,234), 1
  10. size, color, thickness = 5, (255,255,255), 5
  11. #get pose data - data is generated by open pose video
  12. df = pd.read_csv('/home/stephen/Desktop/open_pose_data.csv')
  13.  
  14. # there are 15 points in the skeleton
  15. # 0 head
  16. # 1 neck
  17. # 2, 5 shoulders
  18. # 3, 6 elbows
  19. # 4, 7 hands
  20. # 8, 11 hips
  21. # 9, 12 knees
  22. # 10, 13 ankles
  23. # 14 torso
  24.  
  25. data = []
  26. while cv2.waitKey(10) < 0 and frame_number<len(df.values)-2:
  27. ret, img = cap.read()
  28. if not ret: break
  29. try: values = df.values[frame_number]
  30. except: break
  31. values = np.array(values, int)
  32. points = []
  33. points.append((values[0], values[1]))
  34. points.append((values[2], values[3]))
  35. points.append((values[4], values[5]))
  36. points.append((values[6], values[7]))
  37. points.append((values[8], values[9]))
  38. points.append((values[10], values[11]))
  39. points.append((values[12], values[13]))
  40. points.append((values[14], values[15]))
  41. points.append((values[16], values[17]))
  42. points.append((values[18], values[19]))
  43. points.append((values[20], values[21]))
  44. points.append((values[22], values[23]))
  45. points.append((values[24], values[25]))
  46. points.append((values[26], values[27]))
  47. points.append((values[28], values[29]))
  48.  
  49.  
  50. #create a blank list to store the non-swapped poitns
  51. non_swap_points = []
  52. for i in range(15): non_swap_points.append((0,0))
  53. #add the head, that point never changes
  54. non_swap_points[0] = points[0]
  55. #add the neck, that point never changes
  56. non_swap_points[1] = points[1]
  57. #add the torso, that never changes
  58. non_swap_points[14] = points[14]
  59.  
  60. #swap the left and right shoulders (2 and 5)
  61. if points[2][0]<points[5][0]:
  62. non_swap_points[2] = points[2]
  63. non_swap_points[5] = points[5]
  64. else:
  65. non_swap_points[2] = points[5]
  66. non_swap_points[5] = points[2]
  67. # swap the elbows
  68. if points[3][0]<points[6][0]:
  69. non_swap_points[3] = points[3]
  70. non_swap_points[6] = points[6]
  71. else:
  72. non_swap_points[6] = points[3]
  73. non_swap_points[3] = points[6]
  74.  
  75. #swap the hands
  76. if points[4][0]<points[7][0]:
  77. non_swap_points[4] = points[4]
  78. non_swap_points[7] = points[7]
  79. else:
  80. non_swap_points[7] = points[4]
  81. non_swap_points[4] = points[7]
  82. #swap the hips
  83. if points[8][0]<points[11][0]:
  84. non_swap_points[11] = points[11]
  85. non_swap_points[8] = points[8]
  86. else:
  87. non_swap_points[8] = points[11]
  88. non_swap_points[11] = points[8]
  89.  
  90. #swap the knees
  91. if points[9][0]<points[12][0]:
  92. non_swap_points[9] = points[9]
  93. non_swap_points[12] = points[12]
  94. else:
  95. non_swap_points[12] = points[9]
  96. non_swap_points[9] = points[12]
  97.  
  98. #swap the feet
  99. if points[10][0]<points[13][0]:
  100. non_swap_points[10] = points[10]
  101. non_swap_points[13] = points[13]
  102. else:
  103. non_swap_points[13] = points[10]
  104. non_swap_points[10] = points[13]
  105.  
  106.  
  107. for point in non_swap_points:
  108. cv2.circle(img, point, 3, (0, 0, 255), 3)
  109. cv2.putText(img, str(non_swap_points.index(point)), point, font, scale, colorText, thick, cv2.LINE_AA)
  110.  
  111. cv2.imshow('Output-Skeleton', img)
  112. frame_number+=1
  113. data.append(non_swap_points)
  114. cv2.destroyAllWindows()
  115.  
  116.  
  117. with open('/home/stephen/Desktop/swapped_body_parts.csv', 'w') as csvfile:
  118. fieldnames = []
  119. for i in range(30): fieldnames.append(str(i))
  120. writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  121. writer.writeheader()
  122.  
  123. for trick in data:
  124. writer.writerow({'0': trick[0][0],
  125. '1':trick[0][1],
  126. '2':trick[1][0],
  127. '3':trick[1][1],
  128. '4':trick[2][0],
  129. '5':trick[2][1],
  130. '6':trick[3][0],
  131. '7':trick[3][1],
  132. '8':trick[4][0],
  133. '9':trick[4][1],
  134. '10':trick[5][0],
  135. '11':trick[5][1],
  136. '12':trick[6][0],
  137. '13':trick[6][1],
  138. '14':trick[7][0],
  139. '15':trick[7][1],
  140. '16':trick[8][0],
  141. '17':trick[8][1],
  142. '18':trick[9][0],
  143. '19':trick[9][1],
  144. '20':trick[10][0],
  145. '21':trick[10][1],
  146. '22':trick[11][0],
  147. '23':trick[11][1],
  148. '24':trick[12][0],
  149. '25':trick[12][1],
  150. '26':trick[13][0],
  151. '27':trick[13][1],
  152. '28':trick[14][0],
  153. '29':trick[14][1]})
Add Comment
Please, Sign In to add comment