{ "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": 2, "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": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 1000/1000 [01:39<00:00, 10.05it/s]\n" ] } ], "source": [ "for img_path in tqdm(sorted(glob(\"../../images/imagenet-sample-images/*.JPEG\"))):\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": 7, "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/cat.jpeg\")\n", "ipyplot.plot_images([img])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 9.17 ms, sys: 0 ns, total: 9.17 ms\n", "Wall time: 152 ms\n" ] }, { "data": { "text/plain": [ "{'collection_name': 'images',\n", " 'results': [{'id': '283', 'similarity': 1100469350},\n", " {'id': '278', 'similarity': 1100724118},\n", " {'id': '281', 'similarity': 1100757481},\n", " {'id': '285', 'similarity': 1100871504},\n", " {'id': '174', 'similarity': 1100907644},\n", " {'id': '274', 'similarity': 1101160236},\n", " {'id': '249', 'similarity': 1101246942},\n", " {'id': '332', 'similarity': 1101420168}]}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"../../images/cat.jpeg\", \"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": 6, "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 = \"/home/pawel/logicai/visual-search/images/imagenet-sample-images/{}.JPEG\".format(result[\"id\"])\n", " img = plt.imread(fn)\n", " images_paths.append(img) \n", "ipyplot.plot_images(images_paths)" ] } ], "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 }