class_docstr: MTCNN face detection module.
  
  This class loads pretrained P-, R-, and O-nets and returns images cropped to include the face
  only, given raw input images of one of the following types:
      - PIL image or list of PIL images
      - numpy.ndarray (uint8) representing either a single image (3D) or a batch of images (4D).
  Cropped faces can optionally be saved to file
  also.
  
  Keyword Arguments:
      image_size {int} -- Output image size in pixels. The image will be square. (default: {160})
      margin {int} -- Margin to add to bounding box, in terms of pixels in the final image. 
          Note that the application of the margin differs slightly from the davidsandberg/facenet
          repo, which applies the margin to the original image before resizing, making the margin
          dependent on the original image size (this is a bug in davidsandberg/facenet).
          (default: {0})
      min_face_size {int} -- Minimum face size to search for. (default: {20})
      thresholds {list} -- MTCNN face detection thresholds (default: {[0.6, 0.7, 0.7]})
      factor {float} -- Factor used to create a scaling pyramid of face sizes. (default: {0.709})
      post_process {bool} -- Whether or not to post process images tensors before returning.
          (default: {True})
      select_largest {bool} -- If True, if multiple faces are detected, the largest is returned.
          If False, the face with the highest detection probability is returned.
          (default: {True})
      selection_method {string} -- Which heuristic to use for selection. Default None. If
          specified, will override select_largest:
                  "probability": highest probability selected
                  "largest": largest box selected
                  "largest_over_threshold": largest box over a certain probability selected
                  "center_weighted_size": box size minus weighted squared offset from image center
              (default: {None})
      keep_all {bool} -- If True, all detected faces are returned, in the order dictated by the
          select_largest parameter. If a save_path is specified, the first face is saved to that
          path and the remaining faces are saved to <save_path>1, <save_path>2 etc.
          (default: {False})
      device {torch.device} -- The device on which to run neural net passes. Image tensors and
          models are copied to this device before running forward passes. (default: {None})
