-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreal_time_ABRS.py
212 lines (135 loc) · 5.17 KB
/
real_time_ABRS.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#real_time_ABRS
# Copyright (c) 2019 Primoz Ravbar UCSB
# Licensed under BSD 2-Clause [see LICENSE for details]
# Written by Primoz Ravbar
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pickle
import msvcrt
from scipy import misc #pip install pillow
import scipy
from scipy import ndimage
from PIL import Image
from ABRS_modules import getting_frame_record
from ABRS_modules import center_of_gravity
from ABRS_modules import subtract_average
from ABRS_modules import smooth_2d
from ABRS_modules import smooth_1d
from ABRS_modules import discrete_radon_transform
from ABRS_modules import computeSpeedFromPosXY
from ABRS_modules import create_3C_image
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
cap = cv2.VideoCapture('Empty_Chrimson_dusted_1_2.avi');fb=4 #insert path to the raw movie; see README for the format
newSize = (400,400);
startFrame = 0;
endFrame = 50012;
kernelSize = 100
smoothingWindow = 89
windowSize = 10006 #size of window for training -- ignore in this version
winST = 16;
halfWindowSpeed = 15
ind = 0;
prevFrame = np.zeros((400,400))
frRec = np.zeros((16+1,newSize[0]*newSize[1]))
trainImRec = np.zeros((80*80,1000))
trainLabelRec = np.zeros((1,1000))
predictionsProbRec = np.zeros((10,endFrame))
etho = np.zeros((1,endFrame))
pathToABRSfolder = 'INSERT PATH TO ABRS MAIN FOLDER HERE'
model = keras.models.load_model('modelConv2ABRS_3C')
model.summary()
featureCol = np.zeros((30,1));
featureColAP = np.zeros((30,1));
posCol = np.zeros((2,1));
imCol = np.zeros((80*80,1));
behCol = np.zeros((1,1));
featureMat = np.zeros((30,kernelSize))
posMat = np.zeros((2,kernelSize))
imMat = np.zeros((80*80,windowSize))
behMat = np.zeros((1,windowSize))
im3Crec = np.zeros((1000,80,80,3))
kernelInd = 0
trainInd = windowSize
keyInd = 0
frameInd = 0
while(cap.isOpened()):
ret, frame = cap.read() #
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #
rs = cv2.resize(gray,(newSize[0],newSize[1]));
currentFrame = rs.astype(float)/1;
diffFrame = currentFrame - prevFrame;
prevFrame = currentFrame;
diffFrameAbs = np.absolute(diffFrame)
frameVect = currentFrame.reshape(1,newSize[0]*newSize[1]);
frameVectFloat = frameVect.astype(float);
frRecShort = np.delete(frRec, 0, 0);
frRec = np.vstack((frRecShort,frameVectFloat));
sumFrRec = np.sum(frRec,0);
posDic, maxMovement, cfrVectRec, frameVectFloatRec = getting_frame_record(frRec, 0, winST,fb);
im3CRaw = create_3C_image (cfrVectRec)
if np.count_nonzero(im3CRaw[:,:,0])>6400:
im3CRaw[:,:,0] = np.zeros((80,80))
if np.count_nonzero(im3CRaw[:,:,1])>800:
im3CRaw[:,:,1] = np.zeros((80,80))
rgbArray = np.zeros((80,80,3), 'uint8')
rgbArray[..., 0] = im3CRaw[:,:,0]
rgbArray[..., 1] = im3CRaw[:,:,1]
rgbArray[..., 2] = im3CRaw[:,:,2]
im3C = Image.fromarray(rgbArray)
X_rs = np.zeros((1,80,80,3))
X_rs[0,:,:,:]=im3C
storeFrameRec = 0
if storeFrameRec == 1:
im3Crec[frameInd,:,:,:]=im3C
X = X_rs/256 # normalize
predictionsProb = model.predict(X)
predictionsProbRec[:,ind] = predictionsProb
predictionLabel = np.zeros((1,np.shape(predictionsProb)[0]))
predictionLabel[0,:] = np.argmax(predictionsProb,axis=1)
beh = predictionLabel
if maxMovement < 200: #this is to
beh=7
etho[0,ind]=beh
print(beh)
###### this part is being developed for online training and for semi-automatic ethogram production
trainKey = 'n'
if keyInd == windowSize:
trainKey = input('train?')
if trainKey == 't':
trainLabelRec[0,trainInd-windowSize:trainInd] = behMat
trainImRec[:,trainInd-windowSize:trainInd] = imMat
trainInd = trainInd +windowSize
keyInd=0
print(trainKey)
if trainKey == 'f':
beh = input('behavior?')
trainLabelRec[0,trainInd-windowSize:trainInd] = beh
trainImRec[:,trainInd-windowSize:trainInd] = imMat
trainInd = trainInd +1
keyInd=0
print(trainKey)
if trainKey != 't' and keyInd>windowSize:
keyInd=0
print(trainKey)
keyInd = keyInd + 1
frameInd = frameInd + 1
##################################################################
cv2.imshow('im3CRaw',im3CRaw)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if ind > endFrame-1:
break
ind=ind+1
cap.release()
cv2.destroyAllWindows()