Skip to content

Instantly share code, notes, and snippets.

@chawasit
Created October 30, 2018 07:10
Show Gist options
  • Select an option

  • Save chawasit/0a553c6a53458d9822bff052a1388414 to your computer and use it in GitHub Desktop.

Select an option

Save chawasit/0a553c6a53458d9822bff052a1388414 to your computer and use it in GitHub Desktop.
# coding: utf-8
import os
import sys
from sys import platform
import numpy
import cv2 as cv2
import pandas
import codecs
import matplotlib.pyplot as plt
from scipy import stats
import util
from keras.models import load_model
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path + '/openpose-master/build/python/openpose/')
# Parameters for OpenPose. Take a look at C++ OpenPose example for meaning of components. Ensure all below are filled
try:
from openpose import *
except:
raise Exception(
'Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
params = dict()
params["logging_level"] = 3
params["output_resolution"] = "-1x-1"
params["net_resolution"] = "-1x368"
params["model_pose"] = "BODY_25"
params["alpha_pose"] = 0.6
params["scale_gap"] = 0.3
params["scale_number"] = 1
params["render_threshold"] = 0.05
# If GPU version is built, and multiple GPUs are available, set the ID here
params["num_gpu_start"] = 0
params["disable_blending"] = False
# Ensure you point to the correct path where models are located
# dir_path + "/../../../models/"
params["default_model_folder"] = "openpose-master/models/"
# Construct OpenPose object allocates GPU memory
openpose = OpenPose(params)
def extract_feature(keypoints):
keypoint = keypoints[0][0:8]
neck = keypoint[1]
right_shoulder = keypoint[2]
right_elbow = keypoint[3]
left_shoulder = keypoint[5]
left_elbow = keypoint[6]
shoulder_distance = (util.euclidean_distance(left_shoulder[:2], neck[:2]) + util.euclidean_distance(right_shoulder[:2], neck[:2])) / 2.0
return numpy.array([
[(kp[0] - keypoint[1][0]) / shoulder_distance if kp[2] > 0 else 0,
(kp[1] - keypoint[1][1]) / shoulder_distance if kp[2] > 0 else 0,
kp[2]]
for kp in keypoint[2:8]
]).flatten()
df = pandas.read_csv('punch-counting-dataset.csv', encoding='utf8')
video_no = input('video no: ')
cap = cv2.VideoCapture('Examples/%s.mp4' % (video_no))
left_punch_count = 0
right_punch_count = 0
is_left_punching = False
is_right_punching = False
frame_count = 1
x_data = []
left_punch_data = []
right_punch_data = []
no_punch_data = []
THRESHOLD = 0.5
model = None
counted = False
predict = [0,0,0]
while 1:
# Read new image
grabbed, img = cap.read()
if not grabbed:
break
h, w, _ = img.shape
# Outvariance_of_laplaciane with the human skeleton blended on it
keypoints, output_image = openpose.forward(img, True)
# Print the human pose keypoints, i.e., a [#people x #keypoints x 3]-dimensional numpy object with the keypoints of all the people on that image
if keypoints.shape[0] >= 1:
feature = extract_feature(keypoints)
if not model:
model = load_model('model_temp.h5')
predict = model.predict(numpy.array([feature]))[0]
is_left_punching = is_left_punching * 0.4 + 0.6 * predict[1]
is_right_punching = is_right_punching * 0.4 + 0.6 * predict[2]
if not counted:
if is_left_punching > THRESHOLD:
left_punch_count += 1
counted = 1
if is_right_punching > THRESHOLD:
right_punch_count += 1
counted = 2
else:
if is_left_punching < THRESHOLD and counted == 1:
counted = 0
if is_right_punching < THRESHOLD and counted == 2:
counted = 0
# Display the image
cv2.putText(output_image, 'NO_PNCH %2d' % (predict[0] * 100), (0, 50), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 250, 0.7), 4)
cv2.putText(output_image, 'L_PUNCH %2d' % (is_left_punching * 100), (0, 100), cv2.FONT_HERSHEY_PLAIN, 3, (250, 150, 50, 0.7), 4)
cv2.putText(output_image, 'R_PUNCH %2d' % (is_right_punching * 100), (0, 150), cv2.FONT_HERSHEY_PLAIN, 3, (220, 250, 50, 0.7), 4)
cv2.putText(output_image, str(right_punch_count), (int(w / 4) - 40, int(h / 2)), cv2.FONT_HERSHEY_PLAIN , 6, (220, 250, 50, 0.7) , 4)
cv2.putText(output_image, str(left_punch_count), (int(w / 4 * 3) - 40, int(h / 2)), cv2.FONT_HERSHEY_PLAIN, 6, (250, 150, 50, 0.7), 4)
cv2.imshow("output", output_image)
# graph stat
x_data.append(frame_count)
no_punch_data.append(predict[0])
left_punch_data.append(is_left_punching)
right_punch_data.append(is_right_punching)
frame_count += 1
cv2.waitKey(1)
plt.plot(x_data, left_punch_data, 'b', x_data, right_punch_data, 'g', x_data, no_punch_data, 'r')
plt.show()
ans = df.iloc[int(video_no) - 1]
left_error = numpy.abs(left_punch_count - ans[2])
right_error = numpy.abs(right_punch_count - ans[3])
mse_error = (left_error + right_error) / 2
print('error left: %2d, right: %2d' % (left_error, right_error))
print('MSE = %2f' % (mse_error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment