【GCP】Vision API Product Search触ってみた。

こんにちは。SCSKの島村です。

本日は、Google CloudのAIMLサービスの一つである「Vision API Product Search」についてご紹介させていただきます。

Vision API Product Searchとは??

Vision API Product Searchとは:AIを活用して、検索画像から類似画像を提供するサービス

Product Searchのサービス使用イメージ

画像を検索するには、検索画像のGoogle Cloud Storage URI、ウェブURLまたはbase64でエンコードされた文字列をVison API Product Searchに渡すことで可能です。

細かな手順を割愛すると、下の3ステップで使用開始することが可能です。

使用手順 手順詳細
商品セットの作成 商品セットリソースを作成する。
商品の検索 Vision APIリクエストで類似商品を検索する。
商品の作成 商品を作成し、商品セットへ商品を追加する。

また、Vision API Product  Searchは、商品の参照画像が1点のみでも機能します。特にある程度のバリエーションがある場合は、通常3~8枚の画像があれば、より精度の高い結果を得ることが可能となります。

Vision API Product Search documentation  |  Google Cloud
Enables retailers to create a set of products and of reference images that visually describe the product from a set of v...

触ってみた!!!(画像を使ったデモ)

実際にサービスの使用感と精度を確認するために、GoogleImageから画像を収集し、簡易的なデモを行ってみました。

今回は、「キッチン」のデータセットを作成し、実際に検索をかけるところまで確認しました。

本記事の検証では、Google検索で抽出された画像を利用しております。

17枚のキッチンの画像からデータセットを作成

商品セット:”kitchen_tutorial”
キッチン①
“kitchen_1”

キッチン②“kitchen_2”

キッチン③“kitchen_3”

キッチン④“kitchen_4”

キッチン⑤“kitchen_5”

キッチン⑥“kitchen_6”

キッチン⑦“kitchen_7”

キッチン⑧“kitchen_8”

キッチン⑨“kitchen_9”

キッチン⑩“kitchen_10”

キッチン⑪“kitchen_11”

キッチン⑫“kitchen_12”

キッチン⑬“kitchen_13”

キッチン⑭“kitchen_14”

キッチン⑮“kitchen_15”

新たに用意したキッチンの画像をProduct Searchに入力した結果↓↓↓↓↓↓↓↓↓

検索画像 Responce 出力画像
(似ている画像として出力されたもの)

検索画像①

Product set index time: 
2022-06-09 06:59:28.006128+00:00
Search results:
Score(Confidence): 0.5357654094696045
Image name: projects/———–/locations/———–/products/kitchen_1/referenceImages/image10
Product name: projects/———–/locations/———–/products/kitchen_1
Product display name:  
Product description:            

Product labels: [key: “category”
value: “kitchen”
]

出力画像①

検索画像②

Product set index time: 
2022-06-09 06:59:28.006128+00:00
Search results:
Score(Confidence): 0.45753684639930725
Image name: projects/———–/locations/———–/products/kitchen_1/referenceImages/image14
Product name: projects/———–/locations/———–/products/kitchen_1
Product display name:  
Product description:            

Product labels: [key: “category”
value: “kitchen”
]

 

 

・・・サービスを触ってみての感想・・・

推論の信頼度スコア(Confidence)は50%程度だったということで、まだまだモデルの改良の余地はありそうというのが所感です。

今回使用したデータセットの画像については、撮像画角やサイズ・照度などランダムなものを使用しております。また、キッチン以外の背景についても多く散見されます。これら要因が少なからず精度に依存している可能性は考えられます。

また、本サービスは「検索」というサービスであることから「高い精度で一致する<似たようなものが出力される」なサービスであるとも感じ取れました。

まだまだ改良の余地もありそうなので、データセットを変えて検証してみるのもありかもしれません。

サービス利用量については、こちらのドキュメントよりご確認ください。

Pricing  |  Vision API Product Search  |  Google Cloud
Review pricing for Vision API Product Search

使用手順とサンプルコード


手順
Python Code

1. オンラインで個別にインポートを行い、商品セットを作成する。

空の商品セットを作成する。これは商品のグループを格納するためのシンプルなコンテナです。

from google.cloud import vision

# def create_product_set(
#         project_id, location, product_set_id, product_set_display_name):
#     “””Create a product set.
#     Args:
#         project_id: Id of the project.
#         location: A compute region name.
#         product_set_id: Id of the product set.
#         product_set_display_name: Display name of the product set.
#     “””
#     client = vision.ProductSearchClient()
#     # A resource that represents Google Cloud Platform location.
#     location_path = f”projects/{project_id}/locations/{location}”
#     # Create a product set with the product set specification in the region.
#     product_set = vision.ProductSet(
#             display_name=product_set_display_name)
#     # The response is the product set with `name` populated.
#     response = client.create_product_set(
#         parent=location_path,
#         product_set=product_set,
#         product_set_id=product_set_id)
#     # Display the product set information.
#     print(‘Product set name: {}’.format(response.name))

2. 個別の商品を作成する。

商品セットを作成したら、次のステップとして商品を作成します。

# from google.cloud import vision
# from google.protobuf import field_mask_pb2 as field_mask           

