Skip to content

Commit

Permalink
removed savgol filter from handpose
Browse files Browse the repository at this point in the history
  • Loading branch information
vikasgupta-github committed Oct 24, 2018
1 parent daaa99d commit b30c390
Showing 1 changed file with 24 additions and 67 deletions.
91 changes: 24 additions & 67 deletions HandPose/handPoseVideo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import cv2
import time
import numpy as np
import scipy
from scipy import signal


protoFile = "hand/pose_deploy.prototxt"
weightsFile = "hand/pose_iter_102000.caffemodel"
Expand All @@ -11,6 +10,7 @@

threshold = 0.2


input_source = "asl.mp4"
cap = cv2.VideoCapture(input_source)
hasFrame, frame = cap.read()
Expand All @@ -27,22 +27,13 @@

net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
k = 0

smoothed_data, data, previous_points, frame_number = [], [], [], 0
for i in range(nPoints): previous_points.append((0,0))
# Smoothing parameters - choose a window length of about 1/2 to 1/4 the fps (so 60 fps --> window length of 33)
window_length, exponent_value = 33,2 # Window length must be odd

### This is going to take some time before the party get's started! The downside of smoothing is that
### data from the past and the future is required to smooth data in the present. This means that all the frames
### in the video must be processed before smoothing the data and displaying the result. This method is therefore
### not suitable for realtime results.
while 1:
k+=1
t = time.time()
hasFrame, frame = cap.read()
frameCopy = np.copy(frame)
if not hasFrame:
cv2.waitKey()
break

inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
Expand Down Expand Up @@ -72,64 +63,30 @@
# Add the point to the list if the probability is greater than the threshold
points.append((int(point[0]), int(point[1])))
else :
# Add the last known point (ex: if thumb is not detected, use thumb position from previous frame)
points.append(previous_points[i])

# Save the data from the model - data is a list of lists. Each element is a list containing the 22 coordinates for the hand.
data.append(points)

previous_points = points

print("total = {}".format(time.time() - t))

# Re-capture the source, so that the video starts at the beginning again
cap = cv2.VideoCapture(input_source)
points.append(None)

# Smooth it out
# Split the data so that just the x values for the first point are made into a list
smoothed_data_in_series = []
for point_index in range(nPoints): # Iterate through each point (wrist, thumb, etc...)
data_point_series_x = []
data_point_series_y = []
for values in data: # Iterate through the series of data (each frame of video)
data_point_series_x.append(values[point_index][0])
data_point_series_y.append(values[point_index][1])
# Smooth the x and y values
smoothed_data_point_series_x = signal.savgol_filter(data_point_series_x, window_length, exponent_value)
smoothed_data_point_series_y = signal.savgol_filter(data_point_series_y, window_length, exponent_value)
smoothed_data_in_series.append(smoothed_data_point_series_x)
smoothed_data_in_series.append(smoothed_data_point_series_y)

# Now the data is sepearted into 44 lists (two lists for each of the 22 locations of the hand, time to zip()
for current_frame_number in range(len(smoothed_data_in_series[0])):
frame_values = []
for point_index in range(nPoints):
x = smoothed_data_in_series[point_index*2][current_frame_number]
y = smoothed_data_in_series[point_index*2+1][current_frame_number]
frame_values.append((x,y))
smoothed_data.append(frame_values)

# Iterate through each frame of data
for values in smoothed_data:

hasFrame, img = cap.read()

# When data is smoothed, floats are introduced, these must be eliminated becase cv2.circle requries integers
values = np.array(values, int)

# Draw Skeleton
for pair in POSE_PAIRS:
partA = pair[0]
partB = pair[1]
cv2.line(img, (values[partA][0], values[partA][1]),(values[partB][0], values[partB][1]), (32, 255, 0), 2, lineType=cv2.LINE_AA)
cv2.circle(img, (values[partA][0], values[partA][1]), 2, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
cv2.circle(img, (values[partB][0], values[partB][1]), 2, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)

cv2.imshow('Output-Skeleton', img)
cv2.waitKey(0)
vid_writer.write(img)

cv2.destroyAllWindows()
vid_writer.release()
cap.release()

if points[partA] and points[partB]:
cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2, lineType=cv2.LINE_AA)
cv2.circle(frame, points[partA], 5, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
cv2.circle(frame, points[partB], 5, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)

print("Time Taken for frame = {}".format(time.time() - t))

# 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)
# cv2.putText(frame, "Hand Pose using OpenCV", (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 50, 0), 2, lineType=cv2.LINE_AA)
cv2.imshow('Output-Skeleton', frame)
# cv2.imwrite("video_output/{:03d}.jpg".format(k), frame)
key = cv2.waitKey(1)
if key == 27:
break

print("total = {}".format(time.time() - t))

vid_writer.write(frame)

vid_writer.release()

0 comments on commit b30c390

Please sign in to comment.