def showNearestMHIs(testMoments, trainMoments, trainDirectoryNames, K): distance = np.zeros((1, trainMoments.shape[0])) nMHI = np.zeros((480, 640, K + 1)) for i in range(0, trainMoments.shape[0]): distance[:, i] = np.sqrt( np.sum( np.divide( np.power( np.reshape(trainMoments[i, :], (-1, 1)) - np.reshape(testMoments, (-1, 1)), 2), np.reshape(np.nanvar(trainMoments, axis=0), (-1, 1))))) sorted_train_directory_names = np.reshape( trainDirectoryNames[np.argsort(distance)], (trainDirectoryNames.shape)) for i in range(0, K + 1): nMHI[:, :, i] = computeMHI(sorted_train_directory_names[i, :][0]) return nMHI
def showNearestMHI(testMoments, trainMoments, train_dir,K): train_set_len = trainMoments.shape[0] variance = np.nanvar(trainMoments, axis=0).reshape(-1, 1) # variance=variance.reshape(20, 1) distance = np.zeros((1, train_set_len)) testMoments = np.reshape(testMoments, (-1, 1)) # variance=variance.reshape(20, 1) nearest_MHI=np.zeros((480,640,K+1)) for i in range(0, train_set_len): distance[:,i] = normalized_euclidean(testMoments,trainMoments,variance,i) sorted_index = np.argsort(distance) #predictedAction = np.int(trainLabels[sorted_index][0]) #sorted directory shape = train_dir.shape sorted_directory=train_dir[sorted_index].reshape(shape) #find k image for i in range (0,K+1): print("compute MHI for ", i ,"Nearest ") nearest_MHI[:,:,i]= computeMHI(np.str(sorted_directory[i,:][0])) return nearest_MHI
import matplotlib.pyplot as plt import numpy as np from computeMHI import computeMHI from data_paths import seqpaths def display_mhi(mhi, action): plt.figure() plt.imshow(mhi, cmap='gray') plt.title(action) plt.xticks([]) plt.yticks([]) plt.savefig(action + "_mhi.png") plt.show() if __name__ == '__main__': # generate all MHIs allMHIs_list = [] for path in seqpaths: H = computeMHI(path) allMHIs_list.append(H) allMHIs = np.stack(allMHIs_list, axis=-1) # dump MHIs with open('allMHIs.npy', "wb") as f: pickle.dump(allMHIs, f)
import cv2 import os from predictAction import predictAction from computeMHI import computeMHI from huMoments import huMoments basedir = './' actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] allMHIs_counter = 0 allMHIs = np.empty((480,640,20), dtype=np.float32) allMoments = np.empty((20,7), dtype=np.float64) for actionnum in range(len(actions)): subdirname = basedir + actions[actionnum] + '/' subdir = os.listdir(subdirname) for seqnum in range(len(subdir)): allMHIs[:,:,allMHIs_counter] = computeMHI(subdirname + subdir[seqnum]) allMoments[allMHIs_counter,:] = huMoments(allMHIs[:,:,allMHIs_counter]) allMHIs_counter += 1 allLabels = [i for i in xrange(1,6) for j in xrange(4)] allLabels = np.array(allLabels) #%% actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] #actions = ['leftarmup'] #trainMHI = np.empty((480,640,20), dtype=np.float32) #trainMHI_counter = 0 np.save("huVectors", allMoments) np.save("allMHIs", allMHIs) allMoments = allMoments/np.linalg.norm(allMoments) confusionMatrix = np.zeros((5,5)) for actionnum in range(len(actions)):
from huMoments import huMoments basedir = './' actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] for actionnum in range(len(actions)): subdirname = basedir + actions[actionnum] + '/' subdir = os.listdir(subdirname) allMHIs = np.empty((480,640,20), dtype=np.float32) allMHIs_counter = 0 for seqnum in range(len(subdir)): # cycle through all sequences for this action category allMHIs_counter = allMHIs_counter + 1 allMHIs[:,:,allMHIs_counter] = computeMHI(subdirname + subdir[seqnum]) cv2.imshow(actions[actionnum]+"- %d"% seqnum, allMHIs[:,:,allMHIs_counter]) # cv2.imshow("mask", motion_mask) if 0xFF & cv2.waitKey(1) == 27: break #cv2.destroyAllWindows() #%% allHuMoments = np.empty((20,7), dtype=np.float32) for i in range(allMHIs.shape[2]): allHuMoments[i,:] = huMoments(allMHIs[:,:,i]) #%%
""" Created on Thu Nov 28 18:11:16 2019 @author: songl """ from computeMHI import computeMHI import matplotlib.pyplot as plt import numpy as np import os MHI = computeMHI('./PS5_Data/botharms/botharms-up-p1-1') plt.title('Both Arms') plt.imshow(MHI) plt.show() plt.imsave('botharms-up-p1-1_MHI.png', MHI) np.save('botharms-up-p1-1_MHI.npy', MHI) MHI = computeMHI('./PS5_Data/crouch/crouch-p1-1') plt.title('Crouch') plt.imshow(MHI) plt.show() plt.imsave('crouch-p1-1_MHI.png', MHI) np.save('crouch-p1-1_MHI.npy', MHI) MHI = computeMHI('./PS5_Data/leftarmup/leftarm-up-p1-1') plt.title('Left Arm Up') plt.imshow(MHI) plt.show() plt.imsave('leftarm-up-p1-1_MHI.png', MHI) np.save('leftarm-up-p1-1_MHI.npy', MHI)
from huMoments import huMoments basedir = './' actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] allMHIs = list() for action in actions: subdirname = basedir + action + '/' subdirs = os.listdir(subdirname) for seq in subdirs: # cycle through all sequences for this action category directory = subdirname + seq mhi = computeMHI(directory) allMHIs.append(mhi) allMHIs = np.stack(allMHIs, axis=2) np.save('allMHIs.npy', allMHIs) print 'saved MHIs' allHus = list() allMHIs = np.load('allMHIs.npy') _, _, numSeqs = np.shape(allMHIs) for i in range(numSeqs): mhi = allMHIs[:, :, i] huVec = huMoments(mhi) allHus.append(huVec)