Skip to content

Commit db6ec2d

Browse files
authored
Merge branch 'main' into sent-categ-update
2 parents 6f4a765 + 9b32f61 commit db6ec2d

File tree

46 files changed

+659
-808
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+659
-808
lines changed

ai/ai-vision/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Reviewed: 11.06.2025
2828

2929
## GitHub
3030

31+
- [OCI object detection and image segmentation using SAM2](.ai-vision-and-sam/)
32+
3133
- [OCI image classification using data labeling and vision service](https://github.com/carlgira/oci-image-classification)
3234
- [OCI object detection using data labeling and vision service](https://github.com/carlgira/oci-object-detection)
3335
- [AI vision web client](https://github.com/oracle-devrel/oci-tf-vision-web-client)

ai/oracle-digital-assistant/solution-definition/LICENSE renamed to ai/ai-vision/ai-vision-and-sam/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3232
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3333
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3434
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35-
SOFTWARE.
35+
SOFTWARE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
![Example of using this asset with a picture of a chair](files/images/example_use.jpg)
2+
3+
# OCI Vision and SAM2
4+
5+
The following asset is a tool designed to showcase how OCI AI Vision can be connected with SAM2 from Meta for accurate segmentations without needing high amount of data for training.
6+
7+
Reviewed: 22.09.2025
8+
9+
Authors: Matthias Wolf and Cristina Granés
10+
11+
# When to use this asset?
12+
13+
When you need to compute areas from images, detect and segment objects with AI.
14+
15+
# How to use this asset?
16+
17+
See the full setup and usage guide in [`files/README.md`](./files/README.md).
18+
19+
20+
# License
21+
22+
Copyright (c) 2025 Oracle and/or its affiliates.
23+
24+
Licensed under the Universal Permissive License (UPL), Version 1.0.
25+
26+
See [LICENSE](LICENSE) for more details.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_FILE_PATH = "~/.oci/config"
2+
# This needs to be a valid compartment ID within your tenancy
3+
COMPARTMENT_ID = "ocid1.compartment.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
4+
# Change the endpoint to match your account's region
5+
ENDPOINT = "https://inference.generativeai.eu-frankfurt-1.oci.oraclecloud.com"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[theme]
2+
primaryColor = "#e83610" # Red
3+
backgroundColor = "#1E1E1E" # Dark gray background
4+
secondaryBackgroundColor = "#2C2C2C" # Slightly lighter sidebar
5+
textColor = "#FFFFFF" # Clean white text
6+
font = "sans serif"
7+
8+
[client]
9+
showSidebarNavigation = false
10+
toolbarMode = "minimal"
11+
12+
[server]
13+
headless = true
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
![Example of using this asset with a picture of a chair](images/example_use.jpg)
2+
3+
# Image segmentation with OCI AI Vision and SAM2 from Meta
4+
5+
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)**.
6+
7+
**Segmenting objects in images has never been this easy!**
8+
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.
9+
10+
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).
11+
12+
## Some possible business cases
13+
- Compute areas of roofs to detect the area available for solar panels.
14+
- Segment construction sites to estimate areas from aerial images.
15+
- Environmental monitoring: segment different land types (forest, water, urban areas,...) from satellite imagery and estimate changes in those areas.
16+
- Urban planning: segment and detect roads, bridges, and utilities for urban development projects.
17+
- Crop and weed segmentation to optimize pesticide usage and crop management.
18+
19+
## Requirements
20+
You can install the following demo in a personal PC.
21+
You would need some space and at least a CPU with 300MB of memory, for `sam2.1-hiera-small`.
22+
23+
## Setup
24+
1. Install Python (this project requires Python 3.13.5 or later). You can check your current Python version by running:
25+
</br>
26+
```
27+
python --version
28+
```
29+
or
30+
```
31+
python3 --version
32+
```
33+
2. Install the requirements from `requirements.txt` file.
34+
</br>
35+
```
36+
pip install -r /path/to/requirements.txt
37+
```
38+
3. Update the `.config` file with your own `CONFIG_FILE_PATH` and `COMPARTMENT_ID`:
39+
```
40+
CONFIG_FILE_PATH = <path_to_oci_login_config_file>
41+
COMPARTMENT_ID = <compartment_OCID>
42+
# Change the endpoint to match your account's region
43+
ENDPOINT = "https://inference.generativeai.eu-frankfurt-1.oci.oraclecloud.com"
44+
```
45+
4. Run the application using `streamlit run app.py`.
46+
47+
## Technical Details
48+
* The solution leverages Oracle Cloud Infrastructure (OCI) AI Vision Service, an AI service designed to simplify AI adoption.
49+
* Specifically, this demo utilizes:
50+
+ OCI Vision object detection
51+
+ SAM2 segmentation
52+
53+
### About SAM2
54+
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.
55+
56+
Key features include:
57+
- State-of-the-art segmentation performance
58+
- Support for promptable segmentation (points, boxes, masks)
59+
- Open-source and ready for integration into custom workflows
60+
61+
You can find more information [here](https://docs.ultralytics.com/models/sam-2/).
62+
And you can find information about the License [here](https://github.com/facebookresearch/sam2/blob/main/LICENSE).
63+
64+
## Project Structure
65+
The repository is organized as follows:
66+
67+
```plaintext
68+
│ .config # File to be added as explained in `Setup`, with your own OCI variables
69+
│ app.py # Main Streamlit application entry point
70+
│ config.py # Variables for the Streamlit application
71+
│ navigation.py # Configuration for the sidebar in the Streamlit application
72+
│ README.md # Project documentation
73+
│ requirements.txt # Python dependencies
74+
75+
├───utils
76+
│ │ ai_tools.py # Wrappers for inference on the AI models
77+
│ │ image_utils.py # Wrappers for image functionalities
78+
79+
├───app_images
80+
│ │ oracle_logo.png # Logo of Oracle for Streamlit application
81+
82+
├───.streamlit # Parameters for UI appearance of the Streamlit application
83+
84+
└───uploaded_images # Folder to be used by the Streamlit application
85+
```
86+
87+
## Output
88+
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.
89+
90+
## Authors
91+
- Matthias Wolf
92+
- Cristina Granés
93+
94+
## Contributing
95+
We welcome contributions to improve and expand the capabilities of this demo. Please fork the repository and submit a pull request with your changes.
96+
97+
## License
98+
Copyright (c) 2025 Oracle and/or its affiliates.
99+
100+
Licensed under the Universal Permissive License (UPL), Version 1.0.
101+
102+
See [LICENSE](../LICENSE) for more details.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import streamlit as st
2+
import config
3+
from PIL import Image
4+
5+
import torch
6+
from sam2.sam2_image_predictor import SAM2ImagePredictor
7+
8+
from utils.image_utils import load_image, save_uploaded_image, draw_detections, draw_masks
9+
from utils.ai_tools import InferencePipeline
10+
from navigation import make_sidebar
11+
12+
@st.cache_resource
13+
def load_sam():
14+
# This will require internet access when deploying the model
15+
# Alternative, larger model: "facebook/sam2.1-hiera-large"
16+
predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2.1-hiera-small", device=torch.device("cpu"))
17+
return predictor
18+
19+
20+
def main():
21+
22+
## Page config
23+
icon = Image.open(config.ORACLE_LOGO)
24+
25+
st.set_page_config(
26+
page_title="Detect and segment",
27+
page_icon=icon,
28+
layout="wide",
29+
)
30+
31+
## Header ##
32+
st.markdown("<h1 style='margin-bottom: 0;'>Segment Images with OCI AI Vision + SAM2 from Meta</h1>", unsafe_allow_html=True)
33+
st.divider()
34+
35+
36+
### Init application ###
37+
38+
sam_model = load_sam()
39+
ai_pipeline = InferencePipeline(config=config, sam_model=sam_model)
40+
41+
# Sidebar for upload and run
42+
uploaded_file, run_button = make_sidebar()
43+
44+
45+
######### finish init
46+
47+
## Upload image & process ##
48+
coli1, coli2 = st.columns([0.5, 0.5])
49+
if uploaded_file:
50+
image = load_image(uploaded_file)
51+
with coli1: st.image(image, caption="Original Image")
52+
53+
if run_button:
54+
with st.spinner("Processing...", show_time=True):
55+
uploaded_file.seek(0)
56+
# Save image:
57+
image_path = save_uploaded_image(uploaded_file, save_dir=config.UPLOAD_PATH)
58+
59+
#detections = ai_pipeline.get_detection(uploaded_file)
60+
detections, masks = ai_pipeline.get_detections_and_masks(image_path)
61+
62+
image_with_boxes = draw_detections(image, detections)
63+
with coli2: st.image(image_with_boxes, caption="OCI Vision Detections")
64+
65+
st.header("Final segmentations")
66+
67+
## Plot masks:
68+
mask_names = [msk["class"] for msk in masks]
69+
tabs_list = st.tabs(mask_names)
70+
for id_msk, msk in enumerate(masks):
71+
ctab = tabs_list[id_msk]
72+
det = detections[id_msk]
73+
image_with_mask = draw_masks(image, msk["mask"])
74+
with ctab:
75+
st.image(image_with_mask,
76+
width=700,
77+
caption=f"{msk["class"]} - Detection confidence score: {det["confidence_score"]:.2f}")
78+
79+
80+
if __name__ == '__main__':
81+
main()
2.97 KB
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
from dotenv import load_dotenv
3+
load_dotenv(dotenv_path=".config")
4+
5+
# config, compartments and endpoints
6+
CONFIG_FILE_PATH = os.getenv("CONFIG_FILE_PATH")
7+
COMPARTMENT_ID = os.getenv("COMPARTMENT_ID")
8+
ENDPOINT= os.getenv("ENDPOINT")
9+
10+
## Other config params
11+
ORACLE_LOGO = "app_images/oracle_logo.png"
12+
UPLOAD_PATH = "uploaded_images"
749 KB
Loading

0 commit comments

Comments
 (0)