建立參考圖片
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
建立參考圖片。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","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)."]]