|
| 1 | +# Image segmentation with OCI AI Vision and SAM2 from Meta |
| 2 | +In this demo, you will see how you can segment objects in images by combining **OCI AI Vision** with **Meta’s SAM2 (Segment Anything Model 2)**. |
| 3 | + |
| 4 | +**Segmenting objects in images has never been this easy!** |
| 5 | +You can combine both OCI AI Vision to handle image analysis and SAM2 to provide high-precision segmentation, you can quickly identify and isolate objects in any image using just a few clicks or simple prompts. This integration demonstrates how cloud-based vision services and cutting-edge AI models can work together to streamline complex computer vision tasks. |
| 6 | + |
| 7 | +For more accurate results, you can build a custom object detection model in OCI AI Vision (see [the documentation](https://docs.oracle.com/en-us/iaas/Content/vision/using/custom_image_analysis_models_using.htm) for more details). |
| 8 | + |
| 9 | +## Some possible business cases |
| 10 | +- Compute areas of roofs to detect the area available for solar panels. |
| 11 | +- Segment construction sites to estimate areas from aerial images. |
| 12 | +- Environmental monitoring: segment different land types (forest, water, urban areas,...) from satellite imagery and estimate changes in those areas. |
| 13 | +- Urban planning: segment and detect roads, bridges, and utilities for urban development projects. |
| 14 | +- Crop and weed segmentation to optimize pesticide usage and crop management. |
| 15 | + |
| 16 | +## Requirements |
| 17 | +You can install the following demo in a personal PC. |
| 18 | +You would need some space and at least a CPU with 300MB of memory, for `sam2.1-hiera-small`. |
| 19 | + |
| 20 | +## Setup |
| 21 | +1. Install Python (this project requires Python 3.13.5 or later). You can check your current Python version by running: |
| 22 | +</br> |
| 23 | +``` |
| 24 | +python --version |
| 25 | +``` |
| 26 | +or |
| 27 | +``` |
| 28 | +python3 --version |
| 29 | +``` |
| 30 | +2. Install the requirements from `requirements.txt` file. |
| 31 | +</br> |
| 32 | +``` |
| 33 | +pip install -r /path/to/requirements.txt |
| 34 | +``` |
| 35 | +3. Create a `.config` file that contains the following variables: |
| 36 | +``` |
| 37 | +CONFIG_FILE_PATH = <path_to_oci_login_config_file> |
| 38 | +COMPARTMENT_ID = <compartment_OCID> |
| 39 | +# Change the endpoint to match your account's region |
| 40 | +ENDPOINT = "https://inference.generativeai.eu-frankfurt-1.oci.oraclecloud.com" |
| 41 | +``` |
| 42 | +4. Run the application using `streamlit run app.py`. |
| 43 | + |
| 44 | +## Technical Details |
| 45 | +* The solution leverages Oracle Cloud Infrastructure (OCI) AI Vision Service, an AI service designed to simplify AI adoption. |
| 46 | +* Specifically, this demo utilizes: |
| 47 | + + OCI Vision object detection |
| 48 | + + SAM2 segmentation |
| 49 | + |
| 50 | +### About SAM2 |
| 51 | +SAM2 is an open-source model developed by Meta AI as the next-generation version of the original Segment Anything Model (SAM). It enables fast and flexible image segmentation, allowing users to easily extract precise object masks from images with minimal input. Built to support both interactive and automated segmentation tasks, SAM2 improves on efficiency, accuracy, and generalization across diverse image types and domains. |
| 52 | + |
| 53 | +Key features include: |
| 54 | +- State-of-the-art segmentation performance |
| 55 | +- Support for promptable segmentation (points, boxes, masks) |
| 56 | +- Open-source and ready for integration into custom workflows |
| 57 | + |
| 58 | +You can find more information [here](https://docs.ultralytics.com/models/sam-2/). |
| 59 | +And you can find information about the License [here](https://github.com/facebookresearch/sam2/blob/main/LICENSE). |
| 60 | + |
| 61 | +## Project Structure |
| 62 | +The repository is organized as follows: |
| 63 | + |
| 64 | +```plaintext |
| 65 | +│ .config # File to be added as explained in `Setup`, with your own OCI variables |
| 66 | +│ app.py # Main Streamlit application entry point |
| 67 | +│ config.py # Variables for the Streamlit application |
| 68 | +│ navigation.py # Configuration for the sidebar in the Streamlit application |
| 69 | +│ README.md # Project documentation |
| 70 | +│ requirements.txt # Python dependencies |
| 71 | +│ |
| 72 | +├───utils |
| 73 | +│ │ ai_tools.py # Wrappers for inference on the AI models |
| 74 | +│ │ image_utils.py # Wrappers for image functionalities |
| 75 | +│ |
| 76 | +├───app_images |
| 77 | +│ │ oracle_logo.png # Logo of Oracle for Streamlit application |
| 78 | +│ |
| 79 | +├───.streamlit # Parameters for UI appearance of the Streamlit application |
| 80 | +│ |
| 81 | +└───uploaded_images # Folder to be used by the Streamlit application |
| 82 | +``` |
| 83 | + |
| 84 | +## Output |
| 85 | +The demo will display an interactive dashboard to upload an image and process it, displaying the resulting detections from OCI Vision and segmentations of those detections. |
| 86 | + |
| 87 | +## Authors |
| 88 | +- Matthias Wolf |
| 89 | +- Cristina Granés |
| 90 | + |
| 91 | +## Contributing |
| 92 | +We welcome contributions to improve and expand the capabilities of this demo. Please fork the repository and submit a pull request with your changes. |
| 93 | + |
| 94 | +## License |
| 95 | +Copyright (c) 2025 Oracle and/or its affiliates. |
| 96 | + |
| 97 | +Licensed under the Universal Permissive License (UPL), Version 1.0. |
| 98 | + |
| 99 | +See [LICENSE](../LICENSE) for more details. |
0 commit comments