#!/usr/bin/python

# face_detect.py

# Face Detection using OpenCV. Based on sample code from:
# http://python.pastebin.com/m76db1d6b

# Usage: python face_detect.py <image_file>

import sys, os
from opencv.cv import *
from opencv.highgui import *

def detectObjects(image, img_name, output):
  """Converts an image to grayscale and prints the locations of any
     faces found"""
  grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
  cvCvtColor(image, grayscale, CV_BGR2GRAY)

  storage = cvCreateMemStorage(0)
  cvClearMemStorage(storage)
  cvEqualizeHist(grayscale, grayscale)
  cascade = cvLoadHaarClassifierCascade(
    '/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
    cvSize(1,1))
  faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
                             CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50))

  if faces:
    print("convert %s -stroke blue -strokewidth 2 -fill none -draw \"rectangle %d,%d %d,%d\" %s" % (img_name, faces[0].x, faces[0].y, faces[0].x+faces[0].width, faces[0].y+faces[0].height, output))
    for i in range(1, faces.total):
      print("convert %s -stroke blue -strokewidth 2 -fill none -draw \"rectangle %d,%d %d,%d\" %s" % (output, faces[i].x, faces[i].y, faces[i].x+faces[i].width, faces[i].y+faces[i].height, output))

  # cascade = cvLoadHaarClassifierCascade(
  #   '/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml',
  #   cvSize(1,1))
  # faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
  #                            CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50))

  # if faces:
  #   for f in faces:
  #     print("convert %s -stroke green -fill none -draw \"rectangle %d,%d %d,%d\" %s" % (output, f.x, f.y, f.x+f.width, f.y+f.height, output))

  # cascade = cvLoadHaarClassifierCascade(
  #   '/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml',
  #   cvSize(1,1))
  # faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
  #                            CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50))

  # if faces:
  #   for f in faces:
  #     print("convert %s -stroke blue -fill none -draw \"rectangle %d,%d %d,%d\" %s" % (output, f.x, f.y, f.x+f.width, f.y+f.height, output))

  # cascade = cvLoadHaarClassifierCascade(
  #   '/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml',
  #   cvSize(1,1))
  # faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
  #                            CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50))

  # if faces:
  #   for f in faces:
  #     print("convert %s -stroke yellow -fill none -draw \"rectangle %d,%d %d,%d\" %s" % (output, f.x, f.y, f.x+f.width, f.y+f.height, output))

  # cascade = cvLoadHaarClassifierCascade(
  #   '/usr/share/opencv/haarcascades/haarcascade_profileface.xml',
  #   cvSize(1,1))
  # faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
  #                            CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50))

  # if faces:
  #   for f in faces:
      # print("convert %s -stroke purple -fill none -draw \"rectangle %d,%d %d,%d\" %s" % (output, f.x, f.y, f.x+f.width, f.y+f.height, output))

def main(img_name, output):
  img_name = sys.argv[1]
  output = sys.argv[2]
  image = cvLoadImage(sys.argv[1]);
  detectObjects(image, img_name, output)

img_name = '<EMPTY>'
output = 'output.jpg'

if __name__ == "__main__":
  main(img_name, output)