class_docstr_tok: ["MTCNN","face","detection","module",".","This","class","loads","pretrained","P-",",","R-",",","and","O-nets","and","returns","images","cropped","to","include","the","face","only",",","given","raw","input","images","of","one","of","the","following","types",":","-","PIL","image","or","list","of","PIL","images","-","numpy.ndarray","(","uint8",")","representing","either","a","single","image","(","3D",")","or","a","batch","of","images","(","4D",")",".","Cropped","faces","can","optionally","be","saved","to","file","also",".","Keyword","Arguments",":","image_size","{","int","}","--","Output","image","size","in","pixels",".","The","image","will","be","square",".","(","default",":","{","160","}",")","margin","{","int","}","--","Margin","to","add","to","bounding","box",",","in","terms","of","pixels","in","the","final","image",".","Note","that","the","application","of","the","margin","differs","slightly","from","the","davidsandberg\/facenet","repo",",","which","applies","the","margin","to","the","original","image","before","resizing",",","making","the","margin","dependent","on","the","original","image","size","(","this","is","a","bug","in","davidsandberg\/facenet",")",".","(","default",":","{","0","}",")","min_face_size","{","int","}","--","Minimum","face","size","to","search","for",".","(","default",":","{","20","}",")","thresholds","{","list","}","--","MTCNN","face","detection","thresholds","(","default",":","{","[","0.6",",","0.7",",","0.7","]","}",")","factor","{","float","}","--","Factor","used","to","create","a","scaling","pyramid","of","face","sizes",".","(","default",":","{","0.709","}",")","post_process","{","bool","}","--","Whether","or","not","to","post","process","images","tensors","before","returning",".","(","default",":","{","True","}",")","select_largest","{","bool","}","--","If","True",",","if","multiple","faces","are","detected",",","the","largest","is","returned",".","If","False",",","the","face","with","the","highest","detection","probability","is","returned",".","(","default",":","{","True","}",")","selection_method","{","string","}","--","Which","heuristic","to","use","for","selection",".","Default","None",".","If","specified",",","will","override","select_largest",":","``","probability","''",":","highest","probability","selected","``","largest","''",":","largest","box","selected","``","largest_over_threshold","''",":","largest","box","over","a","certain","probability","selected","``","center_weighted_size","''",":","box","size","minus","weighted","squared","offset","from","image","center","(","default",":","{","None","}",")","keep_all","{","bool","}","--","If","True",",","all","detected","faces","are","returned",",","in","the","order","dictated","by","the","select_largest","parameter",".","If","a","save_path","is","specified",",","the","first","face","is","saved","to","that","path","and","the","remaining","faces","are","saved","to","<","save_path",">","1",",","<","save_path",">","2","etc",".","(","default",":","{","False","}",")","device","{","torch.device","}","--","The","device","on","which","to","run","neural","net","passes",".","Image","tensors","and","models","are","copied","to","this","device","before","running","forward","passes",".","(","default",":","{","None","}",")"]
docstr: Detect all faces in PIL image and return bounding boxes and optional facial landmarks.
  
  This method is used by the forward method and is also useful for face detection tasks
  that require lower-level handling of bounding boxes and facial landmarks (e.g., face
  tracking). The functionality of the forward function can be emulated by using this method
  followed by the extract_face() function.
  
  Arguments:
      img {PIL.Image, np.ndarray, or list} -- A PIL image, np.ndarray, torch.Tensor, or list.
  
  Keyword Arguments:
      landmarks {bool} -- Whether to return facial landmarks in addition to bounding boxes.
          (default: {False})
  
  Returns:
      tuple(numpy.ndarray, list) -- For N detected faces, a tuple containing an
          Nx4 array of bounding boxes and a length N list of detection probabilities.
          Returned boxes will be sorted in descending order by detection probability if
          self.select_largest=False, otherwise the largest face will be returned first.
          If `img` is a list of images, the items returned have an extra dimension
          (batch) as the first dimension. Optionally, a third item, the facial landmarks,
          are returned if `landmarks=True`.
  
  Example:
  >>> from PIL import Image, ImageDraw
  >>> from facenet_pytorch import MTCNN, extract_face
  >>> mtcnn = MTCNN(keep_all=True)
  >>> boxes, probs, points = mtcnn.detect(img, landmarks=True)
  >>> # Draw boxes and save faces
  >>> img_draw = img.copy()
  >>> draw = ImageDraw.Draw(img_draw)
  >>> for i, (box, point) in enumerate(zip(boxes, points)):
  ...     draw.rectangle(box.tolist(), width=5)
  ...     for p in point:
  ...         draw.rectangle((p - 10).tolist() + (p + 10).tolist(), width=10)
  ...     extract_face(img, box, save_path='detected_face_{}.png'.format(i))
  >>> img_draw.save('annotated_faces.png')
