def __annotate(self, frame, response):
     AnnotationParserInstance = AnnotationParser()
     #TODO: Make the choice of the service configurable
     listOfRectanglesToDisplay = AnnotationParserInstance.getCV2RectanglesFromProcessingService1(response)
     for rectangle in listOfRectanglesToDisplay:
         cv2.rectangle(frame, (rectangle(0), rectangle(1)), (rectangle(2), rectangle(3)), (0,0,255),4)
     return
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_C2.txt'),'wb')
    save_res.write('Average Number of annotations per image: '+str(runningTotal)+'\n') 
    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)
    # get all ground truths for the test set
    for test in test_set:
      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 = windowing.main(c1,os.path.join(test_dir,test),step_size,9,True)
      test_res.append(results[0])
    
    print test_res
    
    test_res = flattenResults(test_res)
    calcPrecisionRecall(test_res,truthDict,test_dir,savename,save_res)
def testdb_loader(test_dir,anno_dir,obj_type):

  anno_contents = os.listdir(anno_dir)
  image_contents = os.listdir(test_dir)
  # remove the ending tag value so we can easily look up the name of the file
  image_names = [os.path.splitext(x)[0] for x in image_contents]
  test_set = list()
  i = 0
  j = 0
  runningTotal = 0
  setSize = 100
  while i < setSize:
    
    num = len(resultcheck.getGroundTruths(image_contents[j],test_dir,anno_dir,obj_type)) # old call
    #num = len(checker.getGroundTruths(anno_contents[j],test_dir,anno_dir,obj_type.strip())) #new call
    runningTotal = runningTotal + num
    print "Number of objects",num,i
    if num > 0:
      print "adding"
      #annoTmp = os.path.splitext(anno_contents[j])[0]
      #print annoTmp,image_contents[image_names.index(annoTmp)]
      #test_set.append(image_contents[image_names.index(annoTmp)])
      test_set.append(image_contents[j])
      i += 1
    j += 1
  print runningTotal,setSize,i
  return test_set,float(runningTotal)/setSize
def calcPrecisionRecall(flatList,truthDict,test_dir,savename,save_res):
  sortedDetections = sorted(flatList,key=lambda plot: plot[1],reverse=True)
  # get the number of objects in the set
  total = 0
  for x in truthDict:
    total += len(truthDict[x])
    
  print "Total number of objects", total
  
  td = 0
  fd = 0
  precision = list()
  recall = list()
  dvals = list()
  for elm in sortedDetections:
    print elm
    image = elm[2]
    dval = elm[1]
    detection = elm[0]
    print detection
    groundTruth = truthDict[image]
    # check the detection of this element
    img = Image.open(os.path.join(test_dir,image))
    res = resultcheck.test_detection(groundTruth,detection,img,image,dval)
    td += res[0]
    fd += res[1]
    img = res[2]
    p = float(td)/float(td+fd)
    r = float(td)/float(total)
    precision.append(p)
    recall.append(r)
    dvals.append(dval)
    save_res.write(str(p) +" "+str(r)+' '+str(dval)+"\n")
  pl.plot(recall,precision)
  pl.title("Precision Recall Graph C2 Cars")
  pl.xlabel("Recall")
  pl.ylabel("Precision")
  pl.xlim([0.0,1.0])
  pl.ylim([0.0,1.0])
  pl.savefig(savename+"_C2.png")
  print precision
  print recall
def testdb_loader(test_dir,anno_dir,obj_type):

  contents = os.listdir(test_dir)
  test_set = list()
  test_img = list()
  i = 0
  j = 0

  while i < 15:

    num = len(resultcheck.getGroundTruths(contents[j],test_dir,anno_dir,obj_type))
    print "Number of objects",num
    if num > 0:
      print "ADD",num
      test_set.append(contents[j])
      test_img.append(contents[j])
      i += 1
    j += 1

  return test_set,test_img
def testdb_loader(test_dir,anno_dir,obj_type):

  contents = os.listdir(test_dir)
  test_set = list()
  i = 0
  j = 0
  runningTotal = 0
  setSize = 100
  while i < setSize:

    num = len(resultcheck.getGroundTruths(contents[j],test_dir,anno_dir,obj_type))
    #num = checker.getGroundTruths(contents[i],test_dir,anno_dir,obj_type)
    runningTotal = runningTotal + num
    print "Number of objects",num
    if num > 0:
      print "adding"
      test_set.append(contents[j])
      i += 1
    j += 1
  print runningTotal,setSize,i
  return test_set,float(runningTotal)/setSize
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
def loadTestSet(test_dir,anno_dir,obj_type):
  
  contents = os.listdir(test_dir)
  test_set = list()
  
  an.getTruthLocations(test_set,test_dir,anno_dir)