def main(xform,test_set,dictionary):
  object_dict = open(dictionary,'rb')
  objects = pkl.load(object_dict)
  object_dict.close()

  print len(objects)

  for key in objects.iterkeys():

    crops = objects[key]
    image = os.path.join(test_set,key)
    windowing.sanity_check(xform,image,25,24,crops,debug=False)
    print key
def main(test_dir,anno_dir,c1,obj_type,step_size,savename):

    test_set,runningTotal = testdb_loader(test_dir,anno_dir,obj_type)
    
    save_res = open(os.path.join('results',savename+'_detection_experiment_C1.txt'),'wb')
    save_res.write('Average Number of annotations per image: '+str(runningTotal)+'\n')
    for t in test_set: 
        save_res.write(t)
    print "wrote"
    print runningTotal
    test_res = list()
    true = 0
    false = 0
    total_truth = 0
    precision = list()
    recall = list()
    all_objects = list()
    truthDict = resultcheck.getTruthDictionary(test_set,test_dir,anno_dir,obj_type)
    for items in truthDict:
      print items
    
    # get all ground truths for the test set
    for test in test_set:
      ground_truth = resultcheck.getGroundTruths(test,test_dir,anno_dir,obj_type)
      print test,len(ground_truth)
      all_objects.append((ground_truth,test))

      # process all the images and save the tuples in form (bbox,dval,img)
      results = windowing.main(c1,os.path.join(test_dir,test),step_size,24,True)
      test_res.append(results[0])
    
    print len(test_set)
    
    test_res = flattenResults(test_res)
    calcPrecisionRecall(test_res,truthDict,test_dir,savename,save_res)
def main(test_dir,anno_dir,c1,obj_type,step_size,savename):
  save_res = open(os.path.join('results',savename+'_OldSetConfusion.txt'),'wb') 
  testSet,testImages = testdb_loader(test_dir,anno_dir,obj_type)
  test_res = list()
  all_det = list()
  all_objects = list()
  # get all ground truths for the test set
  for test in testSet:
    ground_truth = resultcheck.getGroundTruths(test,test_dir,anno_dir,obj_type)
    all_objects.append((ground_truth,test))

    # process all the images and save the tuples in form (bbox,dval,img)
    results,all_test_results= windowing.main(c1,os.path.join(test_dir,test),step_size,24,True)
    test_res.append(results)
    all_det.append(all_test_results)
  
  print "saving off dval data"
  dvals_copy_file = open(os.path.join('pickledCrops','allDetectionsConfusion.dat'),'wb')
  pickle.dump(all_det,dvals_copy_file)
  dvals_copy_file.close()
  count = 0

  img_res = list()
  print "number of boxes found", count
  for a in range(0,len(all_det)):
    imgdet = list()
    for det in all_det[a]:
      detection_box = det
      print "ahhhh", all_objects[a][0]
      imgdet.append(resultcheck.c_matrix(all_objects[a][0],detection_box))
      #for labels in all_objects:
      #  pass
      #print "Labels", labels
    img_res.append(imgdet)
  tp=0
  fp=0
  tn=0
  fn=0
  
  print "Crops?", len(img_res) , len(all_det)
  for i in range(0,len(img_res)):
    img = Image.open(os.path.join(test_dir,testImages[i]))
    #draw = ImageDraw.Draw(img)
    
    for imgdata in img_res[i]:
      #tp instance
      imgdata = imgdata[0]
      
      if imgdata[1] > 0 and imgdata[2] == 1:
        #draw.rectangle((imgdata[0][0],imgdata[0][1],imgdata[0][2],imgdata[0][3]),outline='green')
        img = draw_bbox(imgdata[0],img,'green')
        print imgdata
        tp += 1

      elif imgdata[1] > 0 and imgdata[2] == 0:
        fp += 1

      elif imgdata[1] <= 0 and imgdata[2] == 1:
        #draw.rectangle((imgdata[0][0],imgdata[0][1],imgdata[0][2],imgdata[0][3]),outline='red')
        img = draw_bbox(imgdata[0],img,'red')
        
        fn += 1
      elif imgdata[1] <= 0 and imgdata[2] == 0:
        tn += 1
        
    #print i
    #draw.rectangle((0,0,10,10),outline='red')
    print "ALL", all_objects[i]
    #print i
    #for ground in all_objects[i]:
      #print ground
    for gtruth in all_objects[i][0]:
      print gtruth
      ground_tuple = gtruth
      print "GROUND", ground_tuple
      #img = draw_bbox(ground_tuple,img,'blue')
    #img.save(os.path.join('res_img',all_objects[i][1]))
    
  print "Confusion Matrix"

  save_res.write(str(tp)+" "+str(fn)+"\n"+str(fp)+" "+str(tn)+"\n") 
  print tp,fn
  print fp,tn