docstr_tok: ["Detect","all","faces","in","PIL","image","and","return","bounding","boxes","and","optional","facial","landmarks",".","This","method","is","used","by","the","forward","method","and","is","also","useful","for","face","detection","tasks","that","require","lower-level","handling","of","bounding","boxes","and","facial","landmarks","(","e.g.",",","face","tracking",")",".","The","functionality","of","the","forward","function","can","be","emulated","by","using","this","method","followed","by","the","extract_face","(",")","function",".","Arguments",":","img","{","PIL.Image",",","np.ndarray",",","or","list","}","--","A","PIL","image",",","np.ndarray",",","torch.Tensor",",","or","list",".","Keyword","Arguments",":","landmarks","{","bool","}","--","Whether","to","return","facial","landmarks","in","addition","to","bounding","boxes",".","(","default",":","{","False","}",")","Returns",":","tuple","(","numpy.ndarray",",","list",")","--","For","N","detected","faces",",","a","tuple","containing","an","Nx4","array","of","bounding","boxes","and","a","length","N","list","of","detection","probabilities",".","Returned","boxes","will","be","sorted","in","descending","order","by","detection","probability","if","self.select_largest=False",",","otherwise","the","largest","face","will","be","returned","first",".","If","`","img","`","is","a","list","of","images",",","the","items","returned","have","an","extra","dimension","(","batch",")","as","the","first","dimension",".","Optionally",",","a","third","item",",","the","facial","landmarks",",","are","returned","if","`","landmarks=True","`",".","Example",":",">",">",">","from","PIL","import","Image",",","ImageDraw",">",">",">","from","facenet_pytorch","import","MTCNN",",","extract_face",">",">",">","mtcnn","=","MTCNN","(","keep_all=True",")",">",">",">","boxes",",","probs",",","points","=","mtcnn.detect","(","img",",","landmarks=True",")",">",">",">","#","Draw","boxes","and","save","faces",">",">",">","img_draw","=","img.copy","(",")",">",">",">","draw","=","ImageDraw.Draw","(","img_draw",")",">",">",">","for","i",",","(","box",",","point",")","in","enumerate","(","zip","(","boxes",",","points",")",")",":","...","draw.rectangle","(","box.tolist","(",")",",","width=5",")","...","for","p","in","point",":","...","draw.rectangle","(","(","p","-","10",")",".tolist","(",")","+","(","p","+","10",")",".tolist","(",")",",","width=10",")","...","extract_face","(","img",",","box",",","save_path='detected_face_","{","}",".png'.format","(","i",")",")",">",">",">","img_draw.save","(","'annotated_faces.png","'",")"]
code: def detect(self, img, landmarks=False):
      """Detect all faces in PIL image and return bounding boxes and optional facial landmarks.
  
      This method is used by the forward method and is also useful for face detection tasks
      that require lower-level handling of bounding boxes and facial landmarks (e.g., face
      tracking). The functionality of the forward function can be emulated by using this method
      followed by the extract_face() function.
  
      Arguments:
          img {PIL.Image, np.ndarray, or list} -- A PIL image, np.ndarray, torch.Tensor, or list.
  
      Keyword Arguments:
          landmarks {bool} -- Whether to return facial landmarks in addition to bounding boxes.
              (default: {False})
  
      Returns:
          tuple(numpy.ndarray, list) -- For N detected faces, a tuple containing an
              Nx4 array of bounding boxes and a length N list of detection probabilities.
              Returned boxes will be sorted in descending order by detection probability if
              self.select_largest=False, otherwise the largest face will be returned first.
              If `img` is a list of images, the items returned have an extra dimension
              (batch) as the first dimension. Optionally, a third item, the facial landmarks,
              are returned if `landmarks=True`.
  
      Example:
      >>> from PIL import Image, ImageDraw
      >>> from facenet_pytorch import MTCNN, extract_face
      >>> mtcnn = MTCNN(keep_all=True)
      >>> boxes, probs, points = mtcnn.detect(img, landmarks=True)
      >>> # Draw boxes and save faces
      >>> img_draw = img.copy()
      >>> draw = ImageDraw.Draw(img_draw)
      >>> for i, (box, point) in enumerate(zip(boxes, points)):
      ...     draw.rectangle(box.tolist(), width=5)
      ...     for p in point:
      ...         draw.rectangle((p - 10).tolist() + (p + 10).tolist(), width=10)
      ...     extract_face(img, box, save_path='detected_face_{}.png'.format(i))
      >>> img_draw.save('annotated_faces.png')
      """
  
      with torch.no_grad():
          batch_boxes, batch_points = detect_face(
              img,
              self.min_face_size,
              self.pnet,
              self.rnet,
              self.onet,
              self.thresholds,
              self.factor,
              self.device,
          )
  
      boxes, probs, points = [], [], []
      for box, point in zip(batch_boxes, batch_points):
          box = np.array(box)
          point = np.array(point)
          if len(box) == 0:
              boxes.append(None)
              probs.append([None])
              points.append(None)
          elif self.select_largest:
              box_order = np.argsort(
                  (box[:, 2] - box[:, 0]) * (box[:, 3] - box[:, 1])
              )[::-1]
              box = box[box_order]
              point = point[box_order]
              boxes.append(box[:, :4])
              probs.append(box[:, 4])
              points.append(point)
          else:
              boxes.append(box[:, :4])
              probs.append(box[:, 4])
              points.append(point)
      boxes = np.array(boxes, dtype=object)
      probs = np.array(probs, dtype=object)
      points = np.array(points, dtype=object)
  
      if (
          not isinstance(img, (list, tuple))
          and not (isinstance(img, np.ndarray) and len(img.shape) == 4)
          and not (
              isinstance(img, torch.Tensor) and len(img.shape) == 4
          )
      ):
          boxes = boxes[0]
          probs = probs[0]
          points = points[0]
  
      if landmarks:
          return boxes, probs, points
  
      return boxes, probs
  
imports: import torch
  from torch import nn
  import numpy
  import os
  from .utils.detect_face import detect_face, extract_face
label_desc: Use image node_id 3 for calling the MTCNN obj's underlying member method code with example usage: obj.detect(img, landmarks) and returns: boxes,  probs, boxes,  probs,  points