# def create_product(
#         project_id, location, product_id, product_display_name,
#         product_category):
#     “””Create one product.
#     Args:
#         project_id: Id of the project.
#         location: A compute region name.
#         product_id: Id of the product.
#         product_display_name: Display name of the product.
#         product_category: Category of the product.
#     “””
#     client = vision.ProductSearchClient()
#     # A resource that represents Google Cloud Platform location.
#     location_path = f”projects/{project_id}/locations/{location}”
#     # Create a product with the product specification in the region.
#     # Set product display name and product category.
#     product = vision.Product(
#         display_name=product_display_name,
#         product_category=product_category)
#     # The response is the product with the `name` field populated.
#     response = client.create_product(
#         parent=location_path,
#         product=product,
#         product_id=product_id)
#     # Display the product information.
#     print(‘Product name: {}’.format(response.name))

3. 商品セットに商品を追加する。

商品セットと商品が作成されたら、商品セットに商品を追加できます。

from google.cloud import vision

# def add_product_to_product_set(
#         project_id, location, product_id, product_set_id):
#     “””Add a product to a product set.
#     Args:
#         project_id: Id of the project.
#         location: A compute region name.
#         product_id: Id of the product.
#         product_set_id: Id of the product set.
#     “””
#     client = vision.ProductSearchClient()
#     # Get the full path of the product set.
#     product_set_path = client.product_set_path(
#         project=project_id, location=location,
#         product_set=product_set_id)
#     # Get the full path of the product.
#     product_path = client.product_path(
#         project=project_id, location=location, product=product_id)
#     # Add the product to the product set.
#     client.add_product_to_product_set(
#         name=product_set_path, product=product_path)
#     print(‘Product added to product set.’)

4. 商品を更新する。

作成した商品や商品セットを更新んする必要がある場合は、更新用のメソッドを使用できます。

from google.cloud import vision
from google.protobuf import field_mask_pb2 as field_mask           

# def update_product_labels(
#         project_id, location, product_id, key, value):
#     “””Update the product labels.
#     Args:
#         project_id: Id of the project.
#         location: A compute region name.
#         product_id: Id of the product.
#         key: The key of the label.
#         value: The value of the label.
#     “””
#     client = vision.ProductSearchClient()
#     # Get the name of the product.
#     product_path = client.product_path(
#         project=project_id, location=location, product=product_id)
#     # Set product name, product label and product display name.
#     # Multiple labels are also supported.
#     key_value = vision.Product.KeyValue(key=key, value=value)
#     product = vision.Product(
#         name=product_path,
#         product_labels=[key_value])
#     # Updating only the product_labels field here.
#     update_mask = field_mask.FieldMask(paths=[‘product_labels’])
#     # This overwrites the product_labels.
#     updated_product = client.update_product(
#         product=product, update_mask=update_mask)
#     # Display the updated product information.
#     print(‘Product name: {}’.format(updated_product.name))
#     print(‘Updated product labels: {}’.format(product.product_labels))

5.参照画像を作成する。

個別の商品の参照画像を作成すると、画像にインデックスが追加された後にVision API Product Searchでその画像を使用して商品を検索できます。

from google.cloud import vision

# def create_reference_image(
#         project_id, location, product_id, reference_image_id, gcs_uri):
#     “””Create a reference image.
#     Args:
#         project_id: Id of the project.
#         location: A compute region name.
#         product_id: Id of the product.
#         reference_image_id: Id of the reference image.
#         gcs_uri: Google Cloud Storage path of the input image.
#     “””
#     client = vision.ProductSearchClient()
#     # Get the full path of the product.
#     product_path = client.product_path(
#         project=project_id, location=location, product=product_id)
#     # Create a reference image.
#     reference_image = vision.ReferenceImage(uri=gcs_uri)
#     # The response is the reference image with `name` populated.
#     image = client.create_reference_image(
#         parent=product_path,
#         reference_image=reference_image,
#         reference_image_id=reference_image_id)
#     # Display the reference image information.
#     print(‘Reference image name: {}’.format(image.name))
#     print(‘Reference image uri: {}’.format(image.uri))

6. 類似商品を検索する。

新しい画像を入力として受け取り、最も一致する商品を検索することで、作成した商品カタログを区クエリ出来ます。

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(‘Score(Confidence): {}’.format(result.score))
#         print(‘Image name: {}’.format(result.image))
#         print(‘Product name: {}’.format(product.name))
#         print(‘Product display name: {}’.format(
#             product.display_name))
#         print(‘Product description: {}\n’.format(product.description))
#         print(‘Product labels: {}\n’.format(product.product_labels))

最後に

今回はGoogle CloudのAIMLサービスの一つである「Vision API Product Search」についてご紹介させていただきました。

今後とも、AIMLに関する情報やGoogle CloudのAIMLサービスのアップデート情報を掲載していきたいと思います。

最後まで読んでいただき、ありがとうございました!!!

著者について

SCSK株式会社のクラウド専門部隊に所属
Google Cloudの
・AI MLサービス選定/導入コンサルティング/導入サポート/AI 基盤・運用構築 まで多く案件を担当
Google Cloud Partner Top Engineer 2024 受賞 カテゴリ:Cloud AI/ML
Google Cloud Next Tokyo 出展
Google Cloud Day ’23 Tour 登壇
「AI は選んで組み込んで実装!最新 AI を道具として有効利用するためには!?」
■好きな Google Cloud サービス:Vertex AI / Auto ML / Visual Inspection AI / Dialogflow CX

島村裕哉をフォローする
クラウドに強いによるエンジニアブログです。
SCSKは専門性と豊富な実績を活かしたクラウドサービス USiZE(ユーサイズ)を提供しています。
USiZEサービスサイトでは、お客様のDX推進をワンストップで支援するサービスの詳細や導入事例を紹介しています。
AI・MLGoogle Cloudクラウドソリューション
シェアする
タイトルとURLをコピーしました