-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analysis.py
190 lines (159 loc) · 5.58 KB
/
Analysis.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import sys
import extras
import cv2
import cv2.cv as cv
import numpy as np
import pylab as pl
import matplotlib.cm as mpl_cm
from matplotlib import path as mpl_path
class analyze(object):
def __init__(self, File_Saving_Dict):
pass
self.quitList = ['q', 'Q', 'quit', 'Quit']
self.yesList = ['y', 'Y', 'yes', 'YES', 'Yes']
self.noList = ['n', 'N', 'No', 'NO', 'no']
self.allList = ['a', 'A', 'All', 'all', 'ALL']
self.ps3List = ['ps3', 'Ps3', 'PS3', 'pS3', 'ps', 'Ps', 'pS', 'PS']
self.pgList = ['pg', 'Pg', 'pG', 'PG']
self.File_Saving_Dict = File_Saving_Dict
self.VideoNum = 1
#RMS Display Parameters
self.param_names = ['movement_std_threshold', 'wheel_translation','wheel_stretch']
self.params = {}
def maskedPlotRMS(self):
#Private Constants
analyze_video_type = None
video_type_dict = {1:self.File_Saving_Dict["PS3_Target_SaveName"], 2:self.File_Saving_Dict["PG_Target_SaveName"]}
allVideos = False
while True:
input = extras.getUserInput("Analyze Trial Number:")
try:
input = int(input)
except:
pass
if (type(input) == int and os.path.isdir(self.File_Saving_Dict["File_Complete_Target_Location"],'Trial%i'%self.trialNum)):
self.trialNum = input
break
else:
print "Please enter a valid number"
print " "
print "Would you like to analyze PS3 Videos or Point Grey Videos?"
print "Enter either 'PS3' or 'PG'"
while True:
input = extras.getUserInput("Type of Video:")
input = int(input)
if input in self.quitList:
return None
if (input in self.ps3List):
self.savename = self.PS3_SaveName
print 'Analyzing playstation videos...'
break
elif (input in self.pgList):
self.savename = self.pgFileName
print 'Analyzing point grey videos...'
break
else:
print "Please enter a valid response"
print " "
print "Which video number would you like to image?"
print "If you would like to image them all, enter 'a'"
while True:
input = extras.getUserInput("Input:")
input = int(input)
if input in self.quitList:
return None
if (type(input) == int and os.path.isfile(os.path.join(self.fileTargetLocation,'\Trial%i'%self.trialNum,video_type_dict[analyze_video_type]+'%i'%input))):
self.videoNum = input
break
elif input in self.allList:
allVideos = True
else:
print "Please enter a valid number"
Continue == True
while Continue:
m=0
success, frame = self.camera.vc.read()
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#frame = frame.astype(np.uint8)
#frame, timestamp = self.camera.read()
pl.figure()
pl.title("Select mask number: %s."%str(m+1))
pl.imshow(frame, cmap=mpl_cm.Greys_r)
pts = []
while not len(pts):
pts = pl.ginput(0)
pl.close()
path = mpl_path.Path(pts)
mask = np.ones(np.shape(frame), dtype=bool)
for ridx,row in enumerate(mask):
for cidx,pt in enumerate(row):
if path.contains_point([cidx, ridx]):
mask[ridx,cidx] = False
self.mask_pts[m] = np.array(pts, dtype=np.int32)
self.masks[m] = mask
self.mask_idx[m] = np.where(mask==False)
while True:
input = extras.getUserInput("Mask Name:")
if type(input) == str:
self.mask_name[m] = input
break
else:
print "Please enter a valid name"
#np.save(os.path.join(self.fileTargetLocation,'Analysis_mask_'+self.mask_name[m]), np.atleast_1d([self.masks]))
while True:
input = extras.getUserInput("Enter Another Mask? (Y/N):")
if input in self.yesList:
break
elif input in self.noList:
Continue = False
break
elif input in self.quitList:
return None
else:
print "Please enter a valid response."
pl.ion()
self.fig = pl.figure()
self.ax = self.fig.add_subplot(111)
self.ax.set_ylim([-1, 255])
self.plotdata = {m:self.ax.plot(np.arange(self.monitor_vals_display),np.zeros(self.monitor_vals_display), c)[0] for m,c in zip(self.mask_name,['r-'])}
self.plotline, = self.ax.plot(np.arange(self.monitor_vals_display), np.repeat(self.params['movement_std_threshold'], self.monitor_vals_display), 'r--')
if not allVideos:
dir = os.listdir(self.fileTargetLocationRoot+str(self.trialNum))
if not len(dir) == 0:
n = 1
while os.path.isfile(os.path.join(self.fileTargetLocationRoot+str(self.trialNum),self.savename + str(self.videoNum)+'.avi')):
n += 1
for self.VideoNum in range(n):
self.std_dev_plot(os.path.join(self.fileTargetLocationRoot+str(self.trialNum), self.savename+str(self.videoNum)+'.avi'))
else:
self.std_dev_plot(os.path.join(self.fileTargetLocationRoot+str(self.trialNum), self.savename+str(self.videoNum)+'.avi'))
# cap = cv2.VideoCapture(os.path.join(self.fileTargetLocationRoot+str(self.trialNum), self.savename+str(self.videoNum)+'.avi'))
# fourcc = cv2.cv.CV_FOURCC(*'XVID')
# width = int(cap.get(3))
# height = int(cap.get(4))
# numFramesInMovie = int(cap.get(cv.CV_CAP_PROP_FRAME_COUNT))
# fps = int(cap.get(cv.CV_CAP_PROP_FPS))
out = cv2.VideoWriter(os.path.join(self.fileTargetLocationRoot+'%i'%self.sortNum,self.pgFileName+vidNumStr+'.avi'),fourcc, fps, (width,height))
f, img = cap.read()
frameCounter = 1
frames = self.camera.get(vc.VC_CAP_PROP_FRAME_COUNT)
for counter in range(frames):
pass
def menu(self):
print "Analysis Options:"
print "1: Masked RMS Difference Plot"
print "2: Quit"
while True:
input = raw_input("Your Input:")
input = input.rstrip()
input = int(input)
if type(input) == int:
if input == 1:
self.maskedPlotRMS()
elif input == 2:
break
else:
print "Please enter a valid number"
def std_dev_plot(self, video_file):
pass