Ejemplo n.º 1
0
 def __get_media_from_folder(self, folder):
     files = tools.list_files(
         folder,
         tools.IMAGE_EXTS + tools.VIDEO_EXTS,
         self.__nomedia_files)
     files.sort()
     return files
Ejemplo n.º 2
0
 def update_filepaths(self, oldfolder, newfolder):
     newfiles = self.__filenames_to_dict(
         tools.list_files(newfolder, tools.IMAGE_EXTS))
     oldfiles = self.__filenames_to_dict(self.get_files(oldfolder))
     for name, oldpath in oldfiles.items():
         if name not in newfiles:
             old = os.path.join(oldpath, name)
             log.info(f'removing unexists file: {old}')
             self.remove(old, commit=False)
         elif newfiles[name] != oldpath:
             old = os.path.join(oldpath, name)
             new = os.path.join(newfiles[name], name)
             log.info(f'move file {old} to {new}')
             self.move(old, new, commit=False)
     self.commit()
Ejemplo n.º 3
0
    def __list_cache(self, params):
        cache_path = self.server.face_cache_path()
        if self.server.cdb() is not None:
            image_files = self.server.cdb().list_cache()
        else:
            image_files = tools.list_files(cache_path, tools.IMAGE_EXTS)

        result = collections.defaultdict(lambda: [])
        for (i, image_file) in enumerate(image_files):
            name = image_file.split(os.path.sep)[-2]
            result[name].append(os.path.relpath(image_file, cache_path))

        if 'sort' in params and params['sort'][0] == 'date':

            def key(s):
                return re.sub('_\d\d\d_', '_', s)
        else:
            key = None
        res_list = [(k, sorted(r, key=key)) for k, r in result.items()]
        res_list.sort()

        self.__ok_response(res_list)
Ejemplo n.º 4
0
    def generate(self, regenerate=False):
        log.info(f'Patterns generation: {self.__folder} ({regenerate})')

        image_files = {}
        for image_file in tools.list_files(self.__folder, tools.IMAGE_EXTS):
            if os.path.split(image_file)[1] == FACE_FILENAME:
                continue
            image_files[image_file] = os.stat(image_file).st_mtime

        if not regenerate:
            self.load()
            filtered = {}
            for image_file in image_files:
                filename_exsists = self.relpath(image_file) in self.__files
                if not filename_exsists or \
                   self.__files[self.relpath(image_file)][self.FILES_TIME] != \
                   image_files[image_file]:

                    filtered[image_file] = image_files[image_file]
                    if filename_exsists:
                        self.__remove_file(image_file)

            if len(filtered) == 0:
                log.info('Nothing changed')
                return

            image_files = filtered

        for (i, image_file) in enumerate(image_files):
            splitted = image_file.split(os.path.sep)
            name = splitted[-2]
            if name == BAD_FOLDERNAME:
                name = splitted[-3]
                tp = PATTERN_TYPE_BAD
            elif name == OTHER_FOLDERNAME:
                name = splitted[-3]
                tp = PATTERN_TYPE_OTHER
            else:
                tp = PATTERN_TYPE_GOOD

            log.info(f'{i + 1}/{len(image_files)} file: {image_file}')

            descr, thumbnail = tools.load_face_description(image_file)
            try:
                encoding = descr['encoding']
            except Exception:
                encoding = None

            if encoding is None:
                import face_recognition
                try:
                    image = tools.read_image(image_file, self.__max_size)
                except Exception:
                    log.exception(f'read_image failed')
                    continue

                boxes = face_recognition.face_locations(image,
                                                        model=self.__model)
                if len(boxes) != 1:
                    log.warning(
                        f'{len(boxes)} faces detected in {image_file}. Skip.')
                    continue
                encodings, landmarks = \
                    self.__get_encoder().encode(image, boxes)
                if not tools.test_landmarks(landmarks[0]):
                    log.warning(f'bad face detected in {image_file}. Skip.')
                    continue

                encoding = encodings[0]

            self.__files[self.relpath(image_file)] = \
                [encoding,
                 name,
                 image_files[image_file],
                 tp]

        self.__init_basenames()
        self.__persons = self.__calc_persons()
        self.__save()