חיפוש מוצרים

אחרי יצירת קבוצת המוצרים ואינדוקס של קבוצת המוצרים, אפשר לשלוח שאילתה לקבוצת המוצרים באמצעות Cloud Vision API.

כדי למצוא מוצרים דומים לתמונה מסוימת, מעבירים את URI של Cloud Storage של התמונה, את כתובת ה-URL של התמונה באינטרנט או מחרוזת מקודדת של Base64 אל Vision API Google Product Search. במאמר מגבלות שימוש מופיע מידע על גודל הבקשה המקסימלי ועל המכסות.

בנושא הסבר על תשובות לחיפושים וזיהוי של כמה מוצרים יש דוגמה לזיהוי של מוצר אחד ולזיהוי של כמה מוצרים בתמונה.

חיפוש באמצעות תמונה מקומית

בדוגמאות הבאות קוראים קובץ מקומי ושולחים שאילתה ל-API על ידי הכללת הבייטים הגולמיים של התמונה (תמונה מקודדת ב-Base64) בבקשה.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • BASE64_ENCODED_IMAGE: ייצוג base64 (מחרוזת ASCII) של נתוני התמונה הבינאריים. המחרוזת הזו צריכה להיראות כמו המחרוזת הבאה:
    • /9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
    מידע נוסף זמין בנושא קידוד base64.
  • PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • LOCATION_ID: מזהה מיקום תקין. מזהי מיקום תקינים: us-west1,‏ us-east1,‏ europe-west1 ו-asia-east1.
  • PRODUCT_SET_ID: המזהה של קבוצת המוצרים שרוצים להריץ עליה את הפעולה.

שיקולים ספציפיים לשדה:

  • features.maxResults – המספר המקסימלי של התוצאות שיוחזרו.
  • imageContext.productCategories – קטגוריית המוצרים שבה רוצים לחפש. בשלב הזה אפשר לציין רק קטגוריית מוצרים אחת (מוצרים לבית, ביגוד, צעצועים, מוצרים ארוזים וכללי).
  • imageContext.filter – (אופציונלי) ביטוי סינון של מפתח/ערך (או כמה ביטויים) לתווית מוצר. פורמט: ‎"key=value". סינון: אפשר לקשר בין זוגות של מפתח-ערך באמצעות הביטויים AND או OR: ‏‎"color=blue AND style=mens"‎ או ‎"color=blue OR color=black"‎. אם משתמשים בביטוי OR, כל המפתחות בביטוי צריכים להיות זהים.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://vision.googleapis.com/v1/images:annotate

גוף בקשת JSON:

