split processing metadata

This commit is contained in:
Antonio J. Delgado 2022-05-02 16:36:05 +03:00
parent aef12e32de
commit d84472b95e

View file

@ -69,34 +69,37 @@ class image_classifier:
if not entry.name.startswith('.') and entry.is_file(): if not entry.name.startswith('.') and entry.is_file():
self.process_file(os.path.join(directory, entry.name)) self.process_file(os.path.join(directory, entry.name))
def process_metadata(self, file):
self.metadata = pyexiv2.ImageMetadata(file)
self.metadata.read()
if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys:
self._log.debug(f"People (before): \
{self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} \
(type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
def process_file(self, file): def process_file(self, file):
''' Process a file, find faces, add EXIF information and ''' Process a file, find faces, add EXIF information and
move it to the folder of the day''' move it to the folder of the day'''
self._log.debug(f"Looking for faces in file '{file}'...") self._log.debug(f"Looking for faces in file '{file}'...")
folder_date = None folder_date = 'unknown-time'
dirname = os.path.dirname(os.path.realpath(file)) dirname = os.path.dirname(os.path.realpath(file))
filename = os.path.basename(file) filename = os.path.basename(file)
if not os.access(file, os.R_OK): if not os.access(file, os.R_OK):
self._log.error(f"The file '{file}' is not readable.") self._log.error(f"The file '{file}' is not readable.")
else: else:
if self.is_image(file): if self.is_image(file):
self.metadata = pyexiv2.ImageMetadata(file) self.metadata = self.process_metadata(file)
self.metadata.read()
people = self.find_faces(file) people = self.find_faces(file)
if people: if people:
self._log.debug(f"Found {len(people)} known people in the image.") self._log.debug(f"Found {len(people)} known people in the image.")
self._log.debug(json.dumps(people, indent=2)) self._log.debug(json.dumps(people, indent=2))
if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys:
self._log.debug(f"People (before): \
{self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} \
(type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
self.append_people(file, people) self.append_people(file, people)
if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys: if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys:
original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value
self._log.debug(f"File creation time: {original_date} \ self._log.debug(f"File creation time: {original_date} \
(type: {type(original_date)})") (type: {type(original_date)})")
folder_date = original_date.strftime('%Y.%m.%d') folder_date = original_date.strftime('%Y.%m.%d')
if not folder_date: if folder_date == 'unknown-time':
match = re.search(r'(?P<year>20[0-9]{2})[\-/\._]?\ match = re.search(r'(?P<year>20[0-9]{2})[\-/\._]?\
(?P<month>[0-1]?[0-9])[\-/\._]?(?P<day>[0-3]?[0-9])', filename) (?P<month>[0-1]?[0-9])[\-/\._]?(?P<day>[0-3]?[0-9])', filename)
if match: if match:
@ -108,8 +111,6 @@ class image_classifier:
if match: if match:
folder_date = f"{match.group('year')}.{match.group('month')}.\ folder_date = f"{match.group('year')}.{match.group('month')}.\
{match.group('day')}" {match.group('day')}"
else:
folder_date = 'unknown-time'
folder = os.path.join(dirname, folder_date, filename) folder = os.path.join(dirname, folder_date, filename)
self._log.debug(f"Time based folder name section '{folder_date}'") self._log.debug(f"Time based folder name section '{folder_date}'")
new_path = os.path.dirname(folder) new_path = os.path.dirname(folder)