-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmilltracker.py
1540 lines (1455 loc) · 70.7 KB
/
tmilltracker.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Treadmill Animal Tracking and Feedback Control
(c) Andrew J. Spence
Structure and Motion Lab
Royal Veterinary College
11 Mar 2013
aspence at rvc dot ac dot uk
www.spencelab.com
$ Revision 0.5 Alpha $
The code is released under the GNU GPL Version 3. You should have recieved a copy of this license with this code, in the file `LICENSE.TXT`.
*If you build this system or use this code, please cite the following paper*::
Spence, A. J., Nicholson-Thomas, G., and Lampe, R. (2013 (in press)).
"Closing the loop in legged neuromechanics: an open-source computer vision
controlled treadmill." Journal of Neuroscience Methods.
http://dx.doi.org/10.1016/j.jneumeth.2013.03.009
We hope that you find this code useful, and we welcome suggestions to improve the code at
http://github.com/aspence/tmilltracker
This code is pre-Alpha and very unstable. No warranty is implied or given on it's performance.
==============================================
Usage:
Tracking from FireWire camera using fast camiface library, and requiring user input of metadata:
python tmilltracker.py
Ignore metadata and leave at defaults, but can be overriden later with input commands from the menu:
python tmilltracker.py -I
Use slower 12Hz OpenCV highgui for acuisition (does not require import of motmot.pycamiface module):
python tmilltracker.py --noci
Use a directory full of png images of following format fr00001_20130215_154312p232123.png for video stream:
python tmilltracker.py -I --imgs ~/mydirectory/fr20130206_165921p382628/
python tmilltracker.py -I --imgs '/Users/aspence/Documents/work/treadmill/harriet/30hz mouse with without overlay/fr20130205_115303p154501_noover'
'''
import numpy as np, matplotlib as mpl, pylab as pl
import tty, termios, select, sys, os, serial, threading, time, socket, re
import cv
import shelve
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
class TmillSerialInterface:
'''
This class handles control of a Panlab treadmill over the serial port.
It is based on a description of the protocol provided by Panlab s.e.
'''
def __init__(self, port, baudrate, parity, rtscts, xonxoff, echo=False):
try:
self.serial = serial.serial_for_url(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=1)
except AttributeError:
# This can happen when pyserial is older than 2.5.
# We use the serial class directly in that case
self.serial = serial.Serial(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=1)
self.verbose=False
def start(self):
self.buf = ''
self.alive = True
self.starttime = time.time()
# Start recieving thread
self.receiver_thread = threading.Thread(target=self.reader)
self.receiver_thread.setDaemon(1)
self.receiver_thread.start()
def stop(self):
self.alive = False
def join(self):
self.receiver_thread.join()
def reader(self):
"""
This thread loops, parsing any incoming velocity messages
and storing them locally. These could be time stamped and
logged for timing purposes, but at present are not used.
"""
try:
while self.alive:
data = self.serial.read(1)
# When we get XON, start saving data, when we get XOFF, check for proper format,
# if so extract speed and store with timestamp.
if data == '\x11':
# Start of packet: clear buffer
self.buf = ''
elif data == '\x13':
# End of packet: parse and append to log file:
if self.buf[0:6] == 'VELOCI' and len(self.buf)==9:
self.spd = int(self.buf[7:9])
if self.verbose:
sys.stderr.write("VELOCI MSG: " +self.buf+"\n")
sys.stderr.flush()
else:
# Presently unparsed message:
if self.verbose:
sys.stderr.write("UNKNOWN MSG: " +self.buf+"\n")
sys.stderr.flush()
self.buf = ''
else:
# Add character to buffer:
self.buf = self.buf + data
except serial.SerialException, e:
self.alive = False
raise
def tcontrol(self,onoff,verbose=False):
# Set PC control of treadmill on/off
if onoff:
# Send on message
if verbose:
sys.stderr.write('Sending PC OONN message ...\n')
sys.stderr.flush()
self.serial.write('\x11PC OONN\x13')
return True # should check here
else:
if verbose:
sys.stderr.write('Sending PC OOFF message ...\n')
sys.stderr.flush()
self.serial.write('\x11PC OOFF\x13')
def runstop(self,runstop,verbose=False):
# Set treadmill to RUN or STOP
if runstop:
if verbose:
sys.stderr.write('Sending RUN**** message ...\n')
sys.stderr.flush()
self.serial.write('\x11RUN****\x13')
else:
if verbose:
sys.stderr.write('Sending STOP*** message ...\n')
sys.stderr.flush()
self.serial.write('\x11STOP***\x13')
def setspd(self,spd,verbose=False):
# Set treadmill speed:
msg = '\x11VELOCID%03d\x13' % spd
if verbose:
sys.stderr.write('Sending ' + msg + ' message ...\n')
sys.stderr.flush()
self.serial.write(msg)
class TmillTracker():
'''
This class handles the majority of tracking the animal and controlling the
treadmill.
'''
def __init__(self, haveserial=True, noci=False, imgs="",debug=False ):
# Store options
self.debug=debug
self.host = 'crunch2' #remote host
self.port = 50002 # must match open port on host
self.useci = not noci
self.state = "idle"
self.roiinput = False
self.measureinput = False
self.drawline = False
self.drawroi = False
self.usekalman = True
self.imgs = imgs
self.roipt1 = (27, 279)
self.roipt2 = (618, 409)
d = shelve.open("shelf")
if (d.has_key("roipt1") and d.has_key("roipt2")):
self.roipt1 = d["roipt1"]
self.roipt2 = d["roipt2"]
self.roirect = (self.roipt1[0],self.roipt1[1],self.roipt2[0]-self.roipt1[0],self.roipt2[1]-self.roipt1[1])
self.setpt = 0.5 # Set point for controller -- 50% of treadmill
self.areapct = 0.7 # Take contours with area +- 70% around given value e.g. 2200. High value for robustness.
self.area = 2200
self.thresh = 70 # Threshold for contour finding, out of 255 for 8 bit images.
if d.has_key("thresh"):
self.thresh=d["thresh"]
# BETTER SETTINGS FOR MOUSE 09/05/2013 kp = 0.35, kd=20
# at kp=0.5, kd=10, mouse was getting far up belt before it caught them
# at kp = 0.35, kd=15, better, smooth ramp up with mouse, still getting far forward though and then fast catch
# Previous PD settings were driving current mouse to back of treadmill, reducing p gait slightly to compensate. Previous settings kp=0.35, kd=15
# Just lower p gain to 0.25 and leave d gain at 15 and see how we do.
self.kp = 0.25 # Position gain at 15 fps. Not that will be scaled by actual framerate used. Works well at 30 fps with gui on. Jumpy at 150 fps.
self.kd = 15 # See below
self.tmillwidth = 410*0.001 # 41 cm belt, see specs of Panlab.
if(d.has_key("tmillwidth")):
self.tmillwidth = d["tmillwidth"]
self.updaterates = { "guioff":{"video":150.0,"serial":50.0}, "guion":{"video":30.0,"serial":30.0}, "noci":{"video":12,"serial":12} }
self.logging = False
self.logstarttime = np.nan
self.keyinput = False
self.camcap = None
self.cvwindow = False
self.alive = True
self.verbose = False
self.prevwn = "Tracker preview image"
self.tcontrol = False
self.trunning = False
self.vidwriting = False
self.framewriting = False
self.behavtrig = False
self.perturb = False
self.applyingpert = False
self.pertdur = 0.2 # Perturbation lasts 200ms, down and up ramps
self.pertfrac = 0.5 # Perturbation loses 100% of current speed
self.pertresponsedur = 0.75 # Seconds to record after perturbation finishes.
self.pertvidsavetime = -1
self.pertwait = 10.0
self.pertendtime = -np.Inf
self.guion = True
self.tmspd = 0
# Load left and right bracket key speeds if stored
if(d.has_key("lbrk_spd")):
self.lbrk_spd = d["lbrk_spd"]
else:
self.lbrk_spd = 15
if(d.has_key("rbrk_spd")):
self.rbrk_spd = d["rbrk_spd"]
else:
self.rbrk_spd = 30
self.pa = cv.CreateMat(1,1, cv.CV_32FC2)
self.haveserial = haveserial
self.fliplr = False
self.overlay = True
self.lasttracktime = time.time()
self.lastserial = time.time()
self.grabstop = -np.inf
self.lastsercmdtime = -np.inf
self.trackdata = np.empty((150*3600,18,)) # at 150 FPS buffer for 1 hour data, 18 columns.
self.framenum = 0
self.animalnum = -1
self.trialnum = 1
self.weight = -1
self.notes = ''
self.startBehavior = -1
self.endBehavior = -1
self.animspd = 0.0
self.invert = False
self.capsecs = 1.0
# Give lastgoodtracktime a value because np.nan implies have a good track.
# Setting to a time will cause slowdown if we have no track.
self.lastgoodtracktime = time.time()
self.timesincetrack = time.time() - self.lastgoodtracktime
d.close()
if self.usekalman:
self.setupKalman()
self.setUpdateRates()
def get_help_text(self):
return """=== TmillTracker 0.5 alpha - help ===
---
--- Escape or 'q' - Exit program
--- h - Print this help menu
--- s - Display settings
--- v - Toggle verbosity
--- p - Toggle single preview image capture from camera
--- u - Update preview image
--- o - Set ROI for treadmill video tracker on preview image
- NOTE: Click top left first then drag to bottom right and release
--- H - Set threshold for finding contours: default = 70; range 0-255
--- d - Display live video feed
--- c - Toggle control treadmill
--- r - Toggle treadmill run/stop
--- 0-9 - Set treadmill speed 0 - 90 cm/s
--- [, ] - Set speed to preset values. Default [=15 cm/s, ]=30 cm/s
--- shift-[, -] - Set the preset speed value of these keys.
--- C - Calibrate length scale
--- a - Calibrate animal size ellipse
--- M - Measure and set ellipse area
--- n - Set animal ID number
--- N - Input notes string for trial
--- T - Set trial number
--- l - Toggle data logging (autostarts with feedback mode)
--- - Toggle belt speed polling (not implemented yet)
--- - Toggle digital trigger time stamp (req AD board and comedi) (not implemented)
--- t - Toggle video tracking of animal (number keys 0-9 work to manually set belt speed)
--- f - Toggle belt feedback control mode
--- V - Log video to file
--- F - Log video Frames
--- b - Behaviour trigger (High speed video record)
--- <return> - Store last 2 seconds of HSV (in tracking mode only)
--- e - Tell HS camera code to Exit
--- S - Save current trackdata to shelf
--- O - Toggle Overlay on images on screen and disk: speed, FPS, etc.
--- g - Toggle GUI display of video on/off
--- P - Toggle Perturbation mode on/off
--- i - Toggle invert image (use for white mice)
"""
def getSettings(self):
return """
--- Settings ---
ROI: (top: %d, left: %d, width %d, height %d)
Video rate: %d
Serial rate: %d
Elipse area: %d
Threshold for contour detection: %d
Treadmill width calibration (meters): %f
Overlay: %d
Animal num: %d
Trial num: %d
Weight: %3.3f
Notes: %s
Left bracket key speed: %d
Right bracket key speed: %d
""" % (self.roirect + (self.vidrate,self.serialrate,self.area,self.thresh,self.tmillwidth,self.overlay,self.animalnum,self.trialnum,self.weight,self.notes,self.lbrk_spd,self.rbrk_spd))
def mouseHandler(self,event,x,y,flags,param):
if self.roiinput:
if event == cv.CV_EVENT_LBUTTONDOWN:
# Store click
self.roipt1 = (x, y)
self.drawroi = True
elif event == cv.CV_EVENT_MOUSEMOVE:
# Draw a rectangle
if self.drawroi:
img1 = cv.CloneImage(self.prevfr);
cv.Rectangle(img1, (self.roipt1[0], self.roipt1[1]), (x,y), cv.Scalar(0, 0, 255, 0), 2, 8, 0)
cv.ShowImage(self.prevwn,img1)
elif event == cv.CV_EVENT_LBUTTONUP:
self.drawroi = False
self.roiinput = False
self.roipt2 = (x,y)
self.roirect = (self.roipt1[0],self.roipt1[1],self.roipt2[0]-self.roipt1[0],self.roipt2[1]-self.roipt1[1])
print "ROI set to x1 %d y1 %d x2 %d y2 %d" % (self.roipt1[0], self.roipt1[1],self.roipt2[0],self.roipt2[1])
self.prevfr = self.getGrayImage()
# Overlay new ROI
cv.Rectangle(self.prevfr, (self.roipt1[0], self.roipt1[1]), (self.roipt2[0],self.roipt2[1]), cv.Scalar(0, 0, 255, 0), 2, 8, 0)
cv.ShowImage(self.prevwn,self.prevfr)
# Save new ROI
d = shelve.open("shelf")
d["roipt1"] = self.roipt1
d["roipt2"] = self.roipt2
d.close()
if self.measureinput:
if event == cv.CV_EVENT_LBUTTONDOWN:
# Store click
self.mpt1 = (x, y)
self.drawline = True
elif event == cv.CV_EVENT_MOUSEMOVE:
# Draw line
if self.drawline:
img1 = cv.CloneImage(self.prevfr);
cv.Line(img1, (self.mpt1[0], self.mpt1[1]), (x,y), cv.Scalar(0, 0, 255, 0), 2, 8, 0)
cv.ShowImage(self.prevwn,img1)
elif event == cv.CV_EVENT_LBUTTONUP:
self.drawline = False
self.measureinput = False
self.mpt2 = (x,y)
measlength = np.sqrt((self.mpt1[0]-self.mpt2[0])**2.0 + (self.mpt1[1]-self.mpt2[1])**2.0 )
print "Line length: %f\n" % measlength
self.prevfr = self.getGrayImage()
# Overlay new ROI
cv.Line(self.prevfr, (self.mpt1[0], self.mpt1[1]), (x,y), cv.Scalar(0, 0, 255, 0), 2, 8, 0)
cv.ShowImage(self.prevwn,self.prevfr)
print "Ellipse area based on this length (width=1.8x length) = %f\n" % (np.pi*measlength*measlength/(1.8*4.0))
print "Setting area to this value.\n"
self.area = np.pi*measlength*measlength/(1.8*4.0)
def openCam(self):
# Start a capture using opencv_highgui or libcamiface
if self.imgs == "":
if self.useci:
mode_num = 6
device_num = 0
num_buffers = 1
self.camcap = cam_iface.Camera(device_num,num_buffers,mode_num)
self.camcap.start_camera()
self.camcap.set_framerate(420)
else:
# Handle roi manually: grab whole frame then copy sub
self.camcap = cv.CaptureFromCAM(0)
else:
# Make camcap the list of image files in the dir given in imgs
# fr08096_20130206_170005p540540.png
pat="fr\d\d\d\d\d_.*\.png$"
self.camcap=list()
for root, dirs, files in os.walk(self.imgs):
for f in files:
if re.search(pat,f):
self.camcap.append(os.path.join(root,f))
# Keep following code snippet in case we want frame loading speed test function.
if 0:
now=time.time()
im=cv2.imread(fullfile)
done=time.time()
print "%.3f FPS reading %s with cv2 imread" % (1.0/(done-now),fullfile)
now=time.time()
im=cv2.cv.LoadImage(fullfile)
done=time.time()
print "%.3f FPS reading %s with cv LoadImage" % (1.0/(done-now),fullfile)
print "Found %d images of type frXXXXX_XX_XXpXX.png in dir %s" % (len(self.camcap),self.imgs)
self.currfr = 0
# Need to set ROI size to that of these images:
testfr=self.getGrayImage()
self.roipt1 = (1, 1)
self.roipt2 = (testfr.width+1,testfr.height+1)
self.roirect = (self.roipt1[0],self.roipt1[1],self.roipt2[0]-self.roipt1[0],self.roipt2[1]-self.roipt1[1])
self.currfr = 0
def makeROIimage(self):
# Uses ROI pts to allocate roiimage:
self.roisize = (self.roipt2[0]-self.roipt1[0],self.roipt2[1]-self.roipt1[1])
self.roirect = (self.roipt1[0],self.roipt1[1],self.roipt2[0]-self.roipt1[0],self.roipt2[1]-self.roipt1[1])
self.roiimg = cv.CreateImage(self.roisize, 8, 1)
if self.debug:
print "roipt1",self.roipt1
print "roipt2",self.roipt2
print "roisize",self.roisize
print "roimg getsize",cv.GetSize(self.roiimg)
def getGrayImage(self):
if type(self.camcap) is list:
if self.currfr == (len(self.camcap)-1):
self.currfr = 0
#print "Loading frame %s" % self.camcap[self.currfr]
newfr = cv.LoadImage(self.camcap[self.currfr])
self.grabstop=time.time()
self.currfr=self.currfr+1
elif type(self.camcap) is not None:
if self.useci:
if self.verbose:
print "Processing time: %f\n" % (time.time()-self.grabstop)
self.grabstart=time.time()
newfr = cv.fromarray(np.asarray(self.camcap.grab_next_frame_blocking()))
self.grabstop=time.time()
if self.verbose:
print "Grab time: %f\n" % (self.grabstop-self.grabstart)
else:
newfr = cv.QueryFrame(self.camcap)
self.grabstop=time.time()
else:
print "ERROR! No valid camera capture object or directory of images found!"
if self.fliplr:
cv.Flip( newfr, None, 1 );
if newfr.channels is not 1:
newbgfr = cv.CreateImage(cv.GetSize(newfr),8,1)
cv.CvtColor(newfr,newbgfr,cv.CV_RGB2GRAY)
if self.invert:
cv.SubRS(newbgfr,cv.Scalar(255),newbgfr)
return newbgfr
else:
if self.invert:
cv.SubRS(newfr,cv.Scalar(255),newfr)
return newfr
def setupKalman(self):
# Setup Kalman:
A = [ [1, 1], [0, 1] ]
# CreateKalman(dynam_params, measure_params, control_params=0)
#Allocates the Kalman filter structure.
#Parameters:
# dynam_params (int) dimensionality of the state vector
# measure_params (int) dimensionality of the measurement vector
# control_params (int) dimensionality of the control vector
# state: 4D: x,y,vx,vy measure: 2D: x,y
self.kalman = cv.CreateKalman(4, 2, 0)
self.kalmanstate = cv.CreateMat(4, 1, cv.CV_32FC1) # (x,y,vx,vy)
# Not currently used -> this is for generated data
self.process_noise = cv.CreateMat(2, 1, cv.CV_32FC1)
self.measurement = cv.CreateMat(2, 1, cv.CV_32FC1)
self.prediction = cv.CreateMat(4, 1, cv.CV_32FC1)
# Not currently used -> range for random generated
rng = cv.RNG(-1)
code = -1L
cv.Zero(self.measurement)
# Fills with normal values mean zero sigma 0.1. Uses rng random generator object with state
cv.RandArr(rng, self.kalmanstate, cv.CV_RAND_NORMAL, cv.RealScalar(0), cv.RealScalar(0.1))
# What does our matrix look like? 4x4, x = x0 + vx dt
# [1 0 dt 0][x]
# [0 1 0 dt][y]
# [0 0 1 0][vx]
# [0 0 0 1][vy]
# [row, col]
tm = np.array([[ 1., 0., 1., 0.,],\
[ 0., 1., 0., 1.,],\
[ 0., 0., 1., 0.,],\
[ 0., 0., 0., 1.]])
# The following assignment doens't work. Setting each element seems ridiculous -- but can't find
# workaround. Migrating to cv2 in the future should help.
#self.kalman.transition_matrix=cv.fromarray(tm)
self.kalman.transition_matrix[0,0] = 1
self.kalman.transition_matrix[0,1] = 0
self.kalman.transition_matrix[0,2] = 1 # pixels/frame
self.kalman.transition_matrix[0,3] = 0
self.kalman.transition_matrix[1,0] = 0
self.kalman.transition_matrix[1,1] = 1
self.kalman.transition_matrix[1,2] = 0
self.kalman.transition_matrix[1,3] = 1
self.kalman.transition_matrix[2,0] = 0
self.kalman.transition_matrix[2,1] = 0
self.kalman.transition_matrix[2,2] = 1
self.kalman.transition_matrix[2,3] = 0
self.kalman.transition_matrix[3,0] = 0
self.kalman.transition_matrix[3,1] = 0
self.kalman.transition_matrix[3,2] = 0
self.kalman.transition_matrix[3,3] = 1
#print np.asarray(self.kalman.transition_matrix)
# Measurement matrix - we only see positions:
# MxN X NxJ
# [1 0 0 0][x]
# [0 1 0 0][y]
# [vx]
# [vy]
self.kalman.measurement_matrix[0,0] = 1
self.kalman.measurement_matrix[0,1] = 0
self.kalman.measurement_matrix[0,2] = 0
self.kalman.measurement_matrix[0,3] = 0
self.kalman.measurement_matrix[1,0] = 0
self.kalman.measurement_matrix[1,1] = 1
self.kalman.measurement_matrix[1,2] = 0
self.kalman.measurement_matrix[1,3] = 0
#print np.asarray(self.kalman.transition_matrix)
# Process noise incorporates animal dynamics.
# Measurement noise may be interpreted as just that: 0.1 pixels for x, y. This could be higher.
# Value here are 30 Hz, default starting point for tracker:
cv.SetIdentity(self.kalman.process_noise_cov, cv.RealScalar(5e-2))
#print np.asarray(self.kalman.process_noise_cov)
cv.SetIdentity(self.kalman.measurement_noise_cov, cv.RealScalar(1e-1))
#print np.asarray(self.kalman.measurement_noise_cov)
cv.SetIdentity(self.kalman.error_cov_post, cv.RealScalar(1))
#print np.asarray(self.kalman.error_cov_post)
cv.Set(self.kalman.state_pre,cv.Scalar(0))
cv.RandArr(rng, self.kalman.state_post, cv.CV_RAND_NORMAL, cv.RealScalar(0), cv.RealScalar(0.1))
#print np.asarray(self.kalman.state_post)
def setUpdateRates(self):
if self.useci:
if self.guion:
self.vidrate = self.updaterates["guion"]["video"]
self.serialrate = self.updaterates["guion"]["serial"]
if self.usekalman:
# Set Kalman gains for 30 Hz:
cv.SetIdentity(self.kalman.process_noise_cov, cv.RealScalar(5e-2))
cv.SetIdentity(self.kalman.measurement_noise_cov, cv.RealScalar(1e-1))
else:
self.vidrate = self.updaterates["guioff"]["video"]
self.serialrate = self.updaterates["guioff"]["serial"]
if self.usekalman:
# Set Kalman gains for 150 Hz:
# 21-02-2013: These values too fast at 150 Hz, need more smoothing. Factor five decrease in proc noise to 1e-3?
# 21-02-2013: Above was better, could still be smoother. Another factor of two.
cv.SetIdentity(self.kalman.process_noise_cov, cv.RealScalar(5e-4))
cv.SetIdentity(self.kalman.measurement_noise_cov, cv.RealScalar(1e-1))
else:
self.vidrate = self.updaterates["noci"]["video"]
self.serialrate = self.updaterates["noci"]["serial"]
if self.usekalman:
cv.SetIdentity(self.kalman.process_noise_cov, cv.RealScalar(1e-1))
cv.SetIdentity(self.kalman.measurement_noise_cov, cv.RealScalar(1e-1))
# We multiply by this, e.g. 15.0/30.0 factor, to slow feedback.
# Feedback gains tuned for 15 Hz update rate, base future rates off of this.
self.rateadjust = 15.0 / self.serialrate
def storeLengthCal(self):
self.tmillwidth = float(self.keybuf)
sys.stderr.write('\nSetting treadmill width calibration to %f meters\n' % self.tmillwidth )
# Save new length calibration
d = shelve.open("shelf")
d["tmillwidth"] = self.tmillwidth
d.close()
def storeArea(self):
self.area = float(self.keybuf)
sys.stderr.write('\nSetting animal ellipse target area to %f\n' % self.area )
def storeThresh(self):
self.thresh = float(self.keybuf)
sys.stderr.write('\nSetting threshold for countour detection to %d\n' % self.thresh )
# Save new threshold
d = shelve.open("shelf")
d["thresh"] = self.thresh
d.close()
def storeAnimalNum(self):
self.animalnum = int(self.keybuf)
sys.stderr.write('\nSetting animal number to %d\n' % self.animalnum )
def storeTrialNum(self):
self.trialnum = int(self.keybuf)
sys.stderr.write('\nSetting trial number to %d\n' % self.trialnum )
def storeWeight(self):
self.weight = int(self.keybuf)
sys.stderr.write('\nSetting weight to %3.3f\n' % self.weight )
def storeNotes(self):
self.notes = str(self.keybuf)
sys.stderr.write('\nSetting notes to %s\n' % self.notes )
def storeLeftBrkSpd(self):
self.lbrk_spd = int(self.keybuf)
sys.stderr.write('\nSetting left bracket key speed to %d, and saving settings.\n' % self.lbrk_spd )
d = shelve.open("shelf")
d["lbrk_spd"] = self.lbrk_spd
d.close()
def storeRightBrkSpd(self):
self.rbrk_spd = int(self.keybuf)
sys.stderr.write('\nSetting right bracket key speed to %d, and saving this setting.\n' % self.rbrk_spd )
d = shelve.open("shelf")
d["rbrk_spd"] = self.rbrk_spd
d.close()
def makeTimeStampStr(self,epochtime):
return time.strftime("%Y%m%d_%H%M%Sp",time.localtime(epochtime)) + ("%0.6f" % (epochtime % 1))[2:]
def getLengthCal(self):
sys.stderr.write('Old treadmill width: %f meters\n' % self.tmillwidth )
sys.stderr.write("New width of treadmill belt (ROI) in meters:")
self.keycallback = self.storeLengthCal
self.keybuf = ""
self.keyinput = True
def getThresh(self):
sys.stderr.write('Old threshold: %d\n' % self.thresh )
sys.stderr.write("New threshold (0-255; default 70 for black mice on white bg): ")
self.keycallback = self.storeThresh
self.keybuf = ""
self.keyinput = True
def getanimalnum(self):
sys.stderr.write('Old animal num: %d\n' % self.animalnum )
sys.stderr.write("Enter new animal ID number:")
self.keycallback = self.storeAnimalNum
self.keybuf = ""
self.keyinput = True
def getTrialNum(self):
sys.stderr.write('Old trial num: %d\n' % self.trialnum )
sys.stderr.write("Enter new trial number:")
self.keycallback = self.storeTrialNum
self.keybuf = ""
self.keyinput = True
def getWeight(self):
sys.stderr.write("Enter new weight:")
self.keycallback = self.storeWeight
self.keybuf = ""
self.keyinput = True
def getNotes(self):
sys.stderr.write('Old notes: %s\n' % self.notes )
sys.stderr.write("Enter new trial notes: ")
self.keycallback = self.storeNotes
self.keybuf = ""
self.keyinput = True
def getLbrkSpd(self):
sys.stderr.write('Old left bracket speed (cm/s): %d\n' % self.lbrk_spd )
sys.stderr.write("Enter new speed setting for left bracket key: ")
self.keycallback = self.storeLeftBrkSpd
self.keybuf = ""
self.keyinput = True
def getRbrkSpd(self):
sys.stderr.write('Old right bracket speed (cm/s): %d\n' % self.rbrk_spd )
sys.stderr.write("Enter new speed setting for right bracket key: ")
self.keycallback = self.storeRightBrkSpd
self.keybuf = ""
self.keyinput = True
def getInitial(self):
self.animalnum = int(raw_input('Enter animal number: '))
self.weight = float(raw_input('Enter weight: '))
self.trialnum = int(raw_input('Enter trial number: '))
self.notes = raw_input('Enter notes for trial: ')
def handleInput(self,c):
if self.verbose:
sys.stderr.write('Got char: ordinal %d\n' % ord(c) )
if c == '\x1b': # x1b is ESC - Always check this first for quick exit
self.alive = False
elif self.keyinput == True:
if c == '\x0A': # This is ordinal 10 = Line Feed. Get these from terminal carriage return input.
# Then call the function stored in self.keycallback to process the buffer
self.keycallback()
self.keyinput = False
self.keybuf = ""
else:
# Store chars in a buffer until return
sys.stderr.write( c )
self.keybuf = self.keybuf + c
elif c == 's': # Display settings
sys.stderr.write(self.getSettings())
elif c == 'C': # Get length calibration.
self.getLengthCal()
elif c == 'n': # Get animal number
self.getanimalnum()
elif c == 'T': # Input trial number for this trial
self.getTrialNum()
elif c == 'w': # Input weight for this trial
self.getWeight()
elif c == 'N': # Input notes for this trial
self.getNotes()
elif c =='{':
if self.state=="idle":
self.getLbrkSpd()
elif c =='}':
if self.state=="idle":
self.getRbrkSpd()
elif c == 'a': # New animal ellipse area:
sys.stderr.write('Old animal ellipse area: %f\n' % self.area )
sys.stderr.write("New ellipse area:")
self.keycallback = self.storeArea
self.keybuf = ""
self.keyinput = True
elif c == 'H': # Threshold:
if self.state=="idle":
self.getThresh()
else:
sys.stderr.write("Can only set threshold in idle mode")
elif c == 'M': # Measure new animal area:
if self.state == 'preview':
self.measureinput = not self.measureinput
else:
sys.stderr.write(' CAN ONLY MEASURE IN PREVIEW MODE: hit p\n' )
elif c == 'q': # also quit on q if not in keyboard input mode:
self.alive = False
elif c == 'h':
sys.stderr.write(self.get_help_text())
elif c == 'v':
self.verbose = not self.verbose
if self.haveserial:
self.tsinter.verbose = not self.tsinter.verbose
if self.verbose:
sys.stderr.write('Verbosity: ON\n' )
else:
sys.stderr.write('Verbosity: OFF\n' )
elif c == 'g':
self.guion = not self.guion
self.setUpdateRates()
elif c == 'p':
# Make p toggle the preview - can only go into from idle
if self.state == 'idle':
try:
if self.camcap is None:
self.openCam()
# Not in useci mode, will grab full frame
self.prevfr = self.getGrayImage()
# Overlay current ROI
cv.Rectangle(self.prevfr, (self.roipt1[0], self.roipt1[1]), (self.roipt2[0],self.roipt2[1]), cv.Scalar(0, 0, 255, 0), 2, 8, 0)
cv.NamedWindow(self.prevwn, cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage(self.prevwn,self.prevfr)
cv.SetMouseCallback(self.prevwn, self.mouseHandler, None)
self.cvwindow = True
self.state = 'preview'
except:
sys.stderr.write('Unable to open camera with cv.CaptureFromCAM(0).\n')
elif self.state == 'preview':
# Close down the preview
# Switch back to idle
cv.DestroyWindow(self.prevwn)
self.cvwindow = False
# Give opencv a few chances to close the window
for k in range(10):
c = cv.WaitKey(10)
del(self.camcap)
self.camcap = None
self.state = "idle"
elif c == 'u':
if self.verbose:
sys.stderr.write('Updating preview\n')
try:
self.prevfr = self.getGrayImage()
# Overlay current ROI
cv.Rectangle(self.prevfr, (self.roipt1[0], self.roipt1[1]), (self.roipt2[0],self.roipt2[1]), cv.Scalar(0, 0, 255, 0), 2, 8, 0)
cv.ShowImage(self.prevwn,self.prevfr)
except:
std.stderr.write('Unable to get new frame.\n')
elif c == 'o':
if self.state == 'preview':
self.roiinput = not self.roiinput
elif c == 'd':
# First check if we are given image sequence input, cannot display as only have ROIs
if self.imgs != "":
print "Cannot go into display mode given image sequence, only have ROIs"
return
# if we are in idle go into display
# if we are in other modes, ignore
if self.state == "idle":
self.makeROIimage()
if self.camcap is None:
self.openCam()
# Not in useci mode, will grab full frame
self.prevfr = self.getGrayImage()
cv.Copy(cv.GetSubRect(self.prevfr,self.roirect),self.roiimg)
print "Got Image"
cv.NamedWindow(self.prevwn, cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage(self.prevwn,self.prevfr)
self.cvwindow = True
self.state = "display"
elif self.state == "display":
# Switch back to idle
cv.DestroyWindow(self.prevwn)
self.cvwindow = False
# Give opencv a few chances to close the window
for k in range(10):
c = cv.WaitKey(10)
del(self.camcap)
self.camcap = None
self.state = "idle"
elif c == 't':
# Go into tracking mode
if self.state == "idle":
# Difference order from display!
if self.camcap is None:
self.openCam() # This also adjusts ROI if in files mode
self.makeROIimage()
self.filtimg = cv.CreateImage(self.roisize, 8, 1)
self.storage = cv.CreateMemStorage(0)
#print cv.GetSize(self.roiimg)
#print self.roirect
# Frame should be roi size, copy to cv image:
self.prevfr = self.getGrayImage()
#print self.camcap.get_frame_roi()
print "Prevfr size:"
print cv.GetSize(self.prevfr)
print "ROI Size:"
print cv.GetSize(self.roiimg)
if self.imgs != "":
cv.Copy(self.prevfr,self.roiimg)
else:
cv.Copy(cv.GetSubRect(self.prevfr,self.roirect),self.roiimg)
cv.NamedWindow(self.prevwn, cv.CV_WINDOW_AUTOSIZE)
self.cvwindow = True
cv.ShowImage(self.prevwn,self.roiimg)
self.trackstarttime = time.time()
self.lasttrack=-np.inf
self.state = "tracking"
if sys.platform == 'darwin':
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
elif self.state == "tracking":
# Switch back to idle
cv.DestroyWindow(self.prevwn)
self.cvwindow = False
# Give opencv a few chances to close the window
for k in range(10):
c = cv.WaitKey(10)
del(self.camcap)
self.camcap = None
self.state = "idle"
if self.logging:
self.stopLogging()
self.logging = False
elif c == 'f':
# Go into feedback control mode
if self.state == "idle":
# Get tracking setup
self.makeROIimage()
self.filtimg = cv.CreateImage(self.roisize, 8, 1)
self.storage = cv.CreateMemStorage(0)
# Start camera
if self.camcap is None:
self.openCam()
# Setup windows, grab and display
cv.NamedWindow(self.prevwn, cv.CV_WINDOW_AUTOSIZE)
self.cvwindow = True
self.prevfr = self.getGrayImage()
cv.Copy(cv.GetSubRect(self.prevfr,self.roirect),self.roiimg)
cv.ShowImage(self.prevwn,self.roiimg)
# Setup serial
if self.haveserial:
# Get control of belt:
self.tcontrol = self.tsinter.tcontrol(True,self.verbose)
time.sleep(0.2)
if not self.tcontrol:
print "UNABLE TO GET CONTROL OF TMILL"
# Set speed to zero, then run:
self.tsinter.setspd(0,self.verbose)
time.sleep(0.2)
self.tmspd = 0
self.tsinter.runstop(True,self.verbose)
time.sleep(0.2)
self.trunning = True
self.trackstarttime = time.time()
self.lastfeedback = -np.inf
self.lastserial=-np.inf
self.state = "feedback"
sys.stderr.write("Feedback mode ON\n")
elif self.state == "feedback":
# Stop the treadmill
if self.haveserial:
self.tsinter.runstop(False,self.verbose)
self.tsinter.setspd(0,self.verbose)
# Switch back to idle
cv.DestroyWindow(self.prevwn)
self.cvwindow = False
# Give opencv a few chances to close the window
for k in range(10):
c = cv.WaitKey(10)
del(self.camcap)
self.camcap = None
self.state = "idle"
sys.stderr.write("Feedback mode OFF\n")
# Shut down vidwriter if on
if self.vidwriting:
del(self.vidwriter)
self.vidwriter = None
self.vidwriting = False
if self.logging:
self.stopLogging()
self.logging = False
sys.stderr.write("Logging mode OFF\n")
elif c == 'b':
if self.state == "feedback" or self.state == "tracking":
self.behavtrig = not self.behavtrig
print "Behavioural trigger set to %s." % self.behavtrig
else:
print "Cant turn off or off behavioural trigger except in feedback or tracking mode."
elif c == '\x0D' or c == '\x0A': # Carriage return = 0x0D = 13: note that these come from OpenCV window on hitting enter, whereasa 0x0A == 10 comes from terminal enter. User may hit enter into either.
# If we are in tracking mode, and user hits carriage return, save X secs of high speed video
if self.state == "tracking" or self.state == "feedback":
#send packet to trigger camera to capture last capsecs seconds
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
now = time.time()
print "Sending request to HSV machine store %d seconds up to present time." % (self.capsecs)
s.connect((self.host, self.port))
s.send("%.6f %.6f" % (now-self.capsecs,now) )
except:
print "Error connecting"
finally:
print 'closing socket!'
s.close()
else:
print "<enter> stores high speed video in tracking mode only"
elif c == 'i':
if not self.invert:
self.invert = True
print "Invert image set to True"
else:
self.invert = False
print "Invert image set to False"
elif c == 'P':
if self.state == "feedback":
self.perturb = not self.perturb
print "PERTURBATION set to %s." % self.perturb
else:
print "Cant turn off or off PERTURBATION mode except in feedback mode."
elif c == 'e':
# send packet to tell hsv camera code to exit
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((self.host, self.port))
s.send("exit")
data = s.recv(1024)
print 'Received', repr(data)
except:
print "Error connecting"
finally:
s.close()
# =========== NEW CODE HERE ===========
elif c == 'm':
# send packet to tell hsv camera code to toggle streaming to disk
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((self.host, self.port))
s.send("togglestreaming")
data = s.recv(1024)
print 'Received', repr(data)
except:
print "Error connecting"
finally:
s.close()
elif c == 'V':
if self.state == "feedback":
if not self.vidwriting:
# Try IYUV or I420 for uncompressed in AVI container
# CreateVideoWriter(filename, fourcc, fps, frame_size [, is_color])
# Sort file name + placement as below
self.vidwriter = cv.CreateVideoWriter('vid.avi', cv.CV_FOURCC('I','4','2','0'), self.vidrate, self.roisize)
self.vidwriting = True
else:
del(self.vidwriter)
self.vidwriter = None
self.vidwriting = False
elif c == 'F':
if not self.framewriting:
self.framedir = "data/raw/fr" + self.makeTimeStampStr(self.trackstarttime)
if not os.path.exists(self.framedir):
os.makedirs(self.framedir)
self.framewriting = True
else:
self.framewriting = False
elif c == 'O':
self.overlay = not self.overlay
elif c == 'l':
if self.logging:
self.stopLogging()
else:
self.startLogging()
elif c == 'c':
if self.tcontrol:
self.tsinter.tcontrol(False,self.verbose)
self.tcontrol = False