-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathreference_image_management.py
executable file
·200 lines (166 loc) · 7.65 KB
/
reference_image_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This application demonstrates how to perform basic operations on reference
images in Cloud Vision Product Search.
For more information, see the tutorial page at
https://cloud.google.com/vision/product-search/docs/
"""
import argparse
# [START vision_product_search_create_reference_image]
# [START vision_product_search_delete_reference_image]
# [START vision_product_search_list_reference_images]
# [START vision_product_search_get_reference_image]
from google.cloud import vision
# [END vision_product_search_create_reference_image]
# [END vision_product_search_delete_reference_image]
# [END vision_product_search_list_reference_images]
# [END vision_product_search_get_reference_image]
# [START vision_product_search_create_reference_image]
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))
# [END vision_product_search_create_reference_image]
# [START vision_product_search_list_reference_images]
def list_reference_images(
project_id, location, product_id):
"""List all images in a product.
Args:
project_id: Id of the project.
location: A compute region name.
product_id: Id of the product.
"""
client = vision.ProductSearchClient()
# Get the full path of the product.
product_path = client.product_path(
project=project_id, location=location, product=product_id)
# List all the reference images available in the product.
reference_images = client.list_reference_images(parent=product_path)
# Display the reference image information.
for image in reference_images:
print('Reference image name: {}'.format(image.name))
print('Reference image id: {}'.format(image.name.split('/')[-1]))
print('Reference image uri: {}'.format(image.uri))
print('Reference image bounding polygons: {}'.format(
image.bounding_polys))
# [END vision_product_search_list_reference_images]
# [START vision_product_search_get_reference_image]
def get_reference_image(
project_id, location, product_id, reference_image_id):
"""Get info about 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.
"""
client = vision.ProductSearchClient()
# Get the full path of the reference image.
reference_image_path = client.reference_image_path(
project=project_id, location=location, product=product_id,
reference_image=reference_image_id)
# Get complete detail of the reference image.
image = client.get_reference_image(name=reference_image_path)
# Display the reference image information.
print('Reference image name: {}'.format(image.name))
print('Reference image id: {}'.format(image.name.split('/')[-1]))
print('Reference image uri: {}'.format(image.uri))
print('Reference image bounding polygons: {}'.format(image.bounding_polys))
# [END vision_product_search_get_reference_image]
# [START vision_product_search_delete_reference_image]
def delete_reference_image(
project_id, location, product_id, reference_image_id):
"""Delete 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.
"""
client = vision.ProductSearchClient()
# Get the full path of the reference image.
reference_image_path = client.reference_image_path(
project=project_id, location=location, product=product_id,
reference_image=reference_image_id)
# Delete the reference image.
client.delete_reference_image(name=reference_image_path)
print('Reference image deleted from product.')
# [END vision_product_search_delete_reference_image]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='command')
parser.add_argument(
'--project_id',
help='Project id. Required',
required=True)
parser.add_argument(
'--location',
help='Compute region name',
default='us-west1')
create_reference_image_parser = subparsers.add_parser(
'create_reference_image', help=create_reference_image.__doc__)
create_reference_image_parser.add_argument('product_id')
create_reference_image_parser.add_argument('reference_image_id')
create_reference_image_parser.add_argument('gcs_uri')
list_reference_images_parser = subparsers.add_parser(
'list_reference_images',
help=list_reference_images.__doc__)
list_reference_images_parser.add_argument('product_id')
get_reference_image_parser = subparsers.add_parser(
'get_reference_image', help=get_reference_image.__doc__)
get_reference_image_parser.add_argument('product_id')
get_reference_image_parser.add_argument('reference_image_id')
delete_reference_image_parser = subparsers.add_parser(
'delete_reference_image', help=delete_reference_image.__doc__)
delete_reference_image_parser.add_argument('product_id')
delete_reference_image_parser.add_argument('reference_image_id')
args = parser.parse_args()
if args.command == 'create_reference_image':
create_reference_image(
args.project_id, args.location, args.product_id,
args.reference_image_id, args.gcs_uri)
elif args.command == 'list_reference_images':
list_reference_images(
args.project_id, args.location, args.product_id)
elif args.command == 'get_reference_image':
get_reference_image(
args.project_id, args.location, args.product_id,
args.reference_image_id)
elif args.command == 'delete_reference_image':
delete_reference_image(
args.project_id, args.location, args.product_id,
args.reference_image_id)