{
  "requests": [
    {
      "image": {
        "content": base64-encoded-image
      },
      "features": [
        {
          "type": "PRODUCT_SEARCH",
          "maxResults": 5
        }
      ],
      "imageContext": {
        "productSearchParams": {
          "productSet": "projects/project-id/locations/location-id/productSets/product-set-id",
          "productCategories": [
               "apparel"
          ],
          "filter": "style = womens"
        }
      }
    }
  ]
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://vision.googleapis.com/v1/images:annotate"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content

אם הבקשה תתבצע בהצלחה, השרת יחזיר קוד סטטוס 200 OK של HTTP ואת התשובה בפורמט JSON.

קובץ ה-JSON של התגובה כולל את שני סוגי התוצאות הבאים:

  • productSearchResults – מכיל רשימה של מוצרים תואמים לכל התמונה. בדוגמה לתשובה, המוצרים התואמים הם: product_id65, product_id35, product_id34, product_id62, product_id32.
  • productGroupedResults – מכיל קואורדינטות של תיבת תוחמת ופריטים תואמים לכל מוצר שזוהה בתמונה. בתגובה הבאה יש רק מוצר אחד שזוהה, ואחריו מוצרים תואמים במדגם המוצרים: product_id65, product_id35, product_id34, product_id93, product_id62.

שימו לב שיש חפיפה בין שני סוגי התוצאות, אבל יכול להיות שיהיו גם הבדלים (למשל, product_id32 ו-product_id93 בתגובה).

Go

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Go API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.


import (
	"context"
	"fmt"
	"io"
	"os"

	vision "cloud.google.com/go/vision/apiv1"
	"cloud.google.com/go/vision/v2/apiv1/visionpb"
)

// getSimilarProducts searches for products from a product set similar to products in an image file.
func getSimilarProducts(w io.Writer, projectID string, location string, productSetID string, productCategory string, file string, filter string) error {
	ctx := context.Background()
	c, err := vision.NewImageAnnotatorClient(ctx)
	if err != nil {
		return fmt.Errorf("NewImageAnnotatorClient: %w", err)
	}
	defer c.Close()

	f, err := os.Open(file)
	if err != nil {
		return fmt.Errorf("Open: %w", err)
	}
	defer f.Close()

	image, err := vision.NewImageFromReader(f)
	if err != nil {
		return fmt.Errorf("NewImageFromReader: %w", err)
	}

	ictx := &visionpb.ImageContext{
		ProductSearchParams: &visionpb.ProductSearchParams{
			ProductSet:        fmt.Sprintf("projects/%s/locations/%s/productSets/%s", projectID, location, productSetID),
			ProductCategories: []string{productCategory},
			Filter:            filter,
		},
	}

	response, err := c.ProductSearch(ctx, image, ictx)
	if err != nil {
		return fmt.Errorf("ProductSearch: %w", err)
	}

	fmt.Fprintf(w, "Product set index time:\n")
	fmt.Fprintf(w, "seconds: %d\n", response.IndexTime.Seconds)
	fmt.Fprintf(w, "nanos: %d\n", response.IndexTime.Nanos)

	fmt.Fprintf(w, "Search results:\n")
	for _, result := range response.Results {
		fmt.Fprintf(w, "Score(Confidence): %f\n", result.Score)
		fmt.Fprintf(w, "Image name: %s\n", result.Image)

		fmt.Fprintf(w, "Prodcut name: %s\n", result.Product.Name)
		fmt.Fprintf(w, "Product display name: %s\n", result.Product.DisplayName)
		fmt.Fprintf(w, "Product labels: %s\n", result.Product.ProductLabels)
	}

	return nil
}

Java

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Java API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/**
 * Search similar products to image in local file.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param filePath - Local file path of the image to be searched
 * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
 *     color = blue) AND style = kids It will search on all products with the following labels:
 *     color:red AND style:kids color:blue AND style:kids
 * @throws IOException - on I/O errors.
 */
public static void getSimilarProductsFile(
    String projectId,
    String computeRegion,
    String productSetId,
    String productCategory,
    String filePath,
    String filter)
    throws IOException {
  try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {

    // Get the full path of the product set.
    String productSetPath = ProductSetName.format(projectId, computeRegion, productSetId);

    // Read the image as a stream of bytes.
    File imgPath = new File(filePath);
    byte[] content = Files.readAllBytes(imgPath.toPath());

    // Create annotate image request along with product search feature.
    Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
    // The input image can be a HTTPS link or Raw image bytes.
    // Example:
    // To use HTTP link replace with below code
    //  ImageSource source = ImageSource.newBuilder().setImageUri(imageUri).build();
    //  Image image = Image.newBuilder().setSource(source).build();
    Image image = Image.newBuilder().setContent(ByteString.copyFrom(content)).build();
    ImageContext imageContext =
        ImageContext.newBuilder()
            .setProductSearchParams(
                ProductSearchParams.newBuilder()
                    .setProductSet(productSetPath)
                    .addProductCategories(productCategory)
                    .setFilter(filter))
            .build();

    AnnotateImageRequest annotateImageRequest =
        AnnotateImageRequest.newBuilder()
            .addFeatures(featuresElement)
            .setImage(image)
            .setImageContext(imageContext)
            .build();
    List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);

    // Search products similar to the image.
    BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);

    List<Result> similarProducts =
        response.getResponses(0).getProductSearchResults().getResultsList();
    System.out.println("Similar Products: ");
    for (Result product : similarProducts) {
      System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
      System.out.println(
          String.format("Product display name: %s", product.getProduct().getDisplayName()));
      System.out.println(
          String.format("Product description: %s", product.getProduct().getDescription()));
      System.out.println(String.format("Score(Confidence): %s", product.getScore()));
      System.out.println(String.format("Image name: %s", product.getImage()));
    }
  }
}

