{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "4Pjmz-RORV8E" }, "source": [ "# Generate image captions and detect objects\n", "\n", "txtai as the name implies works with text and ai, pretty straightforward. But that doesn't mean it can't work with different types of content. For example, an image can be described with words. We can use that description to compare an image to a query or other documents. This notebook shows how images and text can be embedded into the same space to generate image captions and detect objects." ] }, { "cell_type": "markdown", "metadata": { "id": "Dk31rbYjSTYm" }, "source": [ "# Install dependencies\n", "\n", "Install `txtai` and all dependencies. Since this notebook is using optional pipelines, we need to install the pipeline extras package." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "XMQuuun2R06J" }, "outputs": [], "source": [ "%%capture\n", "!pip install ipyplot git+https://github.com/neuml/txtai#egg=txtai[pipeline]\n", "\n", "# Get test data\n", "!wget -N https://github.com/neuml/txtai/releases/download/v3.5.0/tests.tar.gz\n", "!tar -xvzf tests.tar.gz" ] }, { "cell_type": "markdown", "metadata": { "id": "PNPJ95cdTKSS" }, "source": [ "# Create a captions instance\n", "\n", "The captions pipeline takes an image or list of images and generates captions. This pipelines works using a combination of an image encoder model and a text model. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "nTDwXOUeTH2-" }, "outputs": [], "source": [ "%%capture\n", "\n", "from txtai.pipeline import Caption\n", "\n", "# Create caption pipeline\n", "caption = Caption()" ] }, { "cell_type": "markdown", "metadata": { "id": "-vGR_piwZZO6" }, "source": [ "# Generate captions\n", "\n", "The example below shows how to generate captions. A list of images are read from a directory, passed to a caption model and text descriptions are returned." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "-K2YJJzsVtfq", "outputId": "7cfd549a-1db6-47b9-c4ae-623e94ed48d1" }, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ "\n", " \n", "