{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append(\"../python/recoai_visual_search/\")\n", "from visual_search import RecoAIVisualSearch\n", "from models import *\n", "import json\n", "from glob import glob\n", "import ipyplot\n", "from matplotlib import pyplot as plt\n", "from tqdm import tqdm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creating a collection to keep the images \n", "-------------\n", "\n", "In this case we are using MOBILE_NET_V2 as the feature extractor. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "api = RecoAIVisualSearch(bearer_token=\"secrettoken\", address=\"http://localhost:8890\")\n", "upsert_collection = UpsertCollection(\n", " config=GenericModelConfig(\n", " model_architecture=ModelArchitecture.MOBILE_NET_V2\n", " ), \n", " name=\"images\"\n", ")\n", "response = api.upsert_collection(upsert_collection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing local images\n", "-----------\n", "\n", "It is possible to index local images using `ImageBytes` or `ImageSource(url=\"link_to_image\")`" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 759/759 [01:58<00:00, 6.42it/s]\n" ] } ], "source": [ "for img_path in tqdm(sorted(glob(\"../../images/ecommerce-images/data/Apparel/Boys/Images/images_with_product_ids/*.jpg\"))):\n", " image_id = img_path.split(\"/\")[-1].split(\".\")[0]\n", " with open(img_path, \"rb\") as inp:\n", " image_bytes = list(inp.read())\n", " image_source = ImageSource(image_bytes=ImageBytes(image_bytes))\n", " add_image = AddImage(collection_name=\"images\", id=image_id, source=image_source)\n", " resp = api.add_image(add_image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Searching for a cat \n", "-----------" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", "
\n", " \n", " \n", " \n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", "
\n", "
\n", "
\n", "

0

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "img = plt.imread(\"../../images/ecommerce-images/data/Apparel/Boys/Images/images_with_product_ids/10054.jpg\")\n", "ipyplot.plot_images([img], img_width=300)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 187 ms, sys: 11 µs, total: 187 ms\n", "Wall time: 430 ms\n" ] }, { "data": { "text/plain": [ "{'collection_name': 'images',\n", " 'results': [{'id': '10054', 'similarity': 0},\n", " {'id': '4728', 'similarity': 1093241214},\n", " {'id': '4202', 'similarity': 1093312557},\n", " {'id': '52121', 'similarity': 1093681515},\n", " {'id': '50718', 'similarity': 1093681515},\n", " {'id': '35999', 'similarity': 1094314830},\n", " {'id': '24909', 'similarity': 1094343361},\n", " {'id': '34183', 'similarity': 1094374909}]}" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"../../images/ecommerce-images/data/Apparel/Boys/Images/images_with_product_ids/10054.jpg\", \"rb\") as inp:\n", " image_bytes = list(inp.read())\n", "image_source = ImageSource(image_bytes=ImageBytes(image_bytes))\n", "search_image = SearchImage(collection_name=\"images\", n_results=8, source=image_source)\n", "%time search_results = json.loads(api.search_image(search_image).content)\n", "search_results" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", "
\n", " \n", " \n", " \n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", "
\n", "
\n", "
\n", "

0

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

1

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

2

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

3

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

4

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

5

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

6

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", "

7

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "images_paths = []\n", "for result in search_results[\"results\"]:\n", " fn = \"../../images/ecommerce-images/data/Apparel/Boys/Images/images_with_product_ids/{}.jpg\".format(result[\"id\"])\n", " img = plt.imread(fn)\n", " images_paths.append(img) \n", "ipyplot.plot_images(images_paths, img_width=200)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".py", "format_name": "light", "format_version": "1.5", "jupytext_version": "1.5.0" } }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }