CoolFace
Apppublic

camino-gr/florence2-sam2-demo

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

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 UI

Features

  • โ€”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

bash
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.txt

Run the Demo

bash
python app.py

Open http://localhost:7860 in your browser.

Usage

  1. 1.Open the Gradio web UI
  2. 2.Upload an image (or select an example)
  3. 3.View results:
  4. 4.Caption โ€” natural language description
  5. 5.Detection โ€” image with bounding boxes and labels
  6. 6.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
โ””โ”€โ”€ LICENSE

Screenshots

<!-- Add screenshots here --> Screenshots will be added after first successful run.

Models Used

ModelSourcePurpose
Florence-2-largeMicrosoftCaptioning + Object Detection
SAMMetaSegmentation Masks

License

MIT โ€” see LICENSE.