I am creating a python webui using FastAPI, React, SQLAlchemy and Pydantic. I have just…
- User
I am creating a python webui using FastAPI, React, SQLAlchemy and Pydantic. I have just finished implementing a basic database that lets me add images(stores the path on disk, image width and image height), but also stores image captions, which is flexible to let me have multiple captions for each image, because each caption has it's own caption source(scraped from web, caption .txt file, generated by BLIP/CLIP vision AI models, user written), and I just finished the ability to search the captions to return a list of captions from the database containing the proper search terms.
So I am almost ready to start filling the database with all my images and captions, except for one hiccup, I want to prevent duplicate images from going into the database, it might be worthwhile to link duplicates together if they exist as multiple files, however I don't want to maintain multiple entries and multiple entries of captions for the exact same image.
Just to add a bit of context, this is the SQLAlchemy model for the Images:
class Image(Base): __tablename__ = "image" # columns id: Mapped[int] = mapped_column(primary_key=True) #TODO: Some kind of perceptual hash? Maybe just use, or in addition to the fingerprint and other hashing algos? image_file: Mapped[str] = mapped_column(unique=True) width: Mapped[int] # Image Width height: Mapped[int] # Image Height created_at = mapped_column(DateTime(timezone=True), server_default=func.now()) updated_at = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) # relationships # methods def __repr__(self): return f"{self.__tablename__!r}(id={self.id!r}, image_file=`{self.image_file!r}`, width={self.width!r}, height={self.height!r})"So what I need your help with, firstly, if there exists a way to generate some kind of perceptual hash of an image, in such a way that comparing two hashes together, will allow me to calculate the "difference" between the images(ie, how similar the two images are to each other), and also as a way to quickly check for similar matches for any new images I attempt to add. Let's think this through and show me in a step-by-step way all my options to create a robust way to detect duplicate images within my python program.
- Assistant
- User
How can I use SQLAlchemy to query the database based on the hamming distance? Like I was writing a crud function to get Images based on an averaged hash(using imagehash), stored in a hex string format in the database, I added a threshold variable before I realized I didn't know how to make that appropriate hamming distance comparison within the database itself, which would mean I would need to query all Images from the DB, make the comparison in python, then decide if I want to add that as part of my results. That defeats the purpose of using the database.
Here is my current crud function:
async def images_get_by_ahash(ahash: str, db: Session, threshold: float = 0.99): return [schemas.Image.model_validate(row) for row in db.execute(_sql.select(models.Image).where(models.Image.ahash == ahash)).all()] - Assistant
- User
I am using SQLite, and would like to focus any potential solutions on SQLite.
- Assistant
- User
What could I use that I don't need to run an extra server for(like mysql), and don't need to connect to a remote server(it must be local to the python app), that would allow me to either link to my SQLite database(I can just store the SQLite image id along side the ahash if needed), or do everything I need within this new system(basically has to at least do what SQLite can do, even if it does it in a completely different way).
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.