{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 2, "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": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "api = RecoAIVisualSearch(bearer_token=\"secrettoken\", address=\"http://localhost:8890\")\n" ] }, { "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": [ "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", "