#!/usr/bin/env python
# encoding: utf-8

# The MIT License (MIT)

# Copyright (c) 2016 CNRS

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# AUTHORS
# Hervé BREDIN - http://herve.niderb.fr

"""Face tracking"""

from .face import Face
from .face import DLIB_SMALLEST_FACE
from ..tracking import TrackingByDetection


def get_face_detect(face):
    """Create function for face detection"""
    def face_detect(frame):
        """Detect face in frame"""
        for f in face.iterfaces(frame):
            yield (f.left(), f.top(), f.right(), f.bottom())
    return face_detect


class FaceTracking(TrackingByDetection):
    """Face tracking

    Parameters
    ----------
    smallest : int, optional
        Approximate size in pixel of the smallest face that should be
        detected. Defaults to 36.
    min_confidence : float, optional
        Kill trackers whose confidence goes below this value. Defaults to 10.
    min_overlap_ratio : float, optional
        Do not associate trackers and detections if their overlap ratio goes
        below this value. Defaults to 0.3.
    max_gap : float, optional
        Bridge gaps with duration shorter than this value.
    """
    def __init__(self, smallest=SMALLEST_DEFAULT,
                 min_confidence=10., min_overlap_ratio=0.3, max_gap=0.):

        face = Face(smallest=smallest)
        detect_func = get_face_detect(face)

        super(FaceTracking, self).__init__(
            detect_func=detect_func,
            min_confidence=min_confidence,
            min_overlap_ratio=min_overlap_ratio,
            max_gap=max_gap)

@@ -1679,21 +1679,30 @@
     
-smallest : in
+detect_min_size : floa
 t, o
@@ -1738,16 +1738,31 @@
 ize 
+(
 in 
-pixel
+video height ratio)
  of 
@@ -1783,16 +1783,24 @@
 ace that
+%0A       
  should 
@@ -1805,47 +1805,209 @@
 d be
-%0A        detected. Defaults to 36.%0A    
+ detected. Defaults to any face.%0A    detect_every : float, optional%0A        When provided, face detection is applied every %60detect_every%60 seconds.%0A        Defaults to processing every frame.%0A    track_
 min_
@@ -2113,24 +2113,30 @@
  to 10.%0A    
+track_
 min_overlap_
@@ -2279,24 +2279,30 @@
 to 0.3.%0A    
+track_
 max_gap : fl
@@ -2409,33 +2409,43 @@
 lf, 
-smallest=SMALLEST_DEFAULT
+detect_min_size=0., detect_every=0.
 ,%0A  
@@ -2451,32 +2451,38 @@
                 
+track_
 min_confidence=1
@@ -2484,17 +2484,22 @@
 nce=10.,
- 
+track_
 min_over
@@ -2512,17 +2512,40 @@
 tio=0.3,
- 
+%0A                 track_
 max_gap=
@@ -2574,25 +2574,8 @@
 ace(
-smallest=smallest
 )%0A  
@@ -2706,24 +2706,162 @@
             
+detect_smallest=DLIB_SMALLEST_FACE,%0A            detect_min_size=detect_min_size,%0A            detect_every=detect_every,%0A            track_
 min_confiden
@@ -2863,16 +2863,22 @@
 fidence=
+track_
 min_conf
@@ -2897,16 +2897,22 @@
         
+track_
 min_over
@@ -2921,16 +2921,22 @@
 p_ratio=
+track_
 min_over
@@ -2958,24 +2958,30 @@
         
+track_
 max_gap=
 max_gap)
@@ -2972,16 +2972,22 @@
 max_gap=
+track_
 max_gap)

update FaceTracking with new TrackingByDetection API