Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. class DatasetPersonDetection(DatasetPerson):
  2.     """Dataset containing images from only one person with face detection"""
  3.  
  4.     def __init__(self, root_dir, transform=None):
  5.         """
  6.        :param root_dir: Directory with the images.
  7.        :param transform: Transformations applied to the images.
  8.        """
  9.         # don't transform the images -> transform=None
  10.         super().__init__(root_dir, transform=None)
  11.         self.transform = transform
  12.         self.detected_faces = []
  13.  
  14.         # run face detection on images
  15.         for img in self.images:
  16.             face_location = face_recognition.face_locations(img, model='hog')
  17.  
  18.             # ignore if 2 faces detected because in most cases they originate not form the same person
  19.             if face_location and len(face_location) == 1:
  20.                 top, right, bottom, left = face_location[0]
  21.                 cropped_img = img[top:bottom, left:right]
  22.  
  23.                 if self.transform:
  24.                     cropped_img = self.transform(cropped_img)
  25.  
  26.                 self.detected_faces.append(cropped_img)
  27.         self.images.clear()
  28.         self.images = self.detected_faces
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement