This repository provides reusable Python code for creating image embeddings from videos and storing them in Azure AI Search. It enables semantic search over video frames using Azure's AI services.
- Azure AI Search Index Management: Create and manage search indexes with vector search capabilities
- Video Frame Extraction: Extract frames from video files with configurable sampling rates
- Image Embedding Generation: Generate embeddings using Azure Foundry models (Azure AI Inference)
- Semantic Search: Search for similar images using vector similarity
- Batch Processing: Efficient batch processing of multiple images
The system consists of four main components:
- AzureSearchIndexManager: Manages Azure AI Search index creation and configuration
- VideoFrameExtractor: Extracts frames from video files using OpenCV
- ImageEmbeddingGenerator: Generates embeddings using Azure AI Inference
- ImageEmbeddingPipeline: Orchestrates the complete workflow
- Python 3.8 or higher
- Azure subscription with:
- Azure AI Search service
- Azure AI Foundry/Inference endpoint
- API keys or Azure AD credentials for both services
- Clone the repository:
git clone https://github.com/aparna198809/Create-Image-Embeddings-using-Azure-AI-Search.git
cd Create-Image-Embeddings-using-Azure-AI-Search- Install required packages:
pip install -r requirements.txt- Configure environment variables:
cp .env.template .env
# Edit .env with your Azure credentialsCreate a .env file with your Azure credentials:
AZURE_SEARCH_ENDPOINT=https://your-search-service.search.windows.net
AZURE_SEARCH_KEY=your_search_key
AZURE_SEARCH_INDEX_NAME=image-embeddings-index
AZURE_INFERENCE_ENDPOINT=https://your-inference-endpoint.inference.ai.azure.com
AZURE_INFERENCE_KEY=your_inference_keyfrom azure.core.credentials import AzureKeyCredential
from image_embeddings import ImageEmbeddingPipeline, load_config_from_env
# Load configuration
config = load_config_from_env()
# Create credentials
search_credential = AzureKeyCredential(config["search_key"])
inference_credential = AzureKeyCredential(config["inference_key"])
# Initialize pipeline
pipeline = ImageEmbeddingPipeline(
search_endpoint=config["search_endpoint"],
search_credential=search_credential,
inference_endpoint=config["inference_endpoint"],
inference_credential=inference_credential,
index_name=config["index_name"],
)
# Process a video
document_ids = pipeline.process_video(
video_path="path/to/video.mp4",
sample_rate=30, # Extract every 30th frame
max_frames=100, # Maximum 100 frames
output_dir="frames" # Save frames here
)from azure.core.credentials import AzureKeyCredential
from image_embeddings import AzureSearchIndexManager
index_manager = AzureSearchIndexManager(
endpoint="https://your-search-service.search.windows.net",
credential=AzureKeyCredential("your_key")
)
index_manager.create_index(
index_name="image-embeddings-index",
vector_dimensions=1024
)from image_embeddings import VideoFrameExtractor
extractor = VideoFrameExtractor("path/to/video.mp4")
# Extract every 30th frame
frames = extractor.extract_frames(
sample_rate=30,
output_dir="frames"
)
# Or extract frames at specific intervals
frames = extractor.extract_frames_by_interval(
interval_seconds=1.0, # One frame per second
output_dir="frames"
)import cv2
from azure.core.credentials import AzureKeyCredential
from image_embeddings import ImageEmbeddingGenerator
generator = ImageEmbeddingGenerator(
endpoint="https://your-inference-endpoint.inference.ai.azure.com",
credential=AzureKeyCredential("your_key")
)
### Search Similar Images
```python
import cv2
# Search for similar images
input_query = read(user_query)
embedding = generator.generate_embedding(input_query)
results = pipeline.search_similar_images(input_query, top_k=5)
for result in results:
print(f"Video: {result['file_name']}")
print(f"Score: {result['@search.score']}")Run the example script:
python example_usage.pyMake sure to update the video_path variable in the script with the path to your video file.
.
├── image_embeddings.py # Main module with all functionality
├── example_usage.py # Example usage script
├── requirements.txt # Python dependencies
├── .env.template # Environment variables template
├── .gitignore # Git ignore file
└── README.md # This file
azure-ai-inference: Azure AI Inference SDK for embedding generationazure-identity: Azure authenticationazure-search-documents: Azure AI Search SDKopencv-python: Video processing and frame extractionPillow: Image processingnumpy: Numerical operationspython-dotenv: Environment variable management
create_index(index_name, vector_dimensions): Create a new search indexdelete_index(index_name): Delete an existing indexlist_indexes(): List all indexes
extract_frames(sample_rate, max_frames, output_dir): Extract frames with samplingextract_frames_by_interval(interval_seconds, output_dir): Extract frames at time intervals
generate_embedding(image): Generate embedding for a single imagegenerate_embeddings_batch(images, batch_size): Generate embeddings in batches
process_video(video_path, sample_rate, max_frames, output_dir): Complete video processing workflowsearch_similar_images(query_image, top_k): Search for similar images
- Video file not opening: Ensure OpenCV is properly installed and the video codec is supported
- Authentication errors: Verify your Azure credentials and endpoint URLs
- Memory issues: Reduce
batch_sizeormax_framesfor large videos
- "Unable to open video file": Check video path and file permissions
- "Missing required environment variables": Ensure all required variables are set in
.env - "Invalid video FPS": Video file may be corrupted or unsupported format
Contributions are welcome! Please feel free to submit a Pull Request.
This project is provided as-is for educational and development purposes.
- Azure AI Search documentation
- Azure AI Inference documentation
- OpenCV community