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 = checker.getTruthDict(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 = checker.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 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:
        print anno_contents[j], image_names[j]
        # 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 main(test_dir, anno_dir, c1, obj_type, step_size, savename):
    save_res = open(os.path.join("results", savename + "_NewSetConfusion.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 = checker.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)):
        print "image? ", testImages
        img = Image.open(os.path.join(test_dir, testSet[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