Add check for image files

This commit is contained in:
Antonio J. Delgado 2021-09-08 14:36:28 +03:00
parent 9bfb54e1b1
commit 7422b25bf6

View file

@ -148,7 +148,7 @@ class image_classifier:
if os.access(self.faces_directory, os.R_OK):
with os.scandir(self.faces_directory) as faces_items:
for entry in faces_items:
if not entry.name.startswith('.') and entry.is_file():
if not entry.name.startswith('.') and entry.is_file() and self.is_image(self.faces_directory + os.sep + entry.name):
self._log.debug(f"Identifying face in file '{entry.name}'...")
person = dict()
person['filename'] = face_recognition.load_image_file(self.faces_directory + os.sep + entry.name)
@ -164,7 +164,7 @@ class image_classifier:
def find_faces(self, file):
''' Find faces in an image/video file '''
people = list()
try:
if self.is_image(file):
image = face_recognition.load_image_file(file)
encodings = face_recognition.face_encodings(image)
self._log.debug(f"Found {len(encodings)} faces.")
@ -173,11 +173,18 @@ class image_classifier:
if face_recognition.compare_faces([known_person['encoding']], encoding)[0]:
if known_person['name'] not in people:
people.append(known_person['name'])
except PIL.UnidentifiedImageError as error:
self._log.debug(f"File '{file}' don't seem to be an image.")
else:
return False
return people
def is_image(self, file):
try:
im = PIL.Image.open(file)
except PIL.UnidentifiedImageError as error:
self._log.debug(f"File '{file}' is not an image recognizable by PIL. {error}")
return False
return True
def _init_log(self):
''' Initialize log object '''
self._log = logging.getLogger("image_classifier")