πŸ” Build an AI-Powered Image Search Engine Using BLOBs and Oracle Database 23ai

 

Introduction

Imagine being able to search for similar images by simply uploading a picture—just like Google Reverse Image Search, but built using Oracle Database 23ai. With the rise of AI-powered vector search and native BLOB support in Oracle, you can now build this capability directly within your database.

In this post, we'll guide you through building an AI-powered image similarity search engine using:

  • Oracle Database 23ai

  • Python (oml4py)

  • ONNX pre-trained model (EfficientNet / ResNet)

  • Vector embeddings + native VECTOR data type

  • Oracle APEX (optional UI)

 What Is Vector Search?

Vector search lets you compare data like images, documents, or audio by semantic similarity using high-dimensional vectors. Instead of exact keyword matching, it compares meaning or content.

We’ll convert images into vectors (embeddings) using a neural network model, then store and query those vectors using Oracle’s native VECTOR type and approximate nearest neighbor (ANN) indexing.

Architecture Overview

[User Uploads Image] 
        ↓
[Convert to Vector via Python ONNX]
        ↓
[Store BLOB + Vector in Oracle 23ai Table]
        ↓
[Query Similar Vectors using VECTOR SEARCH]
        ↓
[Return Top N Similar Images]

Step 1: Create the Table to Store Images and Vectors

We’ll use Oracle’s native support for BLOBs and VECTORs (available in 23ai):

CREATE TABLE image_embeddings (

    id           NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,

    image_name   VARCHAR2(255),

    image_blob   BLOB,

    vector_embedding VECTOR(128), -- Assuming 128-dim vectors

    created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

Step 2: Generate Image Embeddings Using ONNX in Oracle

Use OML4Py (Oracle Machine Learning for Python) to load an ONNX model and convert images into vector embeddings.

Install ONNX model (EfficientNet B0)

You can use a pre-trained ONNX model like EfficientNet B0 or MobileNet:


import oml

from oml import automl

import onnxruntime as ort

from PIL import Image

import numpy as np

import base64

import io


# Load ONNX model

session = ort.InferenceSession("/models/efficientnet_b0.onnx")


def preprocess_image(image_data):

    image = Image.open(io.BytesIO(image_data)).resize((224, 224)).convert("RGB")

    image_np = np.array(image).astype(np.float32) / 255.0

    image_np = image_np.transpose(2, 0, 1)  # Channels-first

    image_np = np.expand_dims(image_np, axis=0)

    return image_np


def generate_embedding(image_blob):

    image_np = preprocess_image(image_blob)

    result = session.run(None, {"images": image_np})[0]

    return result.flatten().astype(np.float32)




Step 3: Insert the Image and Vector into Oracle Table

You can insert the image and vector into the database using OML4Py or a Python client:

from oml import connection


conn = connection.connect()


image_path = "ring1.jpg"

with open(image_path, "rb") as f:

    img_blob = f.read()


embedding = generate_embedding(img_blob)


insert_sql = """

INSERT INTO image_embeddings (image_name, image_blob, vector_embedding)

VALUES (:1, :2, :3)

"""


cursor = conn.cursor()

cursor.execute(insert_sql, ("ring1.jpg", img_blob, embedding.tolist()))

conn.commit()


 Step 4: Create VECTOR Index for Fast Search

CREATE SEARCH INDEX img_vec_index ON image_embeddings (vector_embedding)

   INDEXTYPE IS CTXSYS.CONTEXT PARAMETERS ('ANN TOPN 10 DISTANCE EUCLIDEAN');


Step 5: Perform Similar Image Search

To find similar images, convert the input image into a vector and use a vector similarity query:

SELECT image_name, created_at FROM image_embeddings ORDER BY vector_distance(vector_embedding, VECTOR(:your_embedding)) FETCH FIRST 5 ROWS ONLY;

You can call this from Python or PL/SQL after embedding the query image.


Step 6 (Optional): Build a UI in Oracle APEX

You can build a user-friendly web app using Oracle APEX to:

  • Upload images

  • Preview stored results

  • Show top 5 similar images

Use the APEX file browse item and PL/SQL process to handle image upload and call the vector search backend via REST or stored procedures.


Example Use Cases

  • Jewelry or fashion image search engines

  • Retail or e-commerce “Search by Image”

  • Visual defect detection in manufacturing

  • Reverse image lookup for digital asset management

Tips and Best Practices

  • Store normalized vectors (unit length) for cosine similarity.

  • Use batch indexing for large-scale datasets.

  • Compress vectors if using many dimensions (e.g., PCA or Autoencoder).

  • Use Oracle HeatWave ML for more advanced ANN indexing (coming soon).

 Conclusion

With Oracle 23ai and native vector search, building an image similarity engine is no longer a complex task. Using ONNX for inference and VECTOR type for storage, Oracle now competes with modern vector databases—right inside your enterprise-grade RDBMS.

You can further expand this by adding:

  • Multi-modal search (text + image)

  • Metadata filters (price, category)

  • RESTful APIs for integration

Sample Repository (Optional)

GitHub Repo: github.com/techie-g/oracle-vector-image-search


Comments

Popular posts from this blog

Introduction to Oracle Vector Search – Concepts, Requirements & Use Cases

Setting Up Monitoring and Alerts in OCI for Your Resources