-
Notifications
You must be signed in to change notification settings - Fork 2
/
videoPrep.py
192 lines (172 loc) · 5.77 KB
/
videoPrep.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
191
# Sets the following parameters, with user input:
#
# Background: average of N random frames
# Threshold: intensity value applied to the background subtracted image
# Diameter for morphological operations
# Boundaries for Nest and Food area, position of the nest and middle of arena
# Start and End time for analysis (start applies to 1st movie and end to last movies of the series)
#
# User selects the first file of a movie series and the script creates a $HOME/analysis/ dir with the basename
# of the file series with the following files:
#
# bg.npy - background
# pmts.npy - values for arena areas and threholding
# listOfFiles.txt - files in the series to be analysed
# times.txt - start and end time for analysis
#
# vPrep can be called with "False" argument to ommit visually checking the output of the parameters selected
#
# Tiago Branco - March 2014
import numpy as np
import matplotlib.pylab as plt
import os.path
import videoAnalysisLib as va
import tkFileDialog
import os
import sys
#fname = '/lmb/home/tbranco/115954a_hab2.avi'
#saveDir = '/lmb/home/tbranco/analysis/'
if len(sys.argv)>1:
check = sys.argv[1]
else:
check = True
def vPrep(fname, saveDir, check=True):
fileName = va.getFileName(fname)
aviProps = va.getAVIinfo(fname)
plt.ion()
# fBackground frames
# -----------------
# Default values
nFrames = 10
bgOK = False
bgFile = saveDir + "/bg.npy"
if os.path.isfile(bgFile):
print("Background file exists, loading...")
bg = np.load(bgFile)
else:
print("Generating background with default settings...")
bg = va.getBg(fname, aviProps, nFrames)
if check:
while not bgOK:
plt.figure()
plt.imshow(bg)
plt.set_cmap('gray')
plt.show()
uDecision = raw_input("Background OK? [y]es; [n]o ")
if uDecision=='y':
bgOK = True
else:
uframes = raw_input("Select number of frames ")
bg = va.getBg(fname, aviProps, int(uframes))
np.save(saveDir + 'bg', bg)
print('Background file saved')
# Threshold
# --------------
# Default values
ths = 40
morphDiameter = 10
pmtsFileExists = False
thsOK = False
pmtsFile = saveDir + "/pmts.npy"
if os.path.isfile(pmtsFile):
print("Parameters file exists, loading...")
pmtsFileExists = True
filePmts = np.load(pmtsFile)
ths, morphDiameter = va.setThreshold(fname, aviProps, bg, filePmts[0][0], filePmts[1][0])
else:
print("Generating threshold with default settings...")
ths, morphDiameter = va.setThreshold(fname, aviProps, bg, ths, morphDiameter)
if check:
while not thsOK:
uDecision = raw_input("Threshold OK? [y]es; [n]o ")
if uDecision=='y':
thsOK = True
else:
while True:
try:
print "Current threshold is", ths
uths = int(raw_input("Select new threshold "))
print "Current diameter is", morphDiameter
umorph = int(raw_input("Select new diameter to erode and dilate "))
break
except ValueError:
print("Invalid number, please try again ")
ths, morphDiameter = va.setThreshold(fname, aviProps, bg, uths, umorph)
# Arena areas
# --------------
# Default values
nestPos = [125, 220]
nestArea = [(1,320),(225,320),(120,125)]
arenaCenter = [600,244]
foodArea = [(785,360),(960,360),(860,185)]
plt.ioff()
arenaOk = False
if pmtsFileExists:
print("Using parameters file...")
va.plotArena(aviProps, filePmts, bg)
pmts = [[ths], [morphDiameter], filePmts[2], filePmts[3], filePmts[4], filePmts[5]]
else:
print("Generating arena with default settings...")
pmts = [[ths], [morphDiameter], nestPos, nestArea, arenaCenter, foodArea]
va.plotArena(aviProps, pmts, bg)
if check:
while not arenaOk:
uDecision = raw_input("Arena OK? [y]es; [n]o ")
if uDecision=='y':
arenaOk = True
else:
print("Select new arena ")
points = va.setPoints(fname, aviProps, bg)
pmts = [[ths], [morphDiameter], points[0], points[1], points[2], points[3]]
#print pmts[0], pmts[1]
#fsaveName = saveDir + fileName.rstrip(".avi") + "_pmts"
np.save(saveDir + 'pmts', pmts)
print('Parameters file saved')
# --------------------------------------------------------------------------------------
# Select movies to analyse
initialdir = '/fhgfs/brancolab'
#initialdir = '/isilon/brancolab/Sabine/'
fname = tkFileDialog.askopenfilenames(title='Choose the 1st file of the movie series', initialdir=initialdir, filetypes=[('Movie files', '*.avi')])
directory, filename = os.path.split(fname[0])
basename = filename.rstrip('-------------.avi')
#adir = '/lmb/home/tbranco/analysis/' + basename + '/'
adir = '/fhgfs/brancolab/' + basename + '/'
os.system('mkdir -p '+ adir)
while True:
try:
tStart = int(raw_input("Select start time "))
tEnd = int(raw_input("Select end time "))
break
except ValueError:
print("Invalid number, please try again ")
temp = sys.stdout
sys.stdout = open(adir+'listOfFiles.txt', 'w')
print directory
filenames = []
for f in os.listdir(directory):
if ('cam2' in basename):
if f.startswith(basename):
filenames.append(f)
else:
if (f.startswith(basename)) & (('cam2' in f)==False):
#n = len(f.rstrip('.avi'))-len(basename)+1
#os.system('ln -s '+directory+'/'+f+' '+tempdir+'/file.'+str(n))
filenames.append(f)
# Order filenames and print to file
counter = 0
while counter<len(filenames):
n = -1
nfile = 0
while n!=counter:
f = filenames[nfile]
n = len(f.rstrip('.avi'))-len(basename)
nfile+=1
print f
counter+=1
sys.stdout.close()
sys.stdout = open(adir+'times.txt', 'w')
print tStart
print tEnd
sys.stdout = temp
# Define parameters
vPrep(fname[0], adir, check=check)