OCI Vision AI: Image Classification and Object Detection with Terraform

OCI Vision provides image analysis as a managed API: it classifies images, detects objects with bounding boxes, extracts text from documents and receipts, and identifies faces. Pre-trained models cover common use cases immediately. For domain-specific needs, you train custom image classification or object detection models on your own labeled images within the same service. This post covers Terraform setup, pre-trained API usage patterns, and custom model training for product image classification.

Step 1: IAM Policy

resource "oci_identity_policy" "vision_policy" {
  compartment_id = var.compartment_id
  name           = "oci-vision-policy"
  statements = [
    "Allow group AIEngineers to manage ai-service-vision-family in compartment id COMPARTMENT_OCID",
    "Allow dynamic-group ApplicationInstances to use ai-service-vision in compartment id COMPARTMENT_OCID",
    "Allow service vision to read object-family in compartment id COMPARTMENT_OCID"
  ]
}

Step 2: Custom Image Classification Model

resource "oci_ai_vision_project" "product_classifier" {
  compartment_id = var.compartment_id
  display_name   = "product-image-classification"
  description    = "Classify product images into categories for catalog automation"
  defined_tags   = { "Operations.ManagedBy" = "terraform" }
}

resource "oci_ai_vision_model" "product_image_model" {
  compartment_id = var.compartment_id
  project_id     = oci_ai_vision_project.product_classifier.id
  display_name   = "product-category-classifier-v1"
  description    = "Classifies product images into 25 product categories"
  model_type     = "IMAGE_CLASSIFICATION"

  training_dataset {
    dataset_type = "DATA_SCIENCE_LABELING"
    dataset_id   = var.labeled_product_images_dataset_id
  }

  training_image_count  = 5000
  testing_image_count   = 1000

  defined_tags = { "Operations.ManagedBy" = "terraform" }
}

resource "oci_ai_vision_model" "product_defect_detector" {
  compartment_id = var.compartment_id
  project_id     = oci_ai_vision_project.product_classifier.id
  display_name   = "product-defect-object-detector-v1"
  description    = "Detect and locate product defects in manufacturing images"
  model_type     = "OBJECT_DETECTION"

  training_dataset {
    dataset_type = "DATA_SCIENCE_LABELING"
    dataset_id   = var.defect_images_dataset_id
  }

  defined_tags = { "Operations.ManagedBy" = "terraform" }
}

output "classifier_model_id" { value = oci_ai_vision_model.product_image_model.id }
output "detector_model_id"   { value = oci_ai_vision_model.product_defect_detector.id }

Step 3: Batch Image Analysis Job

resource "oci_ai_vision_image_job" "catalog_image_classification" {
  compartment_id = var.compartment_id
  display_name   = "product-catalog-batch-classification"

  input_location {
    source_type    = "OBJECT_STORAGE_LOCATIONS"
    object_locations {
      namespace   = var.tenancy_namespace
      bucket_name = var.product_images_bucket
      object_name = "pending-classification/"
    }
  }

  output_location {
    namespace_name = var.tenancy_namespace
    bucket_name    = var.product_images_bucket
    prefix         = "classification-results/"
  }

  features {
    feature_type  = "IMAGE_CLASSIFICATION"
    model_id      = oci_ai_vision_model.product_image_model.id
    max_results   = 5
  }
}

output "batch_job_id" { value = oci_ai_vision_image_job.catalog_image_classification.id }

Operational Notes

Pre-trained Vision models work without any training data for general use cases: detecting text in receipts and forms, identifying common objects in retail and manufacturing images, and extracting structured data from standardized documents. Use the pre-trained models first and measure accuracy against your specific images before committing to the effort of training a custom model. Custom models are only necessary when the pre-trained model accuracy is below your production threshold on your specific image domain.

For batch processing, use the async image job API rather than calling the synchronous analyze_image endpoint per image. Batch jobs process images directly from Object Storage, write results back to Object Storage, and handle thousands of images per job. Calling the synchronous endpoint per image hits rate limits quickly and is significantly slower than a single batch job for volumes above a few hundred images.

Regards,
Osama

#OCI #OracleCloud #VisionAI #ComputerVision #AI #ObjectDetection #Terraform #IaC #TechBlog #Oracle #MachineLearning #OracleCloudInfrastructure #ImageClassification #CustomModel #DataLabeling #BatchProcessing #GenAI #MLOps #ProductCatalog #ManufacturingAI

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.