Example #1
0
    def add_image(self, image: ImageFile, encodings: List[ndarray]) -> int:
        """
        Adds one image entry to the database.
        :param image: ImageFile to add
        :return: Id of the created image record
        """
        # Check for existence
        exist_check = self.get_image_id_by_attributes(image)
        if exist_check:
            return exist_check

        # Insert images
        insert_image = """
        INSERT INTO Image 
        (filename, date_modified, size_bytes, aperture, shutter_speed, iso, date_taken, path) 
        values (?, ?, ?, ?, ?, ?, ?, ?)
        --      0  1  2  3  4  5  6  7  
        """
        dbresponse = self.connection.execute(insert_image,
                                             self.adapt_ImageFile(image))
        image.dbid = dbresponse.lastrowid

        # Insert associated encodings
        for enc in encodings:
            self.add_encoding(enc, image.dbid, image=True)

        self.connection.commit()
        image.in_database = True
        return image.dbid
Example #2
0
def ensure_image_in_database(db: Database, image: ImageFile) -> int:
    image_id = db.get_image_id_by_attributes(image)
    if image_id:
        encodings: List[FaceEncoding] = db.get_encodings_by_image_id(image_id)
        logging.debug(
            f"File {image.filepath} already in database (image_id: {image_id}) with {len(encodings)} faces(s)."
        )
    else:  # Encode and save
        new_encodings: List[ndarray] = encode_faces(image.filepath)
        image_id = db.add_image(image, new_encodings)
        logging.debug(
            f"File {image.filepath} added to database (image_id: {image_id}) with {len(new_encodings)} face(s)."
        )
    image.dbid = image_id
    return image_id