-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnalyzeVideos_streamLocalThreadedLED_final.py
356 lines (301 loc) · 11 KB
/
AnalyzeVideos_streamLocalThreadedLED_final.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""
Adapted from DeepLabCut Toolbox
by
B Forys, [email protected]
P Gupta, [email protected]
https://github.com/AlexEMG/DeepLabCut
by
A Mathis, [email protected]
M Mathis, [email protected]
This script analyzes a streaming video from a local webcam based
on a trained network. You need tensorflow for evaluation. Run by:
CUDA_VISIBLE_DEVICES=0 python3 AnalyzeVideos_streamLocal.py
"""
####################################################
# Dependencies
####################################################
import os.path
import sys
# import the Queue class from Python 3
if sys.version_info >= (3, 0):
from queue import Queue
else:
print('Please use Python3')
exit(0)
# Get subfolder containing this code
subfolder = os.getcwd().split('Analysis-tools')[0]
sys.path.append(subfolder)
# add parent directory: (where nnet & config are!)
sys.path.append(subfolder + "pose-tensorflow/")
sys.path.append(subfolder + "Generating_a_Training_Set")
# Import config information from myconfig_stream.py
from myconfig_stream import Task, date, \
trainingsFraction, resnet, snapshotindex, shuffle, \
cropping, x1, x2, y1, y2, plotimages, downscale, \
pics, t, avgs
# Deep-cut dependencies
import compileall
from config import load_config
from nnet import predict
from dataset.pose_dataset import data_to_input
import pickle
from skimage.util import img_as_ubyte
import skimage
import skimage.color
import pandas as pd
import numpy as np
import os
import io
import struct
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import time
import datetime
# Image processing dependencies
import cv2
from PIL import Image, ImageTk
import imutils
from imutils.video import WebcamVideoStream
from imutils.video import FPS
# Threading dependencies
from joblib import Parallel, delayed
import _thread
import threading
from copy import copy, deepcopy
# GPIO dependencies
from pyftdi.ftdi import Ftdi
from pyftdi.gpio import GpioController, GpioException
from os import environ
from led_test import LEDTest
####################################################
# Functions for processing each frame and analyzing its pose
####################################################
def get_cmap(n, name='hsv'):
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.
From DeepLabCut'''
return plt.cm.get_cmap(name, n)
def getpose(image, cfg, outputs, outall=False):
''' Adapted from DeeperCut, see pose-tensorflow folder.
From DeepLabCut'''
image_batch = data_to_input(skimage.color.gray2rgb(image))
outputs_np = sess.run(outputs, feed_dict={inputs: image_batch})
scmap, locref = predict.extract_cnn_output(outputs_np, cfg)
pose = predict.argmax_pose_predict(scmap, locref, cfg.stride)
if outall:
return scmap, locref, pose
else:
return pose
def frame_process(image, cfg, outputs, index):
'''Estimates pose in each frame.'''
pose = getpose(image, cfg, outputs)
print('Pose ' + str(index) + ' saved!')
PredicteData[index, :] = pose.flatten()
def frame_plot(frame, index):
'''Plots movement data on frames and saves frames.'''
tmp = deepcopy(frame)
tmp_no_label = deepcopy(frame)
# Plots average of all points, or all points separately, depending on config
if avgs:
img = cv2.circle(tmp, (int(x_avg), int(y_avg)), 6, colors[1],-1)
else:
for x_plt, y_plt, c in zip(x_range, y_range, colors):
cv2.circle(tmp, (int(PredicteData[index, :][x_plt]), \
int(PredicteData[index, :][y_plt])), 2, c,-1)
cv2.imwrite(out_dir + '/' + 'frame{}.png'.format(str(index)), tmp)
cv2.imwrite(out_no_label + '/' + 'no_label_frame{}.png'.format(str(index)), tmp_no_label)
def thresh(LED, reached, ind):
'''Controls GPIO (LED) output based on calculated threshold.'''
if reached:
print("Reached!")
LED_arr.append(ind)
# Default: Connect to GPIO port 7 on the breakout board
try:
LED.set_gpio(7, True)
except GpioException:
print("Error switching light on!")
time.sleep(0.02)
try:
LED.set_gpio(7, False)
except GpioException:
print("Error switching light off!")
time.sleep(0.02)
####################################################
# Loading data, and defining model folder
####################################################
basefolder = '../pose-tensorflow/models/' # for cfg file & ckpt!
modelfolder = (basefolder + Task + str(date) + '-trainset' +
str(int(trainingsFraction * 100)) + 'shuffle' + str(shuffle))
cfg = load_config(modelfolder + '/test/' + "pose_cfg.yaml")
##################################################
# Load and setup CNN part detector
##################################################
# Check which snap shots are available and sort them by # iterations
Snapshots = np.array([
fn.split('.')[0]
for fn in os.listdir(modelfolder + '/train/')
if "index" in fn
])
increasing_indices = np.argsort([int(m.split('-')[1]) for m in Snapshots])
Snapshots = Snapshots[increasing_indices]
print(modelfolder)
print(Snapshots)
##################################################
# Compute predictions over images
##################################################
# Check if data already was generated:
cfg['init_weights'] = modelfolder + '/train/' + Snapshots[snapshotindex]
# Get training iterations:
trainingsiterations = (cfg['init_weights'].split('/')[-1]).split('-')[-1]
# Name for scorer:
scorer = 'DeepCut' + "_resnet" + str(resnet) + "_" + Task + str(
date) + 'shuffle' + str(shuffle) + '_' + str(trainingsiterations)
cfg['init_weights'] = modelfolder + '/train/' + Snapshots[snapshotindex]
sess, inputs, outputs = predict.setup_pose_prediction(cfg)
pdindex = pd.MultiIndex.from_product(
[[scorer], cfg['all_joints_names'], ['x', 'y', 'likelihood']],
names=['scorer', 'bodyparts', 'coords'])
##################################################
# Datafolder
##################################################
# Pre-allocates array for data (change the first value in np.zeros to record more than 10,000 frames)
PredicteData = np.zeros((10000, 3 * len(cfg['all_joints_names'])))
index = 0
x_range = list(range(0,(3 * len(cfg['all_joints_names'])),3))
y_range = list(range(1,(3 * len(cfg['all_joints_names'])),3))
# Sets colors for cv2 plotting
colors = [(0, 0, 255), (0, 165, 255), (0, 255, 255), (0, 255, 0), (255, 0, 0), (240, 32, 160)]
# Sets output directory. When running multiple trials with same parameters, add a number
# to the end of this directory to prevent it from being overwritten
out_dir = '2MINS_THRESH_X_18_UPPERLIM_50_LED_02_out_' + Task + '/'
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# Generates a second directory with just the unlabelled frames (useful if you want to run
# further analysis on the real-time frames)
out_no_label = 'out_no_label_' + Task + '/'
if not os.path.exists(out_no_label):
os.makedirs(out_no_label)
# Initializes arrays that will store movement data and LED flash data
x_arr = []
y_arr = []
x_overall = []
y_overall = []
LED_arr = []
print("Starting to extract posture")
# Accept a single connection and make a file-like object out of it
vs = WebcamVideoStream(src=0).start()
fps = FPS().start()
start = time.time()
# Calls custom pyftdi class in led_test.py. mask is an 8-bit array defining
# which ports on your breakout board are inputs and outputs; by default, 0xFF
# (all ports are output ports) should work.
LED = LEDTest()
mask = 0xFF
try:
while True:
# Opens connection to breakout board
LED.open(mask)
frame = vs.read()
if cropping:
frame = frame[y1:y2, x1:x2]
w = x2-x1
h = y2-y1
else:
w = 400.0
h = 400.0
# if downscale == True, downscales frame by half
if downscale:
frame = imutils.resize(frame, width=400)
# Break program if no more frames are received from webcam
if frame is None:
break
if frame.any():
image = (img_as_ubyte(frame))
# Predict movement data from frame
frame_process(frame, cfg, outputs, index)
# Plot predicted movement data on frame
if plotimages:
# frame_plot(frame, index)
_thread.start_new_thread(frame_plot, (frame, index))
#threading.Thread(target=frame_plot, args=(frame, index))
# Maps recorded movement data for each frame to separate arrays for x
# and y coordinates
for a in PredicteData[index, 0:14:3]:
x_arr.append(a)
for b in PredicteData[index, 1:15:3]:
y_arr.append(b)
x_avg = np.mean(x_arr)
y_avg = np.mean(y_arr)
# get first body part only
# Change body part here:
x_first = PredicteData[index, 9]
y_first = PredicteData[index, 10]
# update the FPS counter
fps.update()
# calculate measures of central tendency and variance
x_avg = np.mean(x_arr)
y_avg = np.mean(y_arr)
x_overall.append(x_avg)
y_overall.append(y_avg)
#x_stdev = np.std(x_overall)
#y_stdev = np.std(y_overall)
x_stdev = 18 # right_paw: 14
y_stdev = 18 # right_paw: 14
x_upper_lim = 50
y_upper_lim = 50
# check whether movement in x or y direction exceeds threshold
# and send signal to activate LED if threshold exceeded
if(x_upper_lim >= abs(x_overall[index] - x_overall[index - 1]) >= x_stdev and \
#abs(y_overall[index] - y_overall[index - 1]) >= y_stdev and \
index >= 1):
ttt = True
_thread.start_new_thread(thresh, (LED, ttt, index))
x_arr = []
y_arr = []
x_first = 0
y_first = 0
index += 1
if time.time() - start > 120:
break
# Finish run early if Ctrl+C pressed
except KeyboardInterrupt:
print("Finished.")
finally:
# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
LED.close()
# server_socket.close()
stop = time.time()
stop_time = datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
dictionary = {
"start": start,
"stop": stop,
"run_duration": stop - start,
"Scorer": scorer,
"config file": cfg,
"frame_dimensions": (w, h),
"nframes": index
}
metadata = {'data': dictionary}
print("Frame rate: {} Hz".format(str(index / (stop - start))))
print("Saving results...")
PredicteData = PredicteData[0:index, :]
DataMachine = pd.DataFrame(
PredicteData, columns=pdindex, index=range(index))
AvgsMachine = pd.DataFrame(
{'x': x_overall, 'y': y_overall}
)
LED_frames = pd.DataFrame(
{'LED_frames': LED_arr})
# Record raw data (DataMachine), averaged data (AvgsMachine), and frames on which
# LED was activated (LED_frames) to csv
DataMachine.to_csv(out_dir + t + "_LEDwebcam_{}_{}_Run".format(Task, pics) + str(stop_time) + '.csv')
AvgsMachine.to_csv(out_dir + t + "_LEDwebcam_{}_{}_Run_avgs_".format(Task, pics) + str(stop_time) + '.csv')
LED_frames.to_csv(out_dir + t + "_LEDwebcam_{}_{}_LED_frames_".format(Task, pics) + str(stop_time) + '.csv')
print("Results saved!")