Fix path and update requirements

This commit is contained in:
Antonio J. Delgado 2021-09-06 23:41:35 +03:00
parent aa5a2d7c2e
commit 8b9ab3f6d2
2 changed files with 31 additions and 20 deletions

View file

@ -9,6 +9,7 @@ import sys
import os import os
import logging import logging
import json import json
import shutil
import click import click
import click_config_file import click_config_file
from logging.handlers import SysLogHandler from logging.handlers import SysLogHandler
@ -59,30 +60,41 @@ class image_classifier:
with os.scandir(directory) as directory_item: with os.scandir(directory) as directory_item:
for entry in directory_item: for entry in directory_item:
if not entry.name.startswith('.') and entry.is_file(): if not entry.name.startswith('.') and entry.is_file():
self.process_file(directory + os.sep + entry.name) self.process_file(os.path.joing(directory, entry.name))
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}'...")
people = self.find_faces(file) if not os.access(file, os.R_OK):
if people: self._log.error(f"The file '{file}' is not readable.")
self._log.debug(f"Found {len(people)} known people in the image.")
self._log.debug(json.dumps(people, indent=2))
self.metadata = pyexiv2.ImageMetadata(file)
self.metadata.read()
#self.print_metadata()
if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys:
print(f"People (before): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
self.append_people(people)
print(f"People (after): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
self.metadata.write()
self._log.debug(f"Updated file '{file}'.")
if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys:
self._log.debug(f"File time stamp: {self.metadata['Exif.Photo.DateTimeOriginal'].raw_value} (type: {type(self.metadata['Exif.Photo.DateTimeOriginal'].raw_value)})")
# move to destination
else: else:
self._log.debug("Doesn't seem to be an image.") people = self.find_faces(file)
if people:
self._log.debug(f"Found {len(people)} known people in the image.")
self._log.debug(json.dumps(people, indent=2))
self.metadata = pyexiv2.ImageMetadata(file)
self.metadata.read()
#self.print_metadata()
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(people)
self._log.debug(f"People (after): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
self.metadata.write()
self._log.debug(f"Updated file '{file}'.")
if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys:
original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value
self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})")
dirname = os.path.dirname(os.path.realpath(file))
filename = os.path.basename(file)
new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename)
self._log.debug(f"New path: {new_path}")
os.makedirs(os.path.dirname(new_path), exist_ok=True)
# if new_path:
# shutil.move(file, new_path)
# move to destination
else:
self._log.debug("Doesn't seem to be an image.")
def print_metadata(self): def print_metadata(self):
print("IPTC keys:") print("IPTC keys:")

View file

@ -1,5 +1,4 @@
click click
click_config_file click_config_file
face_recognition face_recognition
exif py3exiv2
iptcinfo3