Fix 905 Added duplicates and best shot feature
Fixes #905
Relevant Changes
- Made backend handler for detecting duplicate images
- Made backend handler for detecting best shot out of duplicates
- Made a duplicates page in frontend to show duplicates and best shot
Additional changes
- Tweaked a minor bug in backend/app/database/images.py which was causing AI tagging and image upload to stop at 0%
Video - https://res.cloudinary.com/db6cpegxg/video/upload/v1767806671/Screen_Recording_2026-01-07_at_10.48.35_PM_bfxczl.mov
Summary by CodeRabbit
-
New Features
- Added a Duplicates section (sidebar + route) to list grouped visually similar images, highlight a best shot, browse groups in an interactive gallery with modal viewer and keyboard navigation, and select items for bulk deletion.
- New API endpoints to fetch duplicate groups and delete selected images; UI shows deletion results and refreshes data.
-
Chores
- Added image perceptual-hash support and image-similarity processing for duplicate detection.
Walkthrough
Adds perceptual hashing to image records, implements visual duplicate detection and best-shot selection, exposes GET /duplicates/ and POST /duplicates/delete endpoints, persists phash in the database, and provides a frontend Duplicate page with UI, routing, and deletion behavior.
Changes
| Cohort / File(s) | Summary |
|---|---|
Database Enhancement backend/app/database/images.py |
Added phash field to ImageRecord and DB schema; persisted phash in bulk insert/upsert; added db_get_all_images_with_phash() and db_get_images_by_ids() with error handling and safe connection closure. |
Duplicate Detection Utils backend/app/utils/duplicate_detector.py |
New module implementing pHash calculation, dHash-based clustering, ORB geometric similarity checks, sharpness scoring, best-shot identification, and grouping logic with constants and logging. |
Image Utilities backend/app/utils/images.py |
Compute perceptual hash during thumbnail generation and include phash in prepared image records. |
Backend API & Wiring backend/app/routes/duplicates.py, backend/main.py, backend/requirements.txt |
New FastAPI router with GET /duplicates/ and POST /duplicates/delete; registered router at /duplicates; added ImageHash dependency. |
Frontend API & Routing frontend/src/api/apiEndpoints.ts, frontend/src/constants/routes.ts, frontend/src/routes/AppRoutes.tsx |
Added duplicatesEndpoints with getDuplicates/deleteDuplicates, new ROUTES.DUPLICATES, and registered DuplicatePage route. |
Frontend UI frontend/src/components/Navigation/Sidebar/AppSidebar.tsx, frontend/src/pages/DuplicatePage/DuplicatePage.tsx |
Added Duplicates sidebar item and new DuplicatePage component: grouped grids, selection checkboxes, modal viewer with keyboard navigation, delete flow and confirmation. |
API Docs docs/backend/backend_python/openapi.json |
OpenAPI updated to include GET /duplicates/ and POST /duplicates/delete operations with request/response schemas. |
Sequence Diagram(s)
sequenceDiagram
actor User
participant Frontend as Frontend UI
participant API as Backend API
participant DB as Database
participant Detector as Image Processor
participant FS as File System
rect rgba(100,150,200,0.5)
Note over User,Frontend: Duplicate Retrieval Flow
User->>Frontend: Request duplicates
Frontend->>API: GET /duplicates/
API->>DB: db_get_all_images_with_phash()
DB-->>API: images with phash
API->>Detector: group_similar_images(images)
Detector->>Detector: compute hashes, sharpness, cluster, verify geometry
Detector-->>API: grouped images + best_shot_id
API-->>Frontend: duplicate groups
Frontend-->>User: display groups
end
rect rgba(150,100,100,0.5)
Note over User,FS: Deletion Flow
User->>Frontend: Select images & confirm delete
Frontend->>API: POST /duplicates/delete (image_ids)
API->>DB: db_get_images_by_ids(ids)
DB-->>API: image records (paths)
API->>DB: delete records
API->>FS: remove files & thumbnails
FS-->>API: deletion results
API-->>Frontend: {deleted_count, deleted_files_count}
Frontend->>Frontend: refresh view
Frontend-->>User: updated groups
end
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs
- AOSSIE-Org/PictoPy#777 — modifies the same
backend/app/database/images.pyfile and adds image-related fields/DB behavior. - AOSSIE-Org/PictoPy#552 — changes image record schema/types in
backend/app/database/images.py, overlapping schema work.
Suggested labels
Python, TypeScript/JavaScript, Documentation
Suggested reviewers
- rahulharpal1603
Poem
🐰 I hop through hashes, dHash and pHash in tow,
I sniff sharpness where the bright pixels glow,
I gather the twins that mirror the light,
I point the best shot — keep the clearest in sight,
Then nibble the rest and tidy the row.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The PR title clearly summarizes the main change: adding duplicate detection and best shot selection features, which aligns with the primary objective of the changeset. |
| Linked Issues check | ✅ Passed | The PR implements all core requirements from issue #905: duplicate detection via pHash and geometric similarity, best shot identification using sharpness metrics, and a frontend page to display duplicates and recommendations. |
| Out of Scope Changes check | ✅ Passed | All changes directly support duplicate detection and best shot selection. The fix to image upload progress (mentioned in objectives) appears to be implicit in the database schema changes and is within scope. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing Touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
⚠️ This PR has merge conflicts.
Please resolve the merge conflicts before review.
Your PR will only be reviewed by a maintainer after all conflicts have been resolved.
📺 Watch this video to understand why conflicts occur and how to resolve them: https://www.youtube.com/watch?v=Sqsz1-o7nXk