Exemplo n.º 1
0
 def write_metadata(self, data: Image) -> None:
     """
     """
     if hasattr(data, 'metafile') and hasattr(data, 'valid'):
         suffix = '' if data.metafile.endswith('json2') else '2'
         meta = {
             'image': data.filename,
             'dataset': data.dataset,
             'valid': data.valid,
             'age': data.age
         }
         if data.has_attribute('source'):
             meta['source'] = data.source
         if data.has_attribute('boundingbox'):
             meta['boundingbox'] = data.boundingbox
         if data.has_attribute('id'):
             meta['id'] = data.id
         filename = data.metafile + suffix
         LOG.debug("Writing new meta file '%s'", filename)
         with open(filename, 'w') as outfile:
             json.dump(meta, outfile)
     else:
         LOG.debug("Not writing new meta file (metafile: %s, valid: %s).",
                   hasattr(data, 'metafile'), hasattr(data, 'valid'))
Exemplo n.º 2
0
 def load_metadata(self, data: Image) -> None:
     """
     """
     filename_meta = data.filename.rsplit('.', maxsplit=1)[0] + '.json'
     filename_meta += '2' if os.path.isfile(filename_meta + '2') else ''
     if os.path.isfile(filename_meta):
         LOG.debug("Loading meta file '%s'", filename_meta)
         with open(filename_meta) as infile:
             meta = json.load(infile)
             # image:        path to the image file
             # source:       path to the source file
             # dataset:      the dataset from which this images was taken
             # boundingbox:  bounding bos of the face in the original image
             # id:           the class label
             data.add_attribute('image', meta['image'])
             data.add_attribute('dataset', meta['dataset'])
             if 'source' in meta:
                 source_filename = meta['source']
                 source_filename = source_filename.replace('\\', '/')
                 source_filename = \
                     source_filename.replace('E:', self._basedir)
                 data.add_attribute('source', source_filename)
             if 'boundingbox' in meta:
                 data.add_attribute('boundingbox', meta['boundingbox'])
             if 'id' in meta:
                 data.add_attribute('id', meta['id'])
             data.add_attribute('age', meta.get('age', None))
             data.add_attribute('valid', meta.get('valid', True))
         data.add_attribute('metafile', filename_meta)
     else:
         LOG.debug("No meta file for data (tried '%s')", filename_meta)
         if data.filename.startswith(self.directory):
             filename = data.filename[len(self.directory) + 1:]
             parts = filename.split('/')
             label, imagename = parts[0], parts[-1]
             if imagename.startswith('imdb_wiki'):
                 data.add_attribute('dataset', 'imdb_wiki')
                 source_filename = os.path.join(self._clean4, 'Unified',
                                                filename)
                 if os.path.isfile(source_filename):
                     data.add_attribute('source', source_filename)
             elif len(parts) > 2 and parts[1] == 'New':
                 LOG.warning("New image without meta data: '%s'", filename)
                 data.add_attribute('dataset')
             elif os.path.isfile(
                     os.path.join(self._clean2, 'Patricia', filename)):
                 data.add_attribute('dataset', 'Patricia')
                 data.add_attribute(
                     'source',
                     os.path.join(self._clean2, 'Patricia', filename))
             else:
                 LOG.warning("Unknown source dataset for '%s'", filename)
                 data.add_attribute('dataset')
         else:
             LOG.warning("Bad filename: '%s' (not in directory '%s')",
                         data.filename, self.directory)
             data.add_attribute('dataset')
         if not filename_meta.endswith('json2'):
             filename_meta += '2'
         data.add_attribute('metafile', filename_meta)
         if not data.has_attribute('age'):
             data.add_attribute('age')
         if not data.has_attribute('valid'):
             data.add_attribute('valid', True)