-
Notifications
You must be signed in to change notification settings - Fork 0
/
openfield.py
887 lines (705 loc) · 31 KB
/
openfield.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
#! /usr/bin/env python
from Tkinter import *
from math import *
import sys
import os, thread, datetime, string
import PIL
from PIL import Image, ImageTk
#from ctypes_opencv import *
import os
import tkFileDialog
#import tkMessageBox
print("Open Field Analyzer - Michael Kaufman 2010")
numpixels = 0 #Number of Pixels
#calc thigmotaxia
#340 pix per 100 cm
#3.4 pix per cm
#how many cm square box do you want?
thig_sq_boxcm = 85
thig_sq_box = thig_sq_boxcm * 3.4
#MOVEMENT/DISTANCE SENSITIVTY In Pix
maxmove = 5 * 3.4
minmove = 1 * 3.4
capture = 0
current_frame = 0
def menu_saveconfig():
pass
def menu_loadconfig():
pass
def menu_load_files():
bf_load_files()
def bf_mansetrect():
global grabbing_points
global got_firstpoint
grabbing_points = True
got_firstpoint = False
def bf_mansetcircle():
pass
def refresh_canvas():
global can
#load image -> send to canvas -> view
#load trial properties
#draw roi/apparatus image
itemtemp = lb_mainlist.get(lb_mainlist.curselection())
f = open(itemtemp[1], 'r')
infoline = f.readline()
infolist = infoline.split(',')
f.close
print itemtemp
print itemtemp[1].replace('.a2p','.jpg')
print infolist
#0-avifile location 1-frames 2-fps 3-imagewidth 4-imageheight 5-length in seconds 6-apparatus roi coords
resolution = infolist[3] + "x" + infolist[4]
l_trial_name2.config(text=resolution)
l_trial_fps2.config(text=infolist[2])
l_trial_length2.config(text=infolist[5])
#Load image and display
imagefile = itemtemp[1].replace('.a2p','.jpg')
image = Image.open(imagefile)
photo = ImageTk.PhotoImage(image)
can.create_image(320, 240, image=photo)
can.image=photo
def bf_preview_settings1():
print lb_mainlist.curselection()
#CLEAR CANVAS AND REDRAW IMAGE
try: item = lb_mainlist.get(lb_mainlist.curselection())
except TclError:
item = lb_mainlist.get(0)
lb_mainlist.selection_set(0)
refresh_canvas() #CLEAR CANVAS AND REDRAW IMAGE
#get roi
f = open(item[1], 'r')
#load file, settings, xy points
infoline = f.readline()
infolist = infoline.split(',')
infolist[6] = infolist[6].strip('(')
infolist[9] = infolist[9].strip(')\n')
#print rectx, recty, rectx2, recty2
print infolist[6]
#convert to int
rectx = int(infolist[6]) - 7
recty = int(infolist[7]) - 1
rectx2 = int(infolist[8]) + 3
recty2 = int(infolist[9]) + 3
#rectx2 = rectx2 - rectx #convert to rectangle
#recty2 = recty2 - recty
print rectx, recty, rectx2, recty2
can.create_rectangle(rectx,recty,rectx2,recty2, outline='red')
#calculate center
xcenter = (rectx + rectx2)/2
ycenter = (recty + recty2)/2
can.create_oval(xcenter, ycenter, xcenter+3, ycenter+3, fill="yellow")
if checkbox_boxsize.get() == 1:
print "Box Size is checked"
boxsize = en_boxsize.get()
boxpixsize = float(boxsize) * 3.4
topleftx = xcenter - boxpixsize *.5
toplefty = ycenter - boxpixsize *.5
botrightx = xcenter + boxpixsize *.5
botrighty = ycenter + boxpixsize *.5
can.create_rectangle(topleftx, toplefty, botrightx, botrighty, outline='blue')
if checkbox_grid.get() == 1:
print "Grid is selected"
en_grid.get()
if checkbox_manual.get() == 1:
can.create_rectangle(manual_firstx, manual_firsty, manual_lastx, manual_lasty, outline="purple")
def bf_preview_settings():
print lb_mainlist.curselection()
time_to_analyze = en_seconds_to_analyze.get()
#CLEAR CANVAS AND REDRAW IMAGE
try: item = lb_mainlist.get(lb_mainlist.curselection())
except TclError:
item = lb_mainlist.get(0)
lb_mainlist.selection_set(0)
refresh_canvas() #CLEAR CANVAS AND REDRAW IMAGE
#get roi
f = open(item[1], 'r')
#load file, settings, xy points
infoline = f.readline()
infolist = infoline.split(',')
infolist[6] = infolist[6].strip('(')
infolist[9] = infolist[9].strip(')\n')
print "INFOLIST", infolist
#save trial name to file
trialname = infolist[0].split('/')
run_filename = trialname.pop()
#get FPS, calculate frames to analyze
frames_to_analyze = int(time_to_analyze) * int(round(float(infolist[2])))
print "FRAMES TO ANALYZE: ", frames_to_analyze, " ", int(round(float(infolist[2])))
#get ROI from info list
#roi_apparatus = eval(infolist[6])
xypoints = f.readlines() #create xy point coords to analyze
f.close()
first_run = True
lastx = 0
lasty = 0
distancetraveled = 0
#print rectx, recty, rectx2, recty2
print infolist[6]
#convert to int
rectx = int(infolist[6]) - 7
recty = int(infolist[7]) - 1
rectx2 = int(infolist[8]) + 3
recty2 = int(infolist[9]) + 3
#rectx2 = rectx2 - rectx #convert to rectangle
#recty2 = recty2 - recty
print rectx, recty, rectx2, recty2
can.create_rectangle(rectx,recty,rectx2,recty2, outline='red')
#calculate center
xcenter = (rectx + rectx2)/2
ycenter = (recty + recty2)/2
can.create_oval(xcenter, ycenter, xcenter+3, ycenter+3, fill="yellow")
can.create_text(320,10, text=run_filename, fill="green")
if checkbox_boxsize.get() == 1:
print "Box Size is checked"
boxsize = en_boxsize.get()
boxpixsize = float(boxsize) * 3.4
topleftx = xcenter - boxpixsize *.5
toplefty = ycenter - boxpixsize *.5
botrightx = xcenter + boxpixsize *.5
botrighty = ycenter + boxpixsize *.5
can.create_rectangle(topleftx, toplefty, botrightx, botrighty, outline='blue')
if checkbox_grid.get() == 1:
print "Grid is selected"
en_grid.get()
if checkbox_manual.get() == 1:
can.create_rectangle(manual_firstx, manual_firsty, manual_lastx, manual_lasty, outline="purple")
first_run = True
lastx = 0
lasty = 0
distancetraveled = 0
#calculate distance traveled
for each in xypoints:
frame_xypoints = []
frame_xypoints_unformated = each.split(',')
for item in frame_xypoints_unformated:
frame_xypoints.append(int(item.strip("\n")))
print "FRAME XY POINTS", frame_xypoints
if frame_xypoints[0] > frames_to_analyze:
break #end analysis
if first_run == True:
print "FIRST RUN"
first_run = False # First point recieved
lastx = frame_xypoints[1] + rectx
lasty = frame_xypoints[2] + recty
else:
thisx = frame_xypoints[1] + rectx
thisy = frame_xypoints[2] + recty
xpoints = abs(thisx - lastx)
ypoints = abs(thisy - lasty)
thisdistance = sqrt(pow(xpoints, 2) + pow(ypoints, 2))
print "THIS DISTANCE: ", thisdistance
if thisdistance > 30 or thisdistance < 5:
thisx = lastx #do nothing
thisy = lasty
print "Doing nothing"
## elif thisx < roix1 or thisx > roix2 or thisy < roiy1 or thisy > roiy2:
## thisx = lastx
## thisy = lasty
else:
print "ELSE FIRED!!!!"
distancetraveled += thisdistance
print "DISTANCE SO FAR: ", distancetraveled
lastx = thisx
lasty = thisy
can.create_oval(thisx, thisy, thisx+1, thisy+1, fill="red")
def bf_load_files():
filestr = tkFileDialog.askopenfilenames(title='Choose a file', filetypes = [('a2p files', '.a2p')])
filestrtemplist = filestr.split(' {')
print filestrtemplist
for each in filestrtemplist:
each = each.strip('{')
each = each.strip('}')
splittemp = each.rsplit('/', 1)
filename = splittemp[1]
lb_mainlist.insert(END, (filename, each))
if lb_mainlist.size() > 0:
lb_mainlist.selection_set(0)
itemtemp = lb_mainlist.get(lb_mainlist.curselection())
imagefile = itemtemp[1].replace('.a2p','.jpg')
image = Image.open(imagefile)
photo = ImageTk.PhotoImage(image)
can.create_image(320, 240, image=photo)
can.image=photo
def bf_analyzer_threadstarter():
thread.start_new_thread(bf_analyzer, ("Analysis Thread",1))
def f_output_writer(savefile_str, run_database):
f = open(savefile_str, 'w') #create save file
print run_database
reporttitle = "MLK OpenField Analyzer Report"
writebuffer = "<HTML>\n<TITLE>" + reporttitle + "</TITLE>\n"
f.write(writebuffer)
writebuffer = "<h1>" + "OpenField Analyzer Results on " + str(datetime.datetime.now()) + "\n" + "</h1>\n\n"
f.write(writebuffer)
#writebuffer = "This file has been auto-generated using the Maze Track Analyzer Program on " #+ str(time.ctime()) + "\n"
#f.write(writebuffer)
f.write("<center>")
write_buffer = "\nSettings Used:\n"
f.write(write_buffer)
write_buffer = "Time Analyzed (secs): " + en_seconds_to_analyze.get() + "\n"
f.write(write_buffer)
if checkbox_boxsize.get() == 1:
write_buffer = "Thigmotaxia Box(cm2): " + en_boxsize.get() + "\n"
f.write(write_buffer)
if checkbox_grid.get() == 1:
write_buffer = "Grid (Selection): " + en.grid.get() + "\n"
f.write(write_buffer)
if checkbox_manual.get() == 1:
write_buffer = "Manual Zone(cm2): " + "User Defined" + "\n"
f.write(write_buffer)
write_buffer = "\n"
f.write(write_buffer)
writebuffer = "\n<H1>Trial Summary:</H1>\n"
f.write(writebuffer)
writebuffer = '<table border="1"> \n <tr>' #Start of table
f.write(writebuffer)
#Column Headings
f.write("<td></td>")
f.write("<td><B>Distance Traveled (cm)</B></td>")
f.write("<td><B>Thigmotaxia Entries (#)</B></td>")
f.write("<td><B>Thigmotaxia Exits (#)</B></td>")
f.write("<td><B>Thigmotaxia Crosses (#)</B></td>")
f.write("<td><B>Thigmotaxia Time Within (s)</B></td>")
f.write("<td><B>Thigmotaxia Time Outside (s)</B></td>")
f.write("<td><B>Grid Crosses (#)</B></td>")
f.write("<td><B>User Entries (#)</B></td>")
f.write("<td><B>User Exits (#)</B></td>")
f.write("<td><B>User Crosses (#)</B></td>")
f.write("<td><B>User Time Within (s)</B></td>")
f.write("<td><B>User Time Outside (s)</B></td></tr>")
for trial_run in run_database:
#trial_run = run_database.pop(0)
print trial_run
f.write("<tr>") #Start New Row
writebuffer = "<td><b><center>" + str(trial_run[0]) + "</center></b></td>" #Trial
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[1]) + "</center></b></td>" #dist traveled
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[2]) + "</center></b></td>" #thig ents
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[3]) + "</center></b></td>" #thig exits
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[4]) + "</center></b></td>" #thig crosses
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[5]) + "</center></b></td>" #thig timein
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[6]) + "</center></b></td>" #thig timemout
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[7]) + "</center></b></td>" #grid crosses
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[8]) + "</center></b></td>" #man ents
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[9]) + "</center></b></td>" #man exits
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[10]) + "</center></b></td>" #man crosses
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[11]) + "</center></b></td>" #man time in
f.write(writebuffer)
writebuffer = "<td><b><center>" + str(trial_run[12]) + "</center></b></td>" #man time out
f.write(writebuffer)
f.write("</table>\n") #End of table
f.write("</center></HTML>")
f.close()
def bf_analyzer():
savefile_str = tkFileDialog.asksaveasfilename(title='Name Save File', filetypes = [('html file', '.htm')], defaultextension=".htm")
items_to_process = lb_mainlist.get(0, END)
time_to_analyze = en_seconds_to_analyze.get()
print "TIME to ANALYZE: ", time_to_analyze
processing_filenum = 0
run_database = [] #initialize run database for storing of run variables
for item in items_to_process: #process each file
run_filename = 0
dist_traveled = 0
thig_ents = 0
thig_exits = 0
thig_crosses = 0
thig_timein = 0
thig_timeout = 0
grid_crosses = 0
man_ents = 0
man_exits = 0
man_crosses = 0
man_timein = 0
man_timeout = 0
processing_filenum+=1
l_progress.config(text="Processing: File " + str(processing_filenum) + " / " + str(lb_mainlist.size()), background="yellow")
f = open(item[1], 'r')
#load file, settings, xy points
infoline = f.readline()
infolist = infoline.split(',')
print "INFOLIST", infolist
#save trial name to file
trialname = infolist[0].split('/')
run_filename = trialname.pop()
infolist[6] = infolist[6].strip('(')
infolist[9] = infolist[9].strip(')\n')
rectx = int(infolist[6]) - 7
recty = int(infolist[7]) - 1
rectx2 = int(infolist[8]) + 3
recty2 = int(infolist[9]) + 3
#get FPS, calculate frames to analyze
frames_to_analyze = int(time_to_analyze) * int(round(float(infolist[2])))
print "FRAMES TO ANALYZE: ", frames_to_analyze, " ", int(round(float(infolist[2])))
#get ROI from info list
#roi_apparatus = eval(infolist[6])
xypoints = f.readlines() #create xy point coords to analyze
f.close()
first_run = True
lastx = 0
lasty = 0
distancetraveled = 0
#calculate distance traveled
for each in xypoints:
frame_xypoints = []
frame_xypoints_unformated = each.split(',')
for item in frame_xypoints_unformated:
frame_xypoints.append(int(item.strip("\n")))
print "FRAME XY POINTS", frame_xypoints
if frame_xypoints[0] > frames_to_analyze:
break #end analysis
if first_run == True:
print "FIRST RUN"
first_run = False # First point recieved
lastx = frame_xypoints[1] + rectx
lasty = frame_xypoints[2] + recty
else:
thisx = frame_xypoints[1] + rectx
thisy = frame_xypoints[2] + recty
xpoints = abs(thisx - lastx)
ypoints = abs(thisy - lasty)
thisdistance = sqrt(pow(xpoints, 2) + pow(ypoints, 2))
print "THIS DISTANCE: ", thisdistance
if thisdistance > 30 or thisdistance < 5:
thisx = lastx #do nothing
thisy = lasty
print "Doing nothing"
## elif thisx < roix1 or thisx > roix2 or thisy < roiy1 or thisy > roiy2:
## thisx = lastx
## thisy = lasty
else:
print "ELSE FIRED!!!!"
distancetraveled += thisdistance
print "DISTANCE SO FAR: ", distancetraveled
lastx = thisx
lasty = thisy
dist_traveled = str(distancetraveled/3.4)
if checkbox_boxsize.get() == 1:
boxsize = en_boxsize.get()
infolist[6] = infolist[6].strip('(')
infolist[9] = infolist[9].strip(')\n')
#convert to int
rectx = int(infolist[6]) #- 7
recty = int(infolist[7]) #- 1
rectx2 = int(infolist[8]) #+ 3
recty2 = int(infolist[9]) #+ 3
#calculate center
xcenter = (rectx + rectx2)/2
ycenter = (recty + recty2)/2
boxsize = en_boxsize.get()
boxpixsize = float(boxsize) * 3.4
topleftx = xcenter - boxpixsize *.5
toplefty = ycenter - boxpixsize *.5
botrightx = xcenter + boxpixsize *.5
botrighty = ycenter + boxpixsize *.5
toprightx = botrightx
toprighty = toplefty
botleftx = topleftx
botlefty = botrighty
#convert to format for polytest
box_poly = [(topleftx, toplefty), (toprightx, toprighty), (botrightx, botrighty), (botleftx, botlefty)]
print "BOX POLY", box_poly
counter_entries = 0
counter_exits = 0
#counter_timespent_within = 0
counter_frames_within = 0
#counter_timespent_outside = 0
counter_frames_outside = 0
status_locationlast = False
status_locationcurrent = True
for each in xypoints:
split_each = each.split(',')
split_each[0] = int(split_each[0])
split_each[1] = int(split_each[1]) + rectx
split_each[2] = split_each[2].strip("\n")
split_each[2] = int(split_each[2]) + recty
print "SPLIT EACH", split_each
if split_each[0] > frames_to_analyze: #if reached end of analysis
break #end analysis
if first_run == True:
print "FIRST RUN"
first_run = False # First point recieved
lastx = split_each[1] + rectx
lasty = split_each[2] + recty
else:
thisx = split_each[1] + rectx
thisy = split_each[2] + recty
xpoints = abs(thisx - lastx)
ypoints = abs(thisy - lasty)
#check if x,y point is within box
status_locationcurrent = f_getlocation(split_each[1], split_each[2], box_poly)
print "STATS_LOCATIONCURRENT", status_locationcurrent
#### FIX HERE CHECK FOR DISTANCE TRAVELED
if status_locationcurrent == True and status_locationlast == True:
#within box continuing
counter_frames_within += 1
if status_locationcurrent == True and status_locationlast == False:
#just entered box
counter_frames_within += 1
counter_entries += 1
status_locationlast = True #make true for next round
if status_locationcurrent == False and status_locationlast == True:
#just left box
counter_frames_outside += 1
counter_exits += 1
status_locationlast = False #make true for next round
if status_locationcurrent == False and status_locationlast == False:
#outside box continuing
counter_frames_outside += 1
thig_ents = str(counter_entries)
thig_exits = str(counter_exits)
thig_crosses = str(counter_entries+counter_exits)
time_within = counter_frames_within / float(infolist[2])
thig_timein = str(time_within)
time_outside = counter_frames_outside / float(infolist[2])
thig_timeout = str(time_outside)
if checkbox_grid.get() == 1:
print "Grid is selected"
en_grid.get()
if checkbox_manual.get() == 1:
print "Manual mode"
global manual_firstx
global manual_firsty
global manual_lastx
global manual_lasty
topleftx = manual_firstx
toplefty = manual_firsty
botrightx = manual_lastx
botrighty = manual_lasty
toprightx = botrightx
toprighty = toplefty
botleftx = topleftx
botlefty = botrighty
#convert to format for polytest
box_poly = [(topleftx, toplefty), (toprightx, toprighty), (botrightx, botrighty), (botleftx, botlefty)]
print "BOX POLY", box_poly
counter_entries = 0
counter_exits = 0
#counter_timespent_within = 0
counter_frames_within = 0
#counter_timespent_outside = 0
counter_frames_outside = 0
status_locationlast = False
status_locationcurrent = True
for each in xypoints:
split_each = each.split(',')
split_each[0] = int(split_each[0])
split_each[1] = int(split_each[1])
split_each[2] = split_each[2].strip("\n")
split_each[2] = int(split_each[2])
print "SPLIT EACH", split_each
if split_each[0] > frames_to_analyze:
break #end analysis
#check if x,y point is within box
status_locationcurrent = f_getlocation(split_each[1], split_each[2], box_poly)
print "STATS_LOCATIONCURRENT", status_locationcurrent
if status_locationcurrent == True and status_locationlast == True:
#within box continuing
counter_frames_within += 1
if status_locationcurrent == True and status_locationlast == False:
#just entered box
counter_frames_within += 1
counter_entries += 1
status_locationlast = True #make true for next round
if status_locationcurrent == False and status_locationlast == True:
#just left box
counter_frames_outside += 1
counter_exits += 1
status_locationlast = False #make true for next round
if status_locationcurrent == False and status_locationlast == False:
#outside box continuing
counter_frames_outside += 1
man_ents = str(counter_entries)
man_exits = str(counter_exits)
man_crosses = str(counter_entries+counter_exits)
time_within = counter_frames_within / float(infolist[2])
man_timein = str(time_within)
time_outside = counter_frames_outside / float(infolist[2])
man_timeout = str(time_outside)
run_database.append([run_filename, dist_traveled, thig_ents, thig_exits, thig_crosses, thig_timein, thig_timeout, grid_crosses, man_ents, man_exits, man_crosses, man_timein, man_timeout])
f_output_writer(savefile_str, run_database)
l_progress.config(text="FINISHED!", background="green")
def bf_clearfiles():
lb_mainlist.delete(0,END)
def del_lbitem(self):
lbitemtemp = lb_mainlist.curselection()
itemtodelete = int(lbitemtemp[0])
lb_mainlist.delete(itemtodelete)
def click_viewfile(event):
global can
#load image -> send to canvas -> view
#load trial properties
#draw roi/apparatus image
itemtemp = lb_mainlist.get(lb_mainlist.curselection())
f = open(itemtemp[1], 'r')
infoline = f.readline()
infolist = infoline.split(',')
f.close
print itemtemp
print itemtemp[1].replace('.a2p','.jpg')
print infolist
#0-avifile location 1-frames 2-fps 3-imagewidth 4-imageheight 5-length in seconds 6-apparatus roi coords
resolution = infolist[3] + "x" + infolist[4]
l_trial_name2.config(text=resolution)
l_trial_fps2.config(text=infolist[2])
l_trial_length2.config(text=infolist[5])
#Load image and display
imagefile = itemtemp[1].replace('.a2p','.jpg')
image = Image.open(imagefile)
photo = ImageTk.PhotoImage(image)
can.create_image(320, 240, image=photo)
can.image=photo
def canvas_click(event): # MOUSE 1 (LEFT-CLICK) PLACE POINT
global grabbing_points
global got_firstpoint
global manual_firstx
global manual_firsty
global manual_lastx
global manual_lasty
if grabbing_points == True:
if got_firstpoint == False:
manual_firstx = event.x
manual_firsty = event.y
got_firstpoint = True
else:
manual_lastx = event.x
manual_lasty = event.y
grabbing_points = False
got_firstpoint = False
print "MANUAL CAPTURE:", manual_firstx, manual_firsty, manual_lastx, manual_lasty
can.create_rectangle(manual_firstx, manual_firsty, manual_lastx, manual_lasty, outline="purple")
def f_dispimage_old(frame):
dispframe = frame.as_pil_image()
ApImage = ImageTk.PhotoImage(dispframe)
can.create_image(320, 240, image=ApImage)
def f_dispimage(image):
ApImage = ImageTk.PhotoImage(image)
can.create_image(320, 240, image=ApImage)
def f_getlocation(x, y, poly):
#print poly, "X", x, "Y", y
n = len(poly)
inside = False
p1x, p1y = poly[0]
for i in range(n + 1):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xinters = (y-p1y) * (p2x-p1x) / (p2y-p1y) + p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x, p1y = p2x, p2y
return inside
############ ROOT WINDOW GUI #############
rootwindow = Tk()
rootwindow.title("OpenField Analyzer - Michael Kaufman 2010")
rootwindow.geometry("1000x700+200+0")
rootwindow.wm_state('zoomed')
### CANVAS WINDOW
can = Canvas(rootwindow, width='640', height='480')
can.configure(cursor="crosshair")
can.place(x=0, y=0)
can.bind("<Button-1>", canvas_click)
#photo = PhotoImage(file="test.jpg")
#f_dispimage(photo)
#image = Image.open("test.jpg")
#photo = ImageTk.PhotoImage(image)
#can.create_image(320, 240, image=photo)
can.create_text(320,240, text="Load Files to begin analysis", fill="red")
###GUI FEATURES
menubar = Menu(rootwindow)
### Analyzer Settings
l_analyzer_settings = Label(rootwindow, text="Analyzer Settings:")
l_analyzer_settings.place(x=110,y=500)
#Checkbox variables
checkbox_boxsize = IntVar()
checkbox_grid = IntVar()
checkbox_manual = IntVar()
#check box
c_boxsize = Checkbutton(rootwindow, text="Box - Size in cm2", var=checkbox_boxsize, background = 'blue')
c_boxsize.place(x=150,y=530)
en_boxsize = Entry(rootwindow, width=4)
en_boxsize.insert(0, "0")
en_boxsize.place(x=300,y=535)
c_grid = Checkbutton(rootwindow, text="Grid - Divisions Level", var=checkbox_grid, background = 'green', state=DISABLED)
c_grid.place(x=150, y=565)
en_grid = Entry(rootwindow, width=4)
en_grid.insert(0, "0")
en_grid.place(x=300,y=565)
c_manual = Checkbutton(rootwindow, text="Manual - Selection", var=checkbox_manual, background = 'purple')
c_manual.place(x=150, y= 600)
b_manual_rect = Button(rootwindow, text="Set Rect", command=bf_mansetrect)
b_manual_rect.place(x=300, y=605)
#b_manual_circle = Button(rootwindow, text="Set Circle", command=bf_mansetcircle)
#b_manual_circle.place(x=360, y=605)
#preview buttons
b_preview_settings = Button(rootwindow, text="Preview Settings",command=bf_preview_settings)
b_preview_settings.place(x=100,y=660)
### Video Information
l_trial_info = Label(rootwindow, text="Trial Information:")
l_trial_info.place(x=700, y=470)
l_trial_name = Label(rootwindow, text="Resolution=")
l_trial_name.place(x=720, y=500)
l_trial_name2 = Label(rootwindow, text="")
l_trial_name2.place(x=820, y=500)
l_trial_fps = Label(rootwindow, text="FPS=")
l_trial_fps.place(x=720, y=520)
l_trial_fps2 = Label(rootwindow, text="")
l_trial_fps2.place(x=820, y=520)
l_trial_length = Label(rootwindow, text="Length=")
l_trial_length.place(x=720, y=540)
l_trial_length2 = Label(rootwindow, text="")
l_trial_length2.place(x=820, y=540)
### Seconds to Analyze
l_seconds_to_analyze = Label(rootwindow, text="Seconds to Analyze:")
l_seconds_to_analyze.place(x=700, y=630)
en_seconds_to_analyze = Entry(rootwindow, width=6)
en_seconds_to_analyze.insert(0,"120")
en_seconds_to_analyze.place(x=830, y=630)
#Progress Bar
l_progress = Label(rootwindow, text="")
l_progress.place(x=800, y=660)
### Analyze
b_analyzer = Button(rootwindow, text=">>ANALYZE!<<", background ="red", command=bf_analyzer)
### Analyze
b_analyzer.place(x=700, y=660)
#FileList #listbox
l_filestoprocess = Label(text = "Files to Process:")
lb_mainlist = Listbox(rootwindow, width=35, height=20)
lb_mainlist.bind("<BackSpace>", del_lbitem)
lb_mainlist.bind("<Double-Button-1>", click_viewfile)
l_filestoprocess.place(x = 700, y = 0)
lb_mainlist.place(x = 700, y = 20)
#filelist buttons
b_loadfiles = Button(rootwindow, text="Load Files", command=bf_load_files)
b_loadfiles.place(x=700, y=350)
b_clearfiles = Button(rootwindow, text="Clear Files", command=bf_clearfiles)
b_clearfiles.place(x=800, y=350)
#Zone Scroll Bar
sb_listscroll = Scrollbar(rootwindow)
sb_listscroll.place(x = 950, y = 50)
lb_mainlist.config(yscrollcommand=sb_listscroll.set)
sb_listscroll.config(command=lb_mainlist.yview)
### WIDGET PLACEMENT ###
# create a pulldown menu, and add it to the menu bar
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Load Video File", command=menu_load_files)
filemenu.add_separator()
filemenu.add_command(label="Save Zone Data", command=menu_saveconfig)
filemenu.add_command(label="Load Zone Data", command=menu_loadconfig)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=rootwindow.quit)
menubar.add_cascade(label="File", menu=filemenu)
# display the menu
rootwindow.config(menu=menubar)
rootwindow.mainloop()