db_eval.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import wx
00003 import os
00004 import math
00005 import random
00006 import sys
00007 import subprocess
00008 
00009 import math
00010 from threading import Thread
00011 
00012 
00013 class dlg(wx.Frame):
00014   def __init__(self):
00015 
00016     # varables for gui
00017     #self.invalid_file_path="/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/eval_tool_files/invalidlist"
00018     #self.bin_path="/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/bin/"
00019     #self.base_path="/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/"
00020     self.base_path="/share/goa-tz/people_detection/eval/"
00021     self.bin_path="/home/goa-tz/git/care-o-bot/cob_people_perception/cob_people_detection/bin/"
00022     self.invalid_file_path="/share/goa-tz/people_detection/eval/eval_tool_files/invalidlist"
00023     self.bin_name="synth_face_test"
00024     #self.bin_name="face_rec_alg_test"
00025 
00026 
00027     print "Database Evaluation GUI 2013 - ipa-goa-tz"
00028     print "running with binary: %s"%os.path.join(self.bin_path,self.bin_name)
00029 
00030     #self.Evaluator=Evaluator()
00031     self.Evaluator=Evaluator(invalid_list=self.invalid_file_path)
00032 
00033 
00034     self.output_path=os.path.join(self.base_path,"eval_tool_files")
00035     self.cwd=os.getcwd()
00036     self.ts_dir_list = list()
00037     self.f=wx.Frame(None,title="Evaluation GUI",size=(650,500))
00038 
00039     # variables for output
00040     self.pf_list = list()
00041     self.ts_list = list()
00042     self.cl_list = list()
00043     self.unknown_list=list()
00044     self.invalid_list=list()
00045 
00046     # can be switched to true if list synching has to be reversed
00047     self.synch_lists_switch=False
00048 
00049 
00050     if os.path.isfile(self.invalid_file_path):
00051       with open(self.invalid_file_path,"r") as input_stream:
00052         self.invalid_list=input_stream.read().splitlines()
00053       print "invalid list loaded"
00054       print self.invalid_list
00055 
00056 
00057     self.makeLayout(self.f)
00058     self.makeBindings()
00059     self.f.Show()
00060   def makeBindings(self):
00061     self.dir_btn.Bind(wx.EVT_BUTTON,self.OnAddDir)
00062     self.pf_btn.Bind(wx.EVT_BUTTON,self.OnAddPf)
00063     self.ok_btn.Bind(wx.EVT_BUTTON,self.OnProcess)
00064     self.reset_btn.Bind(wx.EVT_BUTTON,self.OnReset)
00065     self.mproc_btn.Bind(wx.EVT_BUTTON,self.OnProcessMulti)
00066     #self.vis_btn.Bind(wx.EVT_BUTTON,self.OnRunVis)
00067     #self.eval_btn.Bind(wx.EVT_BUTTON,self.OnEvaluate)
00068 
00069 
00070     self.del_dir_btn.Bind(wx.EVT_BUTTON,lambda evt,gl=self.ts_glist,l=self.ts_dir_list :self.OnResetList(evt,l,gl))
00071     self.del_pf_btn .Bind(wx.EVT_BUTTON,lambda evt,gl=self.pf_glist,l=self.pf_list :self.OnResetList(evt,l,gl))
00072 
00073   def makeLayout(self,parent):
00074     pf_btn_txt=wx.StaticText(parent,-1,"Select probe file")
00075     self.pf_btn=wx.Button(parent,-1,"Browse",(70,30))
00076 
00077     del_pf_btn_txt=wx.StaticText(parent,-1,"Delete probe file")
00078     self.del_pf_btn=wx.Button(parent,-1,"Reset",(70,30))
00079 
00080     dir_btn_txt=wx.StaticText(parent,-1,"Add training set")
00081     self.dir_btn=wx.Button(parent,-1,"Browse",(70,30))
00082 
00083     del_dir_btn_txt=wx.StaticText(parent,-1,"Delete Database")
00084     self.del_dir_btn=wx.Button(parent,-1,"Reset",(70,30))
00085 
00086     reset_btn_txt=wx.StaticText(parent,-1,"Delete all lists")
00087     self.reset_btn=wx.Button(parent,-1,"Delete",(70,30))
00088 
00089     ok_btn_txt=wx.StaticText(parent,-1,"Processing")
00090     self.ok_btn=wx.Button(parent,-1,"Process",(70,30))
00091 
00092     #vis_btn_txt=wx.StaticText(parent,-1,"Visualize")
00093     #self.vis_btn=wx.Button(parent,-1,"Ok",(70,30))
00094 
00095     #eval_btn_txt=wx.StaticText(parent,-1,"Evaluate")
00096     #self.eval_btn=wx.Button(parent,-1,"Ok",(70,30))
00097 
00098 
00099     protocol_choice_txt=wx.StaticText(parent,-1,"Testfile selection")
00100     self.protocol_choice=wx.Choice(parent,-1,choices=["leave one out","leave half out","manual selection","unknown","yale2","yale3","yale4","yale5","kinect","synth"])
00101 
00102 
00103     #spin_rep_txt=wx.StaticText(parent,-1,"Repetitions")
00104     self.spin_rep=wx.SpinCtrl(parent,-1,size=wx.Size(50,30),min=1,max=60)
00105 
00106     #classifier_choice_txt=wx.StaticText(parent,-1,"Select Classifier")
00107     #self.classifier_choice=wx.Choice(parent,-1,choices=["MIN DIFFS","KNN","SVM","RandomForest"])
00108 
00109     method_choice_txt=wx.StaticText(parent,-1,"Select Method")
00110     self.method_choice=wx.Choice(parent,-1,choices=["2D LDA","Fisherfaces","Eigenfaces","2D PCA"])
00111 
00112     self.nrm_checkbox=wx.CheckBox(parent,label="normalize")
00113 
00114     self.use_xyz_data=wx.CheckBox(parent,label="use xyz data")
00115 
00116     self.upd_checkbox=wx.CheckBox(parent,label="update lists")
00117     self.upd_checkbox.SetValue(True)
00118 
00119 
00120     mproc_btn_txt=wx.StaticText(parent,-1," Cross Validation")
00121     self.mproc_btn=wx.Button(parent,-1,"Process",(70,30))
00122 
00123     plist_btn_txt=wx.StaticText(parent,-1,"Print lists")
00124     self.plist_btn=wx.Button(parent,-1,"Print",(70,30))
00125 
00126     # visual feedback lists
00127     self.ts_glist=wx.ListBox(choices=[],id=-1,parent=parent,size=wx.Size(80,100))
00128     self.pf_glist=wx.ListBox(choices=[],id=-1,parent=parent,size=wx.Size(80,100))
00129 
00130     # dummy for filling empty spaces
00131     dummy=wx.StaticText(parent,-1,'')
00132 
00133     # Change to flexgridsizer
00134     ##sizer=wx.GridSizer(8,3,0,0)
00135     sizer=wx.FlexGridSizer(10,3,0,0)
00136     sizer.AddGrowableCol(2)
00137 
00138     sizer.Add(dir_btn_txt,1,wx.BOTTOM |wx.ALIGN_BOTTOM)
00139     sizer.Add(del_dir_btn_txt,1,wx.BOTTOM |wx.ALIGN_BOTTOM)
00140     sizer.Add(dummy,1,wx.EXPAND)
00141 
00142     bs_3=wx.BoxSizer(wx.VERTICAL)
00143     bs_3.Add(self.dir_btn,1)
00144     bs_3.Add(self.use_xyz_data,1)
00145     sizer.Add(bs_3,1)
00146     #sizer.Add(self.dir_btn,1)
00147     sizer.Add(self.del_dir_btn,1)
00148     sizer.Add(self.ts_glist,1,wx.EXPAND)
00149 
00150 
00151 
00152 
00153 
00154     sizer.Add(pf_btn_txt,1,wx.BOTTOM |wx.ALIGN_BOTTOM)
00155     sizer.Add(del_pf_btn_txt,1,wx.BOTTOM |wx.ALIGN_BOTTOM)
00156     sizer.Add(dummy,1,wx.EXPAND)
00157 
00158     sizer.Add(self.pf_btn,1)
00159     sizer.Add(self.del_pf_btn,1)
00160     sizer.Add(self.pf_glist,1,wx.EXPAND)
00161 
00162     sizer.Add(protocol_choice_txt,1,wx.BOTTOM |wx.ALIGN_BOTTOM)
00163     sizer.Add(dummy,1,wx.EXPAND)
00164     sizer.Add(dummy,1,wx.EXPAND)
00165 
00166     sizer.Add(self.protocol_choice,1)
00167     sizer.Add(dummy,1,wx.EXPAND)
00168     sizer.Add(dummy,1,wx.EXPAND)
00169 
00170     sizer.Add(ok_btn_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00171     sizer.Add(dummy,1,wx.EXPAND)
00172     sizer.Add(method_choice_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00173     #sizer.Add(classifier_choice_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00174 
00175     sizer.Add(self.ok_btn,1)
00176     sizer.Add(self.upd_checkbox,1)
00177 
00178     bs=wx.BoxSizer(wx.HORIZONTAL)
00179     bs.Add(self.method_choice,1)
00180     #bs.Add(self.classifier_choice,1)
00181     bs.Add(self.nrm_checkbox,1)
00182     sizer.Add(bs,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00183 
00184     sizer.Add(mproc_btn_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00185     sizer.Add(dummy,1,wx.EXPAND)
00186     sizer.Add(dummy,1,wx.EXPAND)
00187 
00188     #sizer.Add(vis_btn_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00189     #sizer.Add(eval_btn_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00190 
00191     sizer.Add(self.mproc_btn,1)
00192     sizer.Add(self.spin_rep,1)
00193     sizer.Add(dummy,1,wx.EXPAND)
00194 
00195     sizer.Add(reset_btn_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00196     sizer.Add(plist_btn_txt,1,wx.BOTTOM | wx.ALIGN_BOTTOM)
00197     sizer.Add(dummy,1,wx.EXPAND)
00198 
00199     sizer.Add(self.reset_btn,1)
00200     sizer.Add(self.plist_btn,1)
00201     sizer.Add(dummy,1,wx.EXPAND)
00202     #sizer.Add(self.eval_btn,1)
00203     #sizer.Add(self.vis_btn,1)
00204 
00205     parent.SetSizer(sizer)
00206 #################### CALLBACK ###########################
00207   def OnEvaluate(self,e):
00208     self.evaluate()
00209 
00210   def OnRunVis(self,e):
00211     script_path=self.cwd+"/pvis.m"
00212     os.system("octave  %s"%script_path)
00213     os.chdir(self.base_path+"/vis")
00214     os.system("eog clustering_FF.jpg ")
00215     os.chdir(self.cwd)
00216   def OnAddPf(self,e):
00217     self.pf_dlg=wx.FileDialog(None,"Select Probefile",defaultDir=self.base_path,style=wx.FD_MULTIPLE)
00218     if self.pf_dlg.ShowModal() == wx.ID_OK:
00219       temp_list= self.pf_dlg.GetPaths()
00220       for temp in temp_list:
00221         self.pf_glist.Append(temp)
00222 
00223   def OnAddDir(self,e):
00224     self.ts_dlg=wx.DirDialog(None,"Select directories")
00225     self.ts_dlg.SetPath(self.base_path)
00226     if self.ts_dlg.ShowModal() == wx.ID_OK:
00227       self.ts_dir_list.append(self.ts_dlg.GetPath())
00228       self.ts_glist.Append(self.ts_dir_list[-1])
00229 
00230   def OnProcessMulti(self,e):
00231     # get method and classifer
00232     method=str()
00233     #classifier=str()
00234     xyz_tag=str()
00235     if self.method_choice.GetCurrentSelection()==0:
00236       method="LDA2D"
00237     elif self.method_choice.GetCurrentSelection()==1:
00238       method="FISHER"
00239     elif self.method_choice.GetCurrentSelection()==2:
00240       method="EIGEN"
00241     elif self.method_choice.GetCurrentSelection()==3:
00242       method="PCA2D"
00243 
00244    # if self.classifier_choice.GetCurrentSelection()==0:
00245    #   classifier="DIFFS"
00246    # elif self.classifier_choice.GetCurrentSelection()==1:
00247    #   classifier="KNN"
00248    # elif self.classifier_choice.GetCurrentSelection()==2:
00249    #   classifier="SVM"
00250    # elif self.classifier_choice.GetCurrentSelection()==3:
00251    #   classifier="RF"
00252 
00253     if self.use_xyz_data.Value==True:
00254       xyz_tag="1"
00255     elif self.use_xyz_data.Value==False:
00256       xyz_tag="0"
00257 
00258     # if lists are supposed to be updated
00259     #if self.upd_checkbox.GetValue()==True:
00260     prot_choice=self.protocol_choice.GetCurrentSelection()
00261     for i in xrange(self.spin_rep.GetValue()):
00262       output_file=os.path.join(self.output_path,"eval_file")
00263       if len(self.ts_dir_list)>0:
00264         if(self.protocol_choice.GetCurrentSelection()==0):
00265           self.process_protocol(self.process_leave_1_out,method)
00266           #self.process_protocol(self.process_leave_1_out,method,classifier)
00267 
00268         elif(self.protocol_choice.GetCurrentSelection()==1):
00269           self.process_protocol(self.process_leave_half_out,method)
00270           #self.process_protocol(self.process_leave_half_out,method,classifier)
00271 
00272         elif(self.protocol_choice.GetCurrentSelection()==2):
00273           print "manual selection not suitable for cross validation"
00274           return
00275         elif(self.protocol_choice.GetCurrentSelection()==3):
00276           self.process_protocol(self.process_unknown,method)
00277           #self.process_protocol(self.process_unknown,method,classifier)
00278         elif(prot_choice==4):
00279           self.yale_flag=2
00280           #self.process_protocol(self.process_yale(2),method,classifier)
00281           self.process_protocol(self.process_yale(2),method)
00282         elif(prot_choice==5):
00283           self.yale_flag=3
00284           self.process_protocol(self.process_yale(3),method)
00285           #self.process_protocol(self.process_yale(3),method,classifier)
00286         elif(prot_choice==6):
00287           self.yale_flag=4
00288           #self.process_protocol(self.process_yale(4),method,classifier)
00289           self.process_protocol(self.process_yale(4),method)
00290         elif(prot_choice==7):
00291           self.yale_flag=5
00292           self.process_protocol(self.process_yale(5),method)
00293           #self.process_protocol(self.process_kinect,method,classifier)
00294         elif(prot_choice==9):
00295           self.process_protocol(self.process_synth,method)
00296 
00297         # run binaryin
00298         os.chdir(self.bin_path)
00299         if self.nrm_checkbox.GetValue()==True:
00300           normalizer="1"
00301         else:
00302           normalizer="0"
00303         t=Thread(target=self.run_bin,args=(method,normalizer,xyz_tag))
00304         t.start()
00305         t.join()
00306 
00307         os.chdir(self.cwd)
00308         self.evaluate()
00309         os.rename(output_file,output_file+str(i))
00310     print self.Evaluator.calc_stats()
00311     self.Evaluator.reset()
00312 
00313 
00314   def OnProcess(self,e):
00315     # get method and classifer
00316     method=str()
00317     classifier=str()
00318     xyz_tag=str()
00319     if self.method_choice.GetCurrentSelection()==0:
00320       method="LDA2D"
00321     elif self.method_choice.GetCurrentSelection()==1:
00322       method="FISHER"
00323     elif self.method_choice.GetCurrentSelection()==2:
00324       method="EIGEN"
00325     elif self.method_choice.GetCurrentSelection()==3:
00326       method="PCA2D"
00327 
00328     #if self.classifier_choice.GetCurrentSelection()==0:
00329     #  classifier="DIFFS"
00330     #elif self.classifier_choice.GetCurrentSelection()==1:
00331     #  classifier="KNN"
00332     #elif self.classifier_choice.GetCurrentSelection()==2:
00333     #  classifier="SVM"
00334     #elif self.classifier_choice.GetCurrentSelection()==3:
00335     #  classifier="RF"
00336 
00337     prot_choice=self.protocol_choice.GetCurrentSelection()
00338     # if lists are supposed to be updated
00339     if self.upd_checkbox.GetValue()==True:
00340       if len(self.ts_dir_list)>0:
00341         if(self.protocol_choice.GetCurrentSelection()==0):
00342           self.process_protocol(self.process_leave_1_out,method)
00343           #self.process_protocol(self.process_leave_1_out,method,classifier)
00344           output_file=os.path.join(self.output_path,"eval_file")
00345           self.process_protocol(self.process_leave_1_out,method)
00346           #self.process_protocol(self.process_leave_1_out,method,classifier)
00347 
00348         elif(self.protocol_choice.GetCurrentSelection()==1):
00349           self.process_protocol(self.process_leave_half_out,method)
00350           #self.process_protocol(self.process_leave_half_out,method,classifier)
00351           output_file=os.path.join(self.output_path,"eval_file")
00352           self.process_protocol(self.process_leave_half_out,method)
00353           #self.process_protocol(self.process_leave_half_out,method,classifier)
00354 
00355         elif(self.protocol_choice.GetCurrentSelection()==2):
00356           self.process_protocol(self.process_manual,method)
00357           #self.process_protocol(self.process_manual,method,classifier)
00358         elif(self.protocol_choice.GetCurrentSelection()==3):
00359           output_file=os.path.join(self.output_path,"eval_file")
00360           #self.process_protocol(self.process_unknown,method,classifier)
00361           self.process_protocol(self.process_unknown,method)
00362         elif(prot_choice==4):
00363           self.yale_flag=2
00364           #self.process_protocol(self.process_yale,method,classifier)
00365           self.process_protocol(self.process_yale,method)
00366         elif(prot_choice==5):
00367           self.yale_flag=3
00368           #self.process_protocol(self.process_yale,method,classifier)
00369           self.process_protocol(self.process_yale,method)
00370         elif(prot_choice==6):
00371           self.yale_flag=4
00372           #self.process_protocol(self.process_yale,method,classifier)
00373           self.process_protocol(self.process_yale,method)
00374         elif(prot_choice==7):
00375           self.yale_flag=5
00376           self.process_protocol(self.process_yale,method)
00377           #self.process_protocol(self.process_yale,method,classifier)
00378         elif(prot_choice==8):
00379           #self.process_protocol(self.process_kinect,method,classifier)
00380           self.process_protocol(self.process_kinect,method)
00381         elif(prot_choice==9):
00382           self.process_protocol(self.process_synth,method)
00383 
00384     if self.use_xyz_data.Value==True:
00385       xyz_tag="1"
00386     elif self.use_xyz_data.Value==False:
00387       xyz_tag="0"
00388 
00389     # run binary
00390     os.chdir(self.bin_path)
00391     if self.nrm_checkbox.GetValue()==True:
00392       #bin_str="./ssa_test "+method+" "+classifier+" 1 "+xyz_tag
00393       normalizer="1"
00394     else:
00395       #bin_str="./ssa_test "+method+" "+classifier+" 0 "+xyz_tag
00396       normalizer="0"
00397     t=Thread(target=self.run_bin,args=(method,normalizer,xyz_tag))
00398     t.start()
00399     t.join()
00400 
00401 
00402     os.chdir(self.cwd)
00403     self.evaluate()
00404 
00405     print self.Evaluator.calc_stats()
00406     self.Evaluator.reset()
00407 
00408   def OnReset(self,e):
00409       self.delete_files()
00410 
00411 
00412   def OnResetList(self,e,l,gl):
00413     del l[:]
00414     gl.Clear()
00415 
00416   def delete_files(self):
00417     os.chdir(self.output_path)
00418     str_list=["rm","classification_labels","eval_file","class_overview","probe_file_list","probe_file_xyz_list","training_set_list","training_set_xyz_list"]
00419     subprocess.call(str_list)
00420 
00421 
00422   def reset_lists(self):
00423     del self.ts_list[:]
00424     del self.pf_list[:]
00425     del self.cl_list[:]
00426     del self.unknown_list[:]
00427 
00428 
00429 #*****************************************************
00430 #****************Internal Functions********************
00431 #*****************************************************
00432 
00433   def process_protocol(self,protocol_fn,method):
00434     self.reset_lists()
00435     self.file_ops(self.make_ts_list)
00436     self.file_ops(self.make_cl_list)
00437     #print self.cl_list
00438     protocol_fn()
00439     self.sync_lists()
00440     print self.ts_list
00441     print "--------------------------------------------------------"
00442     print self.pf_list
00443     self.print_lists()
00444 
00445 
00446   def run_bin(self,method,normalize,xyz):
00447     binary=os.path.join(self.bin_path,self.bin_name)
00448 
00449     subprocess.call([binary,method,normalize,xyz])
00450 
00451   def process_synth(self):
00452     self.process_leave_1_out()
00453     tmp=self.pf_list
00454     self.pf_list=self.ts_list
00455     self.pf_list.append([])
00456     self.ts_list=tmp
00457     del self.ts_list[-1]
00458     self.synch_lists_switch=True
00459 
00460 
00461   def process_kinect2(self):
00462     self.ts_list=list()
00463     self.pf_list=list()
00464     self.file_ops(self.kinect2)
00465     ##append empty list for unknown calssifications
00466     self.pf_list.append([])
00467     self.synch_lists_switch=False
00468 
00469   def process_kinect(self):
00470     self.reset_lists()
00471     pf_path=os.path.join(self.base_path,"eval_tool_files","pf_list")
00472     ts_path=os.path.join(self.base_path,"eval_tool_files","ts_list")
00473     curr_files=list()
00474     with open(ts_path,"r") as input_stream:
00475       lines=input_stream.read().splitlines()
00476       for line in lines:
00477         if "$$" in line:
00478           self.ts_list.append(curr_files)
00479           curr_files=list()
00480         else:
00481           curr_files.append(line)
00482 
00483 
00484     cl_ctr=0
00485     curr_files=list()
00486     with open(pf_path,"r") as input_stream:
00487       lines=input_stream.read().splitlines()
00488       for line in lines:
00489         if "$$" in line:
00490           print line
00491           cl_ctr+=1
00492           self.cl_list.append(cl_ctr)
00493           self.pf_list.append(curr_files)
00494           curr_files=list()
00495         else:
00496           curr_files.append(line)
00497 
00498 
00499     self.pf_list.append([])
00500     self.synch_lists_switch=False
00501     self.sync_lists()
00502     self.print_lists()
00503 
00504     
00505 
00506   def process_yale(self):
00507     k=self.yale_flag
00508     self.file_ops(self.yale,k)
00509     ##append empty list for unknown calssifications
00510     self.pf_list.append([])
00511    # k=-1
00512    # self.file_ops(self.yale,k)
00513     self.synch_lists_switch=False
00514 
00515   def process_unknown(self):
00516     C=len(self.cl_list)
00517     ctr=0
00518     rnd_list=list()
00519     k=[False for i in range(int(len(self.cl_list)))]
00520     while ctr < math.floor(C*0.5):
00521       rnd=random.randint(0,C-1)
00522       if  rnd not in  rnd_list:
00523         rnd_list.append(rnd)
00524         k[rnd]=True
00525         ctr+=1
00526     i=0
00527     for cl in self.cl_list:
00528       if cl:
00529         del self.cl_list[i]
00530       i+=1
00531 
00532     self.file_ops(self.unknown,k)
00533     self.pf_list.append(self.unknown_list)
00534     del k[:]
00535     del rnd_list[:]
00536     self.synch_lists_switch=False
00537 
00538   def process_leave_half_out(self):
00539     self.file_ops(self.leave_k_out,"half")
00540     ##append empty list for unknown calssifications
00541     self.pf_list.append([])
00542     self.synch_lists_switch=False
00543 
00544   def process_leave_1_out(self):
00545     self.file_ops(self.leave_k_out,1)
00546     ##append empty list for unknown calssifications
00547     self.pf_list.append([])
00548     self.synch_lists_switch=False
00549 
00550   def process_manual(self):
00551     self.pf_list=[[] for i in range(len(self.cl_list)+1)]
00552     self.pf_list_format(self.pf_glist.GetItems())
00553     self.synch_lists_switch=False
00554 
00555   def file_ops(self,fn=-1,add_param=False):
00556 
00557     if fn ==-1:
00558       def fn(x):
00559         return x
00560 
00561     for db in self.ts_dir_list:
00562       db_path=db
00563       os.chdir(db_path)
00564 
00565       dir_list=os.listdir(".")
00566       # enter directory - class
00567       for dir in dir_list:
00568         file_list_valid=list()
00569         os.chdir(dir)
00570         file_list_all=os.listdir(".")
00571 
00572         # loop through all files
00573         for file in file_list_all:
00574           if file.endswith(".bmp") or file.endswith(".jpg") or file.endswith(".pgm") or file.endswith(".png"):
00575             if not file.endswith("Ambient.pgm") or file in self.invalid_list:
00576 
00577               # construct filepath
00578               file_path=db_path+"/"+dir+"/"+file
00579               file_list_valid.append(file_path)
00580         if add_param==False:
00581           fn(file_list_valid)
00582         else:
00583           fn(file_list_valid,add_param)
00584         os.chdir(db_path)
00585 
00586 
00587   def pf_list_format(self,file_list):
00588     for cl in xrange(len(self.ts_list)):
00589         for i in reversed(xrange(len(file_list))):
00590           if file_list[i] in self.ts_list[cl]:
00591             self.pf_list[cl].append(file_list[i])
00592             file_list.remove(file_list[i])
00593     for rf in file_list:
00594       self.pf_list[-1].append(rf)
00595 
00596   def kinect(self,files):
00597     ts=list()
00598     pf=list()
00599     for f in files:
00600       name=os.path.split(f)[1]
00601       if name[0]=="_":
00602         #print name
00603         pf.append(f)
00604       else:
00605         ts.append(f)
00606     self.pf_list.append(pf)
00607     self.ts_list.append(ts)
00608 
00609   def kinect2(self,files):
00610     ts_list=list()
00611     pf_list=list()
00612     for file in files:
00613       persp=(int(file[-8]))
00614       if (persp == 7 or 
00615           persp == 11 or
00616           persp == 13 or
00617           persp == 11 or
00618           persp == 1
00619          # persp == 14 or
00620          # persp==  15 or
00621          # persp==  16 or
00622          # persp == 17 
00623           #persp == 3 or
00624           #persp == 2 or
00625           #persp == 4 or
00626           #persp == 10 or
00627           #persp == 12 
00628           ):
00629         ts_list.append(file)
00630       #elif (persp == 7 or 
00631       #    persp == 3 or
00632       #    persp == 2 or
00633       #    persp == 4 or
00634       #    persp == 10 or
00635       #    persp == 12 
00636       #    ):
00637       #  pf_list.append(file)
00638       else:
00639         pf_list.append(file)
00640     self.pf_list.append(pf_list)
00641     self.ts_list.append(ts_list)
00642 
00643   def yale(self, files,k):
00644    # remove files from ts list
00645    # if k==-1:
00646    #   for file in self.ts_list:
00647    #     if file not in self.pf_list:
00648    #       self.ts_list.remove(file)
00649 
00650 
00651      ss=[[] for i in range(5)]
00652      for item in files:
00653        if (int(item[-11:-8]) <=12 and int(item[-6:-4]) <=12):
00654        #if int(item[-11:-8]) <=12:
00655          ss[0].append(item)
00656          continue
00657        elif (int(item[-11:-8]) <=25 and  int(item[-6:-4]) <=25):
00658        #elif int(item[-11:-8]) <=25:
00659          ss[1].append(item)
00660          continue
00661        elif (int(item[-11:-8]) <=50 and int(item[-6:-4]) <=50):
00662        #elif int(item[-11:-8]) <=50:
00663          ss[2].append(item)
00664          continue
00665        elif (int(item[-11:-8]) <=77 and int(item[-6:-4]) <=77):
00666        #elif int(item[-11:-8]) <=77:
00667          ss[3].append(item)
00668          continue
00669        elif ( int(item[-11:-8]) >77 or int(item[-6:-4]) >77):
00670        #elif  int(item[-11:-8]) >77:
00671          ss[4].append(item)
00672          continue
00673 
00674      self.pf_list.append(ss[k-1])
00675      for s in ss[1:]:
00676        for f in s:
00677            for c in self.ts_list:
00678              if f in c:
00679                c.remove(f)
00680 
00681     #  ss=[[] for i in range(6)]
00682     #  for item in files:
00683     #    if (int(item[-11:-8]) ==0 and int(item[-6:-4]) ==0):
00684     #      ss[0].append(item)
00685     #    elif (int(item[-11:-8]) <=12 and int(item[-6:-4]) <=12):
00686     #    #if int(item[-11:-8]) <=12:
00687     #      ss[1].append(item)
00688     #      continue
00689     #    elif (int(item[-11:-8]) <=25 and  int(item[-6:-4]) <=25):
00690     #    #elif int(item[-11:-8]) <=25:
00691     #      ss[2].append(item)
00692     #      continue
00693     #    elif (int(item[-11:-8]) <=50 and int(item[-6:-4]) <=50):
00694     #    #elif int(item[-11:-8]) <=50:
00695     #      ss[3].append(item)
00696     #      continue
00697     #    elif (int(item[-11:-8]) <=77 and int(item[-6:-4]) <=77):
00698     #    #elif int(item[-11:-8]) <=77:
00699     #      ss[4].append(item)
00700     #      continue
00701     #    elif ( int(item[-11:-8]) >77 or int(item[-6:-4]) >77):
00702     #    #elif  int(item[-11:-8]) >77:
00703     #      ss[5].append(item)
00704     #      continue
00705 
00706     #  self.pf_list.append(ss[k])
00707     #  for s in ss[1:]:
00708     #    for f in s:
00709     #        for c in self.ts_list:
00710     #          if f in c:
00711     #            c.remove(f)
00712 
00713 
00714 
00715   def unknown(self, files,k):
00716       if k[0] ==True:
00717         self.unknown_list.extend(files)
00718         del k[0]
00719       else:
00720         num_samples=len(files)
00721         n=num_samples/2
00722         #n=9
00723         success_ctr=0
00724         rnd_list=list()
00725         pf_list=list()
00726         while len(rnd_list)<n:
00727           rnd_no=random.randint(0,num_samples-1)
00728           if not rnd_no in rnd_list :
00729             pf_list.append(files[rnd_no])
00730             rnd_list.append(rnd_no)
00731         self.pf_list.append(pf_list)
00732         del k[0]
00733 
00734   def leave_k_out(self,file_list_valid,k):
00735         num_samples=len(file_list_valid)
00736         if(k is "half"):
00737           k=num_samples/2
00738         success_ctr=0
00739         rnd_list=list()
00740         pf_list=list()
00741         while len(rnd_list)<k:
00742           rnd_no=random.randint(0,num_samples-1)
00743           if not rnd_no in rnd_list :
00744             pf_list.append(file_list_valid[rnd_no])
00745             rnd_list.append(rnd_no)
00746         self.pf_list.append(pf_list)
00747 
00748   def make_ts_list(self,file_list):
00749       self.ts_list.append(file_list)
00750 
00751   def make_cl_list(self,file_list):
00752     class_name=os.path.basename(os.getcwd())
00753     self.cl_list.append(class_name)
00754 
00755 
00756   def sync_lists(self):
00757     if  self.synch_lists_switch ==True:
00758       for c in xrange(len(self.ts_list)):
00759         for s in self.ts_list[c]:
00760           if len(s) >0:
00761             for ts in self.pf_list:
00762               if s in ts:
00763                 ts.remove(s)
00764     else:
00765       for c in xrange(len(self.pf_list)):
00766         for s in self.pf_list[c]:
00767           if len(s) >0:
00768             for ts in self.ts_list:
00769               if s in ts:
00770                 ts.remove(s)
00771 
00772 
00773     #print "length list%i"%len(self.pf_list[0])
00774     #print "length list%i"%len(self.ts_list[0])
00775 
00776   def evaluate(self):
00777 
00778     results=list()
00779     groundtruth=list()
00780     files=list()
00781 
00782     input_path=os.path.join(self.output_path,"classification_labels")
00783     eval_file_path=os.path.join(self.output_path,"eval_file")
00784     with open(input_path,"r") as input_stream:
00785       classified_list=input_stream.read().splitlines()
00786 
00787     with open(eval_file_path,"w") as eval_file_stream:
00788       cont_ctr=0
00789       for pf_l in xrange(len(self.cl_list)):
00790         for pf in self.pf_list[pf_l]:
00791           o_str = pf+" "+str(pf_l)+" "+str(classified_list[cont_ctr])+"\n"
00792           eval_file_stream.write(o_str)
00793 
00794           results.append(classified_list[cont_ctr])
00795           groundtruth.append(pf_l)
00796           files.append(pf)
00797 
00798           cont_ctr+=1
00799       # append unknown probe files as -1
00800       for ukf in self.pf_list[-1]:
00801             o_str = ukf+" "+str(-1)+" "+str(classified_list[cont_ctr])+"\n"
00802             eval_file_stream.write(o_str)
00803             results.append(classified_list[cont_ctr])
00804             groundtruth.append(-1)
00805             files.append(ukf)
00806             cont_ctr+=1
00807 
00808     self.Evaluator.add_epoch(groundtruth,results,files)
00809     print "error rate = %f"%self.Evaluator.show_last()
00810 
00811 
00812   def print_lists(self):
00813     #print self.pf_list
00814 
00815     #print "[EVAL TOOL] creating lists"
00816     training_set_list_path=os.path.join(self.output_path,"training_set_list")
00817     training_set_list_xyz_path=os.path.join(self.output_path,"training_set_xyz_list")
00818     probe_file_list_path=os.path.join(self.output_path,"probe_file_list")
00819     probe_file_xyz_list_path=os.path.join(self.output_path,"probe_file_xyz_list")
00820     class_overview_path=os.path.join(self.output_path,"class_overview")
00821 
00822 
00823     training_set_file_stream = open(training_set_list_path,"w")
00824     training_set_file_xyz_stream = open(training_set_list_xyz_path,"w")
00825     probe_file_stream = open(probe_file_list_path,"w")
00826     probe_file_xyz_stream = open(probe_file_xyz_list_path,"w")
00827     class_overview_stream = open(class_overview_path,"w")
00828 
00829 
00830     # make lists with depth
00831     if self.use_xyz_data.Value:
00832       for c in xrange(len(self.ts_list)):
00833         if len(self.ts_list[c])>0:
00834           for s in self.ts_list[c]:
00835               if os.path.split(s)[1] not in self.invalid_list:
00836                 s_mod=os.path.splitext(s)[0]+".xml"
00837                 training_set_file_xyz_stream.write(s_mod)
00838                 training_set_file_xyz_stream.write("\n")
00839           training_set_file_xyz_stream.write("$$\n")
00840 
00841 
00842       for c in xrange(len(self.pf_list)):
00843         for s in self.pf_list[c]:
00844           if os.path.split(s)[1] not in self.invalid_list:
00845             s_mod=os.path.splitext(s)[0]+".xml"
00846             probe_file_xyz_stream.write(s_mod)
00847             probe_file_xyz_stream.write("\n")
00848 
00849     for c in xrange(len(self.ts_list)):
00850       if len(self.ts_list[c])>0:
00851         for s in self.ts_list[c]:
00852             if os.path.split(s)[1] not in self.invalid_list:
00853               training_set_file_stream.write(s)
00854               training_set_file_stream.write("\n")
00855         training_set_file_stream.write("$$\n")
00856 
00857     for c in xrange(len(self.pf_list)):
00858       for s in self.pf_list[c]:
00859         if os.path.split(s)[1] not in self.invalid_list:
00860           probe_file_stream.write(s)
00861           probe_file_stream.write("\n")
00862 
00863     for c in xrange(len(self.cl_list)):
00864       o_str=str(c)+" - "+str(self.cl_list[c])+"\n"
00865       class_overview_stream.write(o_str)
00866 
00867 #---------------------------------------------------------------------------------------
00868 #----------------------------EVALUATOR-----------------------------------------
00869 #---------------------------------------------------------------------------------------
00870 
00871 class epoch():
00872   def __init__(self,gt,res,desc,ctr):
00873     self.gt=gt
00874     self.res=res
00875     self.desc=desc
00876     self.error_rate=float()
00877 
00878     self.calc_error_rate()
00879 
00880   def print_error_list(self,error_list_path):
00881     error_list_stream = open(error_list_path,"w")
00882     for s in self.fi_list:
00883       o_str=str(s)+" \n"
00884       error_list_stream.write(o_str)
00885 
00886   def calc_error_rate(self):
00887     error_list=list()
00888     #true positive
00889     tp_list=list()
00890     #true negative
00891     tn_list=list()
00892     #false positive
00893     fp_list=list()
00894     #false negative
00895     fn_list=list()
00896     #false id
00897     self.fi_list=list()
00898     for i in xrange(len(self.gt)):
00899         if (int(self.gt[i]) == int(self.res[i])):
00900           error_list.append(0)
00901           if int(self.gt[i]) >-1:
00902             fp_list.append(0)
00903             fn_list.append(0)
00904             tn_list.append(0)
00905             tp_list.append(1)
00906             self.fi_list.append(0)
00907           elif int(self.gt[i]) ==-1:
00908             fp_list.append(0)
00909             fn_list.append(0)
00910             tn_list.append(1)
00911             tp_list.append(0)
00912             self.fi_list.append(0)
00913         else:
00914           error_list.append(1)
00915           if int(self.gt[i])==-1:
00916             fp_list.append(1)
00917             fn_list.append(0)
00918             tn_list.append(0)
00919             tp_list.append(0)
00920             self.fi_list.append(0)
00921           elif int(self.gt[i]) >-1 and int(self.res[i])==-1:
00922             fp_list.append(0)
00923             fn_list.append(1)
00924             tn_list.append(0)
00925             tp_list.append(0)
00926             self.fi_list.append(0)
00927           elif int(self.gt[i]) >-1 and int(self.res[i])>-1:
00928             self.fi_list.append(1)
00929 
00930     n=len(error_list)
00931     self.error_rate=float(sum(error_list))/float(n)
00932     self.tp=sum(tp_list)
00933     self.tn=sum(tn_list)
00934     self.fp=sum(fp_list)
00935     self.fn=sum(fn_list)
00936     self.fi=sum(self.fi_list)
00937 
00938     
00939 
00940 class Evaluator():
00941   def __init__(self,invalid_list=0):
00942     self.error_list_path="/share/goa-tz/people_detection/eval/eval_tool_files/error_list"
00943     if invalid_list==0:
00944       self.invalid_files=list()
00945     else:
00946       if os.path.exists(invalid_list):
00947         with open(invalid_list,"r") as input_stream:
00948             self.invalid_files=input_stream.read().splitlines()
00949       else:
00950         self.invalid_files=list()
00951 
00952     #print "EVALUATOR instantiated"
00953     self.epochs=list()
00954     self.epoch_ctr=0
00955 
00956   def reset(self):
00957     self.epoch_ctr=0
00958     del self.epochs[:]
00959 
00960 
00961   def add_epoch(self,gt,res,files):
00962     e=epoch(gt,res,files,self.epoch_ctr)
00963     e.print_error_list(self.error_list_path)
00964     self.epoch_ctr+=1
00965     self.epochs.append(e)
00966 
00967   def calc_stats(self):
00968     n=float(len(self.epochs))
00969     err=0.0
00970     mean_error_rate=0.0
00971     for e in self.epochs:
00972       if e.desc in self.invalid_files:
00973         print "invalid file is excluded from statistical calculations"
00974         continue
00975       err+=e.error_rate
00976     mean_error_rate=err/n
00977 
00978 
00979     if len(self.epochs)>1:
00980       v2=0.0
00981       for e in self.epochs:
00982         v2+=(e.error_rate - mean_error_rate)*(e.error_rate - mean_error_rate)
00983       sigma=math.sqrt(1/(n-1)*v2)
00984     else:
00985       sigma=0.0
00986 
00987 
00988    # #remove comment to display true positive and fals positive
00989 
00990     self.print_ave_values(self.epochs)
00991     stats={"succes_rate":1-mean_error_rate,"m_err":mean_error_rate,"sigma":sigma,"reps":len(self.epochs)}
00992 
00993 
00994     return stats
00995 
00996   def show_all(self):
00997     err=list()
00998     for e in self.epochs:
00999       err.append(e.error_rate)
01000 
01001     return err
01002 
01003   def print_ave_values(self,l):
01004     ave_tp=0.0
01005     ave_tn=0.0
01006     ave_fp=0.0
01007     ave_fn=0.0
01008     ave_fi=0.0
01009     for el in l:
01010       ave_tp+=el.tp
01011       ave_tn+=el.tn
01012       ave_fp+=el.fp
01013       ave_fn+=el.fn
01014       ave_fi+=el.fi
01015     ave_tp/=len(l)
01016     ave_tn/=len(l)
01017     ave_fp/=len(l)
01018     ave_fn/=len(l)
01019     ave_fi/=len(l)
01020 
01021     print "True positives:   %f"%   ave_tp
01022     print "True negatives:   %f"%   ave_tn
01023     print "False positives:  %f"%  ave_fp
01024     print "False negatives:  %f"%  ave_fn
01025     print "False identities: %f"% ave_fi
01026 
01027   def show_last(self):
01028     err=self.epochs[-1].error_rate
01029 
01030     return err
01031 
01032 
01033 
01034 
01035 if __name__=="__main__":
01036   app= wx.App(False)
01037   dlg = dlg()
01038   #E=Evaluator()
01039   #a=list([2,3,3,4])
01040   #b=list([1,2,3,4])
01041   #d=list([2,3,3,4])
01042   #c=list(["1","2","3","4"])
01043   #E.add_epoch(a,b,c)
01044   #E.add_epoch(a,d,c)
01045   #print E.mean_error_rate()
01046   app.MainLoop()


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:12