new_contents: #!/usr/bin/python
  import sys
  import time
  import datetime
  import cv2.cv as cv
  from optparse import OptionParser
  from subprocess import Popen, PIPE
  
  # Parameters for haar detection
  # From the API:
  # The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned 
  # for accurate yet slow object detection. For a faster operation on real video 
  # images the settings are: 
  # scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, 
  # min_size=<minimum possible face size
  scale_factor = 1.2
  min_size = (20, 20)
  image_scale = 2
  haar_scale = 1.2
  min_neighbors = 2
  haar_flags = 0
  #flags=CV_HAAR_DO_CANNY_PRUNING
  
  def detect_and_draw(img, cascade):
      # allocate temporary images
      gray = cv.CreateImage((img.width,img.height), 8, 1)
      small_img = cv.CreateImage((cv.Round(img.width / image_scale),
  			       cv.Round (img.height / image_scale)), 8, 1)
  
      # convert color input image to grayscale
      cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
  
      # scale input image for faster processing
      cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
  
      cv.EqualizeHist(small_img, small_img)
  
      if(cascade):
          t = cv.GetTickCount()
          faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                       haar_scale, min_neighbors, haar_flags, min_size)
          if faces:
              for ((x, y, w, h), n) in faces:
                  pt1 = (int(x * image_scale), int(y * image_scale))
                  pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                  now = datetime.datetime.now()
                  filename = str(now.minute)+str(now.hour)+str(now.second)+str(".jpg")
                  cv.SaveImage(filename, img)
                  p = Popen(["python", "upload.py", filename], stdin=PIPE, stdout=PIPE)
  		output = p.communicate(input)[0]
                  
      cv.ShowImage("multify_identify", img)
  
  if __name__ == '__main__':
  
      parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
      parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
      (options, args) = parser.parse_args()
  
      cascade = cv.Load(options.cascade)
      
      if len(args) != 1:
          parser.print_help()
          sys.exit(1)
  
      input_name = args[0]
      if input_name.isdigit():
          capture = cv.CreateCameraCapture(int(input_name))
      else:
          capture = None
  
      cv.NamedWindow("multify_identify", 1)
  
      width = 600 #leave None for auto-detection
      height = 520 #leave None for auto-detection
  
      if width is None:
      	width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
      else:
      	cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)    
  
      if height is None:
          height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
      else:
          cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height) 
  
      if capture:
          frame_copy = None
          while True:
  
              frame = cv.QueryFrame(capture)
              if not frame:
                  cv.WaitKey(0)
                  break
              if not frame_copy:
                  frame_copy = cv.CreateImage((frame.width,frame.height),
                                              cv.IPL_DEPTH_8U, frame.nChannels)
  
  #                frame_copy = cv.CreateImage((frame.width,frame.height),
  #                                            cv.IPL_DEPTH_8U, frame.nChannels)
  
              if frame.origin == cv.IPL_ORIGIN_TL:
                  cv.Copy(frame, frame_copy)
              else:
                  cv.Flip(frame, frame_copy, 0)
              
              detect_and_draw(frame_copy, cascade)
  
              if cv.WaitKey(10) >= 0:
                  break
      else:
          image = cv.LoadImage(input_name, 1)
          detect_and_draw(image, cascade)
          cv.WaitKey(0)
      session.quit()
      cv.DestroyWindow("multify_identify")
  
subject: Create facedetect.py