Node.js

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Node.js API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
const fs = require('fs');
// Creates a client
const productSearchClient = new vision.ProductSearchClient();
const imageAnnotatorClient = new vision.ImageAnnotatorClient();

async function getSimilarProductsFile() {
  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = 'nodejs-docs-samples';
  // const location = 'us-west1';
  // const productSetId = 'indexed_product_set_id_for_testing';
  // const productCategory = 'apparel';
  // const filePath = './resources/shoes_1.jpg';
  // const filter = '';
  const productSetPath = productSearchClient.productSetPath(
    projectId,
    location,
    productSetId
  );
  const content = fs.readFileSync(filePath, 'base64');
  const request = {
    // The input image can be a GCS link or HTTPS link or Raw image bytes.
    // Example:
    // To use GCS link replace with below code
    // image: {source: {gcsImageUri: filePath}}
    // To use HTTP link replace with below code
    // image: {source: {imageUri: filePath}}
    image: {content: content},
    features: [{type: 'PRODUCT_SEARCH'}],
    imageContext: {
      productSearchParams: {
        productSet: productSetPath,
        productCategories: [productCategory],
        filter: filter,
      },
    },
  };
  const [response] = await imageAnnotatorClient.batchAnnotateImages({
    requests: [request],
  });
  console.log('Search Image:', filePath);
  const results = response['responses'][0]['productSearchResults']['results'];
  console.log('\nSimilar product information:');
  results.forEach(result => {
    console.log('Product id:', result['product'].name.split('/').pop(-1));
    console.log('Product display name:', result['product'].displayName);
    console.log('Product description:', result['product'].description);
    console.log('Product category:', result['product'].productCategory);
  });
}
getSimilarProductsFile();

Python

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Python API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import vision

def get_similar_products_file(
    project_id,
    location,
    product_set_id,
    product_category,
    file_path,
    filter,
    max_results,
):
    """Search similar products to image.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_set_id: Id of the product set.
        product_category: Category of the product.
        file_path: Local file path of the image to be searched.
        filter: Condition to be applied on the labels.
                Example for filter: (color = red OR color = blue) AND style = kids
                It will search on all products with the following labels:
                color:red AND style:kids
                color:blue AND style:kids
        max_results: The maximum number of results (matches) to return. If omitted, all results are returned.
    """
    # product_search_client is needed only for its helper methods.
    product_search_client = vision.ProductSearchClient()
    image_annotator_client = vision.ImageAnnotatorClient()

    # Read the image as a stream of bytes.
    with open(file_path, "rb") as image_file:
        content = image_file.read()

    # Create annotate image request along with product search feature.
    image = vision.Image(content=content)

    # product search specific parameters
    product_set_path = product_search_client.product_set_path(
        project=project_id, location=location, product_set=product_set_id
    )
    product_search_params = vision.ProductSearchParams(
        product_set=product_set_path,
        product_categories=[product_category],
        filter=filter,
    )
    image_context = vision.ImageContext(product_search_params=product_search_params)

    # Search products similar to the image.
    response = image_annotator_client.product_search(
        image, image_context=image_context, max_results=max_results
    )

    index_time = response.product_search_results.index_time
    print("Product set index time: ")
    print(index_time)

    results = response.product_search_results.results

    print("Search results:")
    for result in results:
        product = result.product

        print(f"Score(Confidence): {result.score}")
        print(f"Image name: {result.image}")

        print(f"Product name: {product.name}")
        print("Product display name: {}".format(product.display_name))
        print(f"Product description: {product.description}\n")
        print(f"Product labels: {product.product_labels}\n")


שפות נוספות

