The Product Search feature is in maintenance mode. For better scalability and the same functionality as Product Search, use the
Vision Warehouse.
Create a reference image
Stay organized with collections
Save and categorize content based on your preferences.
Create a reference image.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Create a reference image.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Creating your reference images \\& indexing](/vision/product-search/docs/create-reference-images)\n- [Product Search Tutorial](/vision/product-search/docs/tutorial)\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Vision API Product Search, see\n[Vision API Product Search client libraries](/vision/product-search/docs/libraries).\n\n\nFor more information, see the\n[Vision API Product Search Go API\nreference documentation](https://godoc.org/cloud.google.com/go/vision/apiv1).\n\n\nTo authenticate to Vision API Product Search, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tvision \"cloud.google.com/go/vision/apiv1\"\n \t\"cloud.google.com/go/vision/v2/apiv1/visionpb\"\n )\n\n // createReferenceImage creates a reference image for a product.\n func createReferenceImage(w io.Writer, projectID string, location string, productID string, referenceImageID string, gcsURI string) error {\n \tctx := context.Background()\n \tc, err := vision.NewProductSearchClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewProductSearchClient: %w\", err)\n \t}\n \tdefer c.Close()\n\n \treq := &visionpb.https://cloud.google.com/go/docs/reference/cloud.google.com/go/vision/v2/latest/apiv1/visionpb.html#cloud_google_com_go_vision_v2_apiv1_visionpb_CreateReferenceImageRequest{\n \t\tParent: fmt.Sprintf(\"projects/%s/locations/%s/products/%s\", projectID, location, productID),\n \t\tReferenceImage: &visionpb.https://cloud.google.com/go/docs/reference/cloud.google.com/go/vision/v2/latest/apiv1/visionpb.html#cloud_google_com_go_vision_v2_apiv1_visionpb_ReferenceImage{\n \t\t\tUri: gcsURI,\n \t\t},\n \t\tReferenceImageId: referenceImageID,\n \t}\n\n \tresp, err := c.CreateReferenceImage(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"CreateReferenceImage: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Reference image name: %s\\n\", resp.Name)\n \tfmt.Fprintf(w, \"Reference image uri: %s\\n\", resp.Uri)\n\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Vision API Product Search, see\n[Vision API Product Search client libraries](/vision/product-search/docs/libraries).\n\n\nFor more information, see the\n[Vision API Product Search Java API\nreference documentation](/java/docs/reference/google-cloud-vision/latest/overview).\n\n\nTo authenticate to Vision API Product Search, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * Create a reference image.\n *\n * @param projectId - Id of the project.\n * @param computeRegion - Region name.\n * @param productId - Id of the product.\n * @param referenceImageId - Id of the image.\n * @param gcsUri - Google Cloud Storage path of the input image.\n * @throws IOException - on I/O errors.\n */\n public static void createReferenceImage(\n String projectId,\n String computeRegion,\n String productId,\n String referenceImageId,\n String gcsUri)\n throws IOException {\n try (ProductSearchClient client = ProductSearchClient.create()) {\n\n // Get the full path of the product.\n String formattedParent = ProductName.format(projectId, computeRegion, productId);\n // Create a reference image.\n ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build();\n\n ReferenceImage image =\n client.createReferenceImage(formattedParent, referenceImage, referenceImageId);\n // Display the reference image information.\n System.out.println(String.format(\"Reference image name: %s\", image.getName()));\n System.out.println(String.format(\"Reference image uri: %s\", image.getUri()));\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Vision API Product Search, see\n[Vision API Product Search client libraries](/vision/product-search/docs/libraries).\n\n\nFor more information, see the\n[Vision API Product Search Node.js API\nreference documentation](https://googleapis.dev/nodejs/vision/latest).\n\n\nTo authenticate to Vision API Product Search, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const vision = require('https://cloud.google.com/nodejs/docs/reference/vision/latest/overview.html');\n\n const client = new vision.https://cloud.google.com/nodejs/docs/reference/vision/latest/overview.html();\n\n async function createReferenceImage() {\n /**\n * TODO(developer): Uncomment the following line before running the sample.\n */\n // const projectId = 'Your Google Cloud project Id';\n // const location = 'A compute region name';\n // const productId = 'Id of the product';\n // const referenceImageId = 'Id of the reference image';\n // const gcsUri = 'Google Cloud Storage path of the input image';\n\n const formattedParent = client.productPath(projectId, location, productId);\n\n const referenceImage = {\n uri: gcsUri,\n };\n\n const request = {\n parent: formattedParent,\n referenceImage: referenceImage,\n referenceImageId: referenceImageId,\n };\n\n const [response] = await client.createReferenceImage(request);\n console.log(`response.name: ${response.name}`);\n console.log(`response.uri: ${response.uri}`);\n }\n createReferenceImage();\n\n### Python\n\n\nTo learn how to install and use the client library for Vision API Product Search, see\n[Vision API Product Search client libraries](/vision/product-search/docs/libraries).\n\n\nFor more information, see the\n[Vision API Product Search Python API\nreference documentation](/python/docs/reference/vision/latest).\n\n\nTo authenticate to Vision API Product Search, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import vision\n\n def create_reference_image(\n project_id, location, product_id, reference_image_id, gcs_uri\n ):\n \"\"\"Create a reference image.\n Args:\n project_id: Id of the project.\n location: A compute region name.\n product_id: Id of the product.\n reference_image_id: Id of the reference image.\n gcs_uri: Google Cloud Storage path of the input image.\n \"\"\"\n client = vision.https://cloud.google.com/python/docs/reference/vision/latest/google.cloud.vision_v1.services.product_search.ProductSearchClient.html()\n\n # Get the full path of the product.\n product_path = client.https://cloud.google.com/python/docs/reference/vision/latest/google.cloud.vision_v1.services.product_search.ProductSearchClient.html#google_cloud_vision_v1_services_product_search_ProductSearchClient_product_path(\n project=project_id, location=location, product=product_id\n )\n\n # Create a reference image.\n reference_image = vision.https://cloud.google.com/python/docs/reference/vision/latest/google.cloud.vision_v1.types.ReferenceImage.html(uri=gcs_uri)\n\n # The response is the reference image with `name` populated.\n image = client.https://cloud.google.com/python/docs/reference/vision/latest/google.cloud.vision_v1.services.product_search.ProductSearchClient.html#google_cloud_vision_v1_services_product_search_ProductSearchClient_create_reference_image(\n parent=product_path,\n reference_image=reference_image,\n reference_image_id=reference_image_id,\n )\n\n # Display the reference image information.\n print(f\"Reference image name: {image.name}\")\n print(f\"Reference image uri: {image.uri}\")\n\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=vision_product_search)."]]