forked from stevenwudi/3DCNN_HMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step1_3DCNN_neutral_batch.py
124 lines (112 loc) · 5.04 KB
/
Step1_3DCNN_neutral_batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#-------------------------------------------------------------------------------
# Name: Starting Kit for ChaLearn LAP 2014 Track3
# Purpose: Show basic functionality of provided code
#
# Author: Xavier Baro
# Author: Di Wu: [email protected]
# Created: 24/03/2014
# Copyright: (c) Chalearn LAP 2014
# Licence: GPL3
#-------------------------------------------------------------------------------
import sys, os,random,numpy,zipfile
from shutil import copyfile
import matplotlib.pyplot as plt
import cv2
from ChalearnLAPEvaluation import evalGesture,exportGT_Gesture
from ChalearnLAPSample import GestureSample
from utils import IsLeftDominant
from utils import Extract_feature_normalized
from utils import Extract_feature
import time
import cPickle
from scipy.misc import imresize
""" Main script. Show how to perform all competition steps
Access the sample information to learn a model. """
# Data folder (Training data)
print("Extracting the training files")
data=os.path.join("I:\Kaggle_multimodal\Training\\")
# Get the list of training samples
samples=os.listdir(data)
STATE_NO = 10
batch_num = 1
# pre-allocating the memory
IM_SZ = 90
Feature_all = numpy.zeros(shape=(IM_SZ, IM_SZ, 4, 20000), dtype=numpy.uint8)
Targets = numpy.zeros(shape=(100000, 1), dtype=numpy.uint8)
cuboid_count = 0
###############################
# load prestore template
ref_depth = numpy.load('distance_median.npy')
template = cv2.imread('template.png')
template = numpy.mean(template, axis=2)
template /= template.max()
for file_count, file in enumerate(samples):
time_tic = time.time()
if (file_count<650):
print("\t Processing file " + file)
# Create the object to access the sample
smp=GestureSample(os.path.join(data,file))
# ###############################################
# USE Ground Truth information to learn the model
# ###############################################
# Get the list of actions for this frame
gesturesList=smp.getGestures()
total_frame = smp.getNumFrames()
##################################################
# obtain the shift and scaling according to
shift, scale = smp.get_shift_scale(template, ref_depth, start_frame=total_frame-100, end_frame=total_frame-10)
if numpy.isnan(scale):
scale = 1
for gesture in gesturesList:
gestureID,startFrame,endFrame=gesture
## extract first 4 frames
cuboid = numpy.zeros(( 90, 90, 4), numpy.uint8)
frame_count_temp = 0
if startFrame-5 > 1:
valid_count = 0
for x in range(startFrame-5, startFrame-1):
[img, flag] = smp.get_shift_scale_depth(shift, scale, x, IM_SZ, show_flag=True)
cuboid[:, :, frame_count_temp] = img
frame_count_temp +=1
if flag:# we actually detect some region
valid_count += 1
if valid_count==4: # all 4 frames are valid
Feature_all[:,:,:,cuboid_count] = cuboid
Targets[cuboid_count] = 201
cuboid_count += 1
## extract last 5 frames
cuboid = numpy.zeros(( 90, 90, 4), numpy.uint8)
frame_count_temp = 0
if endFrame+5 < smp.getNumFrames():
valid_count = 0
for x in range(endFrame+1, endFrame+5):
[img, flag] = smp.get_shift_scale_depth(shift, scale, x, IM_SZ, show_flag=True)
cuboid[:, :, frame_count_temp] = img
frame_count_temp +=1
if flag:# we actually detect some region
valid_count += 1
if valid_count==4: # all 4 frames are valid
Feature_all[:,:,:,cuboid_count] = cuboid
Targets[cuboid_count] = 201
cuboid_count += 1
# ###############################################
## delete the sample
del smp
print "Elapsed time %d sec" % int(time.time() - time_tic)
if(not file_count%20 and file_count>0) or file_count==649:
random_idx = numpy.random.permutation(range(cuboid_count))
save_dir = '.\storage_neutral\\'
print "batch: %d" % batch_num
data_temp = Feature_all[:,:,:,random_idx[:]]
data_id = Targets[random_idx[:]]
save_path= os.path.join(save_dir,'data_batch_neutral_'+str(batch_num))
out_file = open(save_path, 'wb')
dic = {'batch_label':['batch 1 of'+ str(batch_num)], 'data':data_temp,
'data_id':data_id}
cPickle.dump(dic, out_file)
out_file.close()
# pre-allocating the memory
batch_num += 1
Feature_all = numpy.zeros(shape=(90, 90, 4, 100000), dtype=numpy.uint8 )
Targets = numpy.zeros(shape=(100000, 1), dtype=numpy.uint8)
cuboid_count = 0