C#‎: צריך לפעול לפי הוראות ההגדרה של C# ‎ בדף של ספריות הלקוח ואז לעבור אל מאמרי העזרה בנושא Google Product Search ב-Vision API עבור ‎ .NET.

PHP: עליכם לפעול לפי הוראות ההגדרה של PHP בדף של ספריות הלקוח ואז לעבור אל מסמכי העזר של Google Product Search ב-Vision API ל-PHP.

Ruby: צריך לפעול לפי ההוראות להגדרת Ruby בדף של ספריות הלקוח ולעיין במסמכי העזר של Vision API Google Product Search ל-Ruby.

חיפוש באמצעות תמונה מרחוק

יש לכם גם אפשרות למצוא מוצרים דומים לתמונה מסוימת על ידי ציון ה-URI של Cloud Storage לתמונה.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • CLOUD_STORAGE_IMAGE_URI: הנתיב לקובץ תמונה תקין בקטגוריה של Cloud Storage. צריכות להיות לכם לפחות הרשאות קריאה לקובץ. דוגמה:
    • gs://storage-bucket/filename.jpg
  • PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • LOCATION_ID: מזהה מיקום תקין. מזהי מיקום תקינים: us-west1,‏ us-east1,‏ europe-west1 ו-asia-east1.
  • PRODUCT_SET_ID: המזהה של קבוצת המוצרים שרוצים להריץ עליה את הפעולה.

שיקולים ספציפיים לשדה:

  • features.maxResults – המספר המקסימלי של התוצאות שיוחזרו.
  • imageContext.productCategories – קטגוריית המוצרים שבה רוצים לחפש. בשלב הזה אפשר לציין רק קטגוריית מוצרים אחת (מוצרים לבית, ביגוד, צעצועים, מוצרים ארוזים וכללי).
  • imageContext.filter – (אופציונלי) ביטוי סינון של מפתח/ערך (או כמה ביטויים) לתווית מוצר. פורמט: ‎"key=value". סינון: אפשר לקשר בין זוגות של מפתח-ערך באמצעות הביטויים AND או OR: ‏‎"color=blue AND style=mens"‎ או ‎"color=blue OR color=black"‎. אם משתמשים בביטוי OR, כל המפתחות בביטוי צריכים להיות זהים.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://vision.googleapis.com/v1/images:annotate

גוף בקשת JSON:

