def main(args):
  classifier = CamClassifier()
  classifier.set_batch_size(1)  # TODO: Batch if its running to slowly.

  with open(args.dataset) as dataset:
    labeled_patches = [line.split() for line in dataset]
    labeled_patches = [(x[0], int(x[1])) for x in labeled_patches]

  num_labels = {label: 0 for label in K_LABELS}
  num_classified = {label: 0 for label in K_LABELS}
  num_correct = {label: 0 for label in K_LABELS}
  num_present = {label: 0 for label in K_LABELS}

  confusion = {}
  for label in K_LABELS:
    confusion[label] = {}
    for label2 in K_LABELS:
      confusion[label][label2] = 0

  counter = 0
  for patch, label in labeled_patches:
    print patch
    counter += 1
    # print counter
    patch = cv2.imread(patch, cv2.IMREAD_COLOR)
    label = K_LABELS[label]
    if patch is None:
      continue
    classification = classify(classifier, patch)

    confusion[classification][label] += 1

    print label, classification
    num_labels[label] += 1
    num_classified[classification] += 1
    if label == classification:
      num_correct[classification] += 1
    if classification != 'noise':
      num_present[label] += 1


  print num_correct
  print num_classified
  print num_labels
  for label in K_LABELS:
    num_classified[label] = max(1, num_classified[label])
    num_labels[label] = max(1, num_labels[label])

  precision = {l: num_correct[l] / float(num_classified[l]) for l in K_LABELS}
  recall = {l: num_correct[l] / float(num_labels[l]) for l in K_LABELS}

  for label in K_LABELS:
    print ("%s\n\tprecision: %f\n\trecall: %f" % (label, precision[label], recall[label]))

  num_present['animal'] = 0
  total_present = sum(num_present.values())
  for label in K_LABELS:
    print "%s filter precentage: %f" % (label, num_present[label]/float(total_present))

  print confusion
from detector.Webcam import Webcam
from classify.classifier import ReferenceClassifier
from classify.classifier import CamClassifier

parser = argparse.ArgumentParser()
parser.add_argument("frames_dir",
    help = "Directory containing webcam image folders")
parser.add_argument("num_webcams",
    help = "Number of webcams to sample", type = int)
args = parser.parse_args()

frames_dir = args.frames_dir
num_webcams = args.num_webcams

wc_paths = [os.path.join(frames_dir, fn) for fn in os.listdir(frames_dir)]
wc_paths = random.sample(wc_paths, num_webcams)


classifier = CamClassifier()
classifier.set_batch_size(1)

while True:
  for path in wc_paths:
    cam = Webcam(online=False, path=path)
    for i in range(5):
      cam.update()
    overlay = cam.filtered_overlay(classifier, squareSize=227)
    if overlay is not None:
      cv2.imshow('overlay', overlay)
      cv2.waitKey(0)