Skip to content
HalfDescent-NDID

HalfDescent-NDID

Near Duplicate Image Detection

Created on 24th January 2026

•

HalfDescent-NDID

HalfDescent-NDID

Near Duplicate Image Detection

The problem HalfDescent-NDID solves

Near-Duplicate Images Detection (NDID)

A Near-Duplicate Image Detection (NDID) system built using CLIP embeddings, FAISS similarity search, and perceptual hashing (pHash), wrapped in an interactive Streamlit application.

The system determines whether an uploaded image is:

  • an exact duplicate
  • a near-duplicate (crop, blur, brightness/contrast changes)
  • or unique

Storage Optimization:

  • Eliminating redundant copies to save petabytes of cloud storage.

Spam & Integrity:

  • Preventing "repost bots" from flooding feeds and protecting original creators from copyright infringement.

Search Relevance:

  • Ensuring a news aggregator doesn't show the same thumbnail ten times for one story.

Notes

  • NDID decision logic is pure (no side effects)
  • Disk writes and DB mutation occur only in the app/controller layer
  • FAISS index, embeddings, and metadata are cached in memory
  • Missing image paths are handled safely (no crashes)

Challenges we ran into

Challenges I Ran Into

Building this NDID system surfaced several real-world challenges that go beyond implementing individual algorithms. Below are the most significant hurdles I encountered and how I resolved them.


1. FAISS Cache Inconsistency (Duplicate Images Still Marked as Unique)

Problem:
After adding a new non-duplicate image to the database, re-uploading the same image (without restarting the Streamlit app) was still classified as NOT DUPLICATE.

Root Cause:
FAISS and metadata were being updated on disk, but the application continued using a cached in-memory FAISS index. Streamlit reruns the script on every interaction, but Python module-level caches persist unless explicitly updated.

Solution:
I introduced an in-memory update mechanism that:

  • Adds the new embedding directly to the cached FAISS index
  • Appends metadata and embeddings to in-memory structures

This ensured real-time consistency between disk state and memory state, allowing the system to correctly detect duplicates immediately after ingestion.


2. Identical Images Still Classified as Unique (FAISS Alone Was Not Enough)

Problem:
Even exact re-uploads of the same image were sometimes marked as NOT DUPLICATE.

Root Cause:
FAISS is a similarity search engine, not an identity verifier. Small changes in encoding, compression, or image metadata can slightly alter embeddings, causing FAISS to miss exact duplicates.

Solution:
I added a hard perceptual-hash guard:

  • Before ingesting any image marked as NOT DUPLICATE, the system performs a pHash scan against all previously ingested images.
  • If a pHash match is found, ingestion is blocked.

This ensured exact duplicates are never added, even if FAISS similarity is imperfect.


3. Minor Image Edits Falling Between Thresholds

Problem:
Images with small edits (slight crop, brightness change, recompression) were classified as NOT DUPLICATE, which was undesirable for dataset cleaning.

Root Cause:
The system initially used single hard thresholds for pHash and CLIP similarity, leaving a gap where near-duplicates could fall.

Solution:
I introduced a three-level decision scheme:

  • Duplicate
  • Near-Duplicate
  • Not Duplicate

By defining threshold bands instead of single cutoffs, the system became robust to minor edits while avoiding aggressive false positives.


4. Streamlit Layout Crashes (Zero Columns Error)

Problem:
The app crashed with StreamlitInvalidColumnSpecError when no valid neighbors were found.

Root Cause:
st.columns() does not allow zero columns, but the app attempted to create columns using len(results) without checking if it was zero.

Solution:
I added a defensive guard:

  • Display a warning if no results are available
  • Only create columns when at least one valid match exists

This made the UI stable under all conditions.


5. Missing Files Referenced in Metadata

Problem:
The application occasionally crashed with FileNotFoundError when opening neighbor images.

Root Cause:
Some image paths stored in metadata.csv no longer existed due to partial dataset generation or file moves.

Solution:
I implemented safe file handling:

  • Verify file existence before loading
  • Skip missing files gracefully without affecting the pipeline

This made the system resilient to imperfect datasets.


6. Architectural Separation Between Logic and Side Effects

Problem:
Early versions mixed NDID decision logic with file writes, FAISS updates, and Streamlit UI code.

Root Cause:
This blurred responsibility boundaries, making the system hard to debug and unsafe to reuse.

Solution:
I enforced a clean architecture:

  • ndid_logic.py → pure decision logic (no I/O)
  • app.py → orchestration, persistence, UI
  • online_update.py → controlled database mutation

This separation significantly improved correctness, testability, and extensibility.

---.

Discussion

Builders also viewed

See more projects on Devfolio