Full change to py3exiv2

This commit is contained in:
Antonio J. Delgado 2021-09-06 23:08:52 +03:00
parent 460becbb64
commit aa5a2d7c2e

View file

@ -64,37 +64,50 @@ class image_classifier:
def process_file(self, file):
''' Process a file, find faces, add EXIF information and
move it to the folder of the day'''
self._log.debug(f"Processing file '{file}'...")
self._log.debug(f"Looking for faces in file '{file}'...")
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()
print(f"IPTC keys: {self.metadata.iptc_keys}")
print(f"EXIF keys: {self.metadata.exif_keys}")
print(f"XMP keys: {self.metadata.xmp_keys}")
#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}'.")
# get date
self._log.debug(f"File time stamp: {self.iptc_info['Image timestamp']} (type: {type(self.iptc_info['Image timestamp'])})")
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:
self._log.debug("Doesn't seem to be an image.")
def print_metadata(self):
print("IPTC keys:")
for key in self.metadata.iptc_keys:
print(f" {key}: '{self.metadata[key].raw_value}'")
print("EXIF keys:")
for key in self.metadata.exif_keys:
print(f" {key}: '{self.metadata[key].raw_value}'")
print("XMP keys:")
for key in self.metadata.xmp_keys:
print(f" {key}: '{self.metadata[key].raw_value}'")
def append_people(self, people):
if self.is_json(self.iptc_info['Person shown']):
data = json.loads(self.iptc_info['Person shown'])
else:
data = dict()
if self.iptc_infoinfo['Person shown']:
data["previous_user_comment"]=self.iptc_info['user_comment']
data['Person shown'] = list()
new_list = list()
if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys:
for person in self.metadata['Xmp.iptcExt.PersonInImage'].raw_value:
new_list.append(person)
for person in people:
if person not in data['Person shown']:
data['Person shown'].append(person)
self._log.debug(f"New 'user_comment': {json.dumps(data, indent=2)}")
self.iptc_info.set("user_comment", json.dumps(data))
if person not in new_list:
self._log.debug(f"Adding person '{person}'...")
new_list.append(person)
if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys:
self.metadata['Xmp.iptcExt.PersonInImage'].value = new_list
else:
self.metadata['Xmp.iptcExt.PersonInImage'] = pyexiv2.XmpTag('Xmp.iptcExt.PersonInImage', new_list)
def is_json(self, data):
try:
@ -110,12 +123,16 @@ class image_classifier:
with os.scandir(self.faces_directory) as faces_items:
for entry in faces_items:
if not entry.name.startswith('.') and entry.is_file():
self._log.debug(f"Detecting known person in file '{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)
person['name'] = os.path.basename(os.path.splitext(self.faces_directory + os.sep + entry.name)[0])
person['encoding'] = face_recognition.face_encodings(person['filename'])[0]
encodings = face_recognition.face_encodings(person['filename'])
if len(encodings) > 0:
person['encoding'] = encodings[0]
known_people.append(person)
else:
self._log.info(f"No faces found in file '{entry.name}'.")
return known_people
def find_faces(self, file):