{
  "requests": [
    {
      "image": {
        "source": {
          "gcsImageUri": "cloud-storage-image-uri"
        }
      },
      "features": [
        {
          "type": "PRODUCT_SEARCH",
          "maxResults": 5
        }
      ],
      "imageContext": {
        "productSearchParams": {
          "productSet": "projects/project-id/locations/location-id/productSets/product-set-id",
          "productCategories": [
               "apparel"
          ],
          "filter": "style = womens"
        }
      }
    }
  ]
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://vision.googleapis.com/v1/images:annotate"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content

אם הבקשה תתבצע בהצלחה, השרת יחזיר קוד סטטוס 200 OK של HTTP ואת התשובה בפורמט JSON.

קובץ ה-JSON של התגובה כולל את שני סוגי התוצאות הבאים:

  • productSearchResults – מכיל רשימה של מוצרים תואמים לכל התמונה. בדוגמה לתשובה, המוצרים התואמים הם: product_id65, product_id35, product_id34, product_id62, product_id32.
  • productGroupedResults – מכיל קואורדינטות של תיבת תוחמת ופריטים תואמים לכל מוצר שזוהה בתמונה. בתגובה הבאה יש רק מוצר אחד שזוהה, ואחריו מוצרים תואמים במדגם המוצרים: product_id65, product_id35, product_id34, product_id93, product_id62.

שימו לב שיש חפיפה בין שני סוגי התוצאות, אבל יכול להיות שיהיו גם הבדלים (למשל, product_id32 ו-product_id93 בתגובה).

Go

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Go API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.


import (
	"context"
	"fmt"
	"io"

	vision "cloud.google.com/go/vision/apiv1"
	"cloud.google.com/go/vision/v2/apiv1/visionpb"
)

// getSimilarProductsURI searches for products from a product set similar to products in an image file on GCS.
func getSimilarProductsURI(w io.Writer, projectID string, location string, productSetID string, productCategory string, imageURI string, filter string) error {
	ctx := context.Background()
	c, err := vision.NewImageAnnotatorClient(ctx)
	if err != nil {
		return fmt.Errorf("NewImageAnnotatorClient: %w", err)
	}
	defer c.Close()

	image := vision.NewImageFromURI(imageURI)

	ictx := &visionpb.ImageContext{
		ProductSearchParams: &visionpb.ProductSearchParams{
			ProductSet:        fmt.Sprintf("projects/%s/locations/%s/productSets/%s", projectID, location, productSetID),
			ProductCategories: []string{productCategory},
			Filter:            filter,
		},
	}

	response, err := c.ProductSearch(ctx, image, ictx)
	if err != nil {
		return fmt.Errorf("ProductSearch: %w", err)
	}

	fmt.Fprintf(w, "Product set index time:\n")
	fmt.Fprintf(w, "seconds: %d\n", response.IndexTime.Seconds)
	fmt.Fprintf(w, "nanos: %d\n", response.IndexTime.Nanos)

	fmt.Fprintf(w, "Search results:\n")
	for _, result := range response.Results {
		fmt.Fprintf(w, "Score(Confidence): %f\n", result.Score)
		fmt.Fprintf(w, "Image name: %s\n", result.Image)

		fmt.Fprintf(w, "Prodcut name: %s\n", result.Product.Name)
		fmt.Fprintf(w, "Product display name: %s\n", result.Product.DisplayName)
		fmt.Fprintf(w, "Product labels: %s\n", result.Product.ProductLabels)
	}

	return nil
}

Java

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Java API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/**
 * Search similar products to image in Google Cloud Storage.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param gcsUri - GCS file path of the image to be searched
 * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
 *     color = blue) AND style = kids It will search on all products with the following labels:
 *     color:red AND style:kids color:blue AND style:kids
 * @throws Exception - on errors.
 */
public static void getSimilarProductsGcs(
    String projectId,
    String computeRegion,
    String productSetId,
    String productCategory,
    String gcsUri,
    String filter)
    throws Exception {
  try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {

    // Get the full path of the product set.
    String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString();

    // Get the image from Google Cloud Storage
    ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();

    // Create annotate image request along with product search feature.
    Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
    Image image = Image.newBuilder().setSource(source).build();
    ImageContext imageContext =
        ImageContext.newBuilder()
            .setProductSearchParams(
                ProductSearchParams.newBuilder()
                    .setProductSet(productSetPath)
                    .addProductCategories(productCategory)
                    .setFilter(filter))
            .build();

    AnnotateImageRequest annotateImageRequest =
        AnnotateImageRequest.newBuilder()
            .addFeatures(featuresElement)
            .setImage(image)
            .setImageContext(imageContext)
            .build();
    List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);

    // Search products similar to the image.
    BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);

    List<Result> similarProducts =
        response.getResponses(0).getProductSearchResults().getResultsList();
    System.out.println("Similar Products: ");
    for (Result product : similarProducts) {
      System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
      System.out.println(
          String.format("Product display name: %s", product.getProduct().getDisplayName()));
      System.out.println(
          String.format("Product description: %s", product.getProduct().getDescription()));
      System.out.println(String.format("Score(Confidence): %s", product.getScore()));
      System.out.println(String.format("Image name: %s", product.getImage()));
    }
  }
}

Node.js

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Node.js API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const productSearchClient = new vision.ProductSearchClient();
const imageAnnotatorClient = new vision.ImageAnnotatorClient();

