camino-gr/florence2-sam2-demo
0
Florence-2 + SAM Image Recognition Demo
๐ Live Demo: https://huggingface.co/spaces/camino-gr/florence2-sam-demo
A combined pipeline that uses Florence-2 for image captioning and object detection, then feeds detected objects into SAM for high-quality segmentation masks. Includes a Gradio web UI for interactive use.
Architecture
Input Image
โ
โผ
โโโโโโโโโโโโโโโโ
โ Florence-2 โ โ Caption + Object Detection (bounding boxes)
โ (Microsoft) โ
โโโโโโโโฌโโโโโโโโ
โ bounding boxes
โผ
โโโโโโโโโโโโโโโโ
โ SAM โ โ Segmentation masks for each detected object
โ (Meta) โ
โโโโโโโโฌโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโ
โ Visualization โ โ Overlay bboxes, masks, and captions on image
โโโโโโโโฌโโโโโโโโ
โ
โผ
Gradio Web UIFeatures
- Image Captioning โ Generate natural language descriptions of images
- Object Detection โ Detect objects with bounding boxes using Florence-2
- Instance Segmentation โ Pixel-level masks via SAM for each detected object
- Interactive Web UI โ Upload images and see results instantly via Gradio
Setup
Prerequisites
- Python 3.10+
- ~4GB disk space for model weights (downloaded on first run)
Installation
git clone https://github.com/caminogr/florence2-sam-demo.git
cd florence2-sam-demo
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtRun the Demo
python app.pyOpen http://localhost:7860 in your browser.
Usage
- Open the Gradio web UI
- Upload an image (or select an example)
- View results:
- Caption โ natural language description
- Detection โ image with bounding boxes and labels
- Segmentation โ image with colored masks overlaid
Configuration
The pipeline runs on CPU by default. To use GPU, ensure CUDA is available โ the code auto-detects and uses it.
Project Structure
florence2-sam-demo/
โโโ README.md
โโโ requirements.txt
โโโ pyproject.toml
โโโ src/
โ โโโ __init__.py
โ โโโ florence2_model.py # Florence-2 inference wrapper
โ โโโ sam_model.py # SAM inference wrapper
โ โโโ pipeline.py # Combined pipeline
โ โโโ visualization.py # Drawing bboxes, masks, captions
โโโ app.py # Gradio demo
โโโ examples/ # Sample images
โโโ .gitignore
โโโ LICENSEScreenshots
<!-- Add screenshots here --> Screenshots will be added after first successful run.
Models Used
License
MIT โ see LICENSE.