async function getSimilarProductsGcs(
  projectId,
  location,
  productSetId,
  productCategory,
  filePath,
  filter
) {
  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = 'Your Google Cloud project Id';
  // const location = 'A compute region name';
  // const productSetId = 'Id of the product set';
  // const productCategory = 'Category of the product';
  // const filePath = 'Local file path of the image to be searched';
  // const filter = 'Condition to be applied on the labels';
  const productSetPath = productSearchClient.productSetPath(
    projectId,
    location,
    productSetId
  );

  const request = {
    // The input image can be a GCS link or HTTPS link or Raw image bytes.
    // Example:
    // To use GCS link replace with below code
    // image: {source: {gcsImageUri: filePath}}
    // To use HTTP link replace with below code
    // image: {source: {imageUri: filePath}}
    image: {source: {gcsImageUri: filePath}},
    features: [{type: 'PRODUCT_SEARCH'}],
    imageContext: {
      productSearchParams: {
        productSet: productSetPath,
        productCategories: [productCategory],
        filter: filter,
      },
    },
  };
  console.log(request.image);

  const [response] = await imageAnnotatorClient.batchAnnotateImages({
    requests: [request],
  });
  console.log('Search Image:', filePath);
  console.log('\nSimilar product information:');

  const results = response['responses'][0]['productSearchResults']['results'];
  results.forEach(result => {
    console.log('Product id:', result['product'].name.split('/').pop(-1));
    console.log('Product display name:', result['product'].displayName);
    console.log('Product description:', result['product'].description);
    console.log('Product category:', result['product'].productCategory);
  });
}
getSimilarProductsGcs();

Python

מידע על התקנת ספריית הלקוח של Vision API Google Product Search ושימוש בה מופיע במאמר ספריות הלקוח של Vision API Google Product Search. מידע נוסף מופיע במאמרי העזרה של Vision API Google Product Search Python API.

כדי לבצע אימות ב-Google Product Search של Vision API, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import vision

def get_similar_products_uri(
    project_id, location, product_set_id, product_category, image_uri, filter
):
    """Search similar products to image.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_set_id: Id of the product set.
        product_category: Category of the product.
        image_uri: Cloud Storage location of image to be searched.
        filter: Condition to be applied on the labels.
        Example for filter: (color = red OR color = blue) AND style = kids
        It will search on all products with the following labels:
        color:red AND style:kids
        color:blue AND style:kids
    """
    # product_search_client is needed only for its helper methods.
    product_search_client = vision.ProductSearchClient()
    image_annotator_client = vision.ImageAnnotatorClient()

    # Create annotate image request along with product search feature.
    image_source = vision.ImageSource(image_uri=image_uri)
    image = vision.Image(source=image_source)

    # product search specific parameters
    product_set_path = product_search_client.product_set_path(
        project=project_id, location=location, product_set=product_set_id
    )
    product_search_params = vision.ProductSearchParams(
        product_set=product_set_path,
        product_categories=[product_category],
        filter=filter,
    )
    image_context = vision.ImageContext(product_search_params=product_search_params)

    # Search products similar to the image.
    response = image_annotator_client.product_search(image, image_context=image_context)

    index_time = response.product_search_results.index_time
    print("Product set index time: ")
    print(index_time)

    results = response.product_search_results.results

    print("Search results:")
    for result in results:
        product = result.product

        print(f"Score(Confidence): {result.score}")
        print(f"Image name: {result.image}")

        print(f"Product name: {product.name}")
        print("Product display name: {}".format(product.display_name))
        print(f"Product description: {product.description}\n")
        print(f"Product labels: {product.product_labels}\n")


שפות נוספות

C#‎: צריך לפעול לפי הוראות ההגדרה של C# ‎ בדף של ספריות הלקוח ואז לעבור אל מאמרי העזרה בנושא Google Product Search ב-Vision API עבור ‎ .NET.

PHP: עליכם לפעול לפי הוראות ההגדרה של PHP בדף של ספריות הלקוח ואז לעבור אל מסמכי העזר של Google Product Search ב-Vision API ל-PHP.

Ruby: צריך לפעול לפי ההוראות להגדרת Ruby בדף של ספריות הלקוח ולעיין במסמכי העזר של Vision API Google Product Search ל-Ruby.