| 47 | + # dataset_name = '[DATASET_NAME]' |
| 48 | + |
| 49 | + from google.cloud import automl_v1beta1 as automl |
| 50 | + |
| 51 | + client = automl.AutoMlClient() |
| 52 | + |
| 53 | + # A resource that represents Google Cloud Platform location. |
| 54 | + project_location = client.location_path(project_id, compute_region) |
| 55 | + |
| 56 | + # Set dataset name and metadata of the dataset. |
| 57 | + my_dataset = { |
| 58 | + 'display_name': dataset_name, |
| 59 | + 'image_object_detection_dataset_metadata': {}, |
| 60 | + } |
| 61 | + |
| 62 | + # Create a dataset with the dataset metadata in the region. |
| 63 | + dataset = client.create_dataset(project_location, my_dataset) |
| 64 | + |
| 65 | + # Display the dataset information. |
| 66 | + print('Dataset name: {}'.format(dataset.name)) |
| 67 | + print('Dataset id: {}'.format(dataset.name.split('/')[-1])) |
| 68 | + print('Dataset display name: {}'.format(dataset.display_name)) |
| 69 | + print('Dataset example count: {}'.format(dataset.example_count)) |
| 70 | + print('Dataset create time:') |
| 71 | + print('\tseconds: {}'.format(dataset.create_time.seconds)) |
| 72 | + print('\tnanos: {}'.format(dataset.create_time.nanos)) |
| 73 | + dataset_spec = {} |
| 74 | + my_dataset = { |
| 75 | + 'display_name': dataset_name, |
| 76 | + 'image_object_detection_dataset_metadata': dataset_spec |
| 77 | + } |
| 78 | + response = client.create_dataset(project_location, my_dataset) |
| 79 | + print('\nDataset creation: {}'.format(response)) |
| 80 | + dataset_full_id = response.name |
| 81 | + print('Dataset full id: {}'.format(dataset_full_id)) |
| 82 | + |
| 83 | + # [END automl_vision_iod_import_data] |
| 84 | + |
| 85 | + |
| 86 | +def get_dataset(project_id, compute_region, dataset_id): |
| 87 | + """Describes a dataset.""" |
| 88 | + # [START automl_vision_iod_get_dataset] |
| 89 | + ## To do: Uncomment and set the following variables |
| 90 | + # project_id = '[PROJECT_ID]'
6D40
|
| 91 | + # compute_region = '[COMPUTE_REGION]' |
| 92 | + # dataset_id = '[DATASET_ID]' |
| 93 | + |
| 94 | + from google.cloud import automl_v1beta1 as automl |
| 95 | + |
| 96 | + client = automl.AutoMlClient() |
| 97 | + |
| 98 | + # A resource that represents Google Cloud Platform location. |
| 99 | + project_location = client.location_path(project_id, compute_region) |
| 100 | + |
| 101 | + # Get the full path of the dataset. |
| 102 | + dataset_full_id = client.dataset_path(project_id, compute_region, dataset_id) |
| 103 | + |
| 104 | + # Get the dataset |
| 105 | + response = client.get_dataset(dataset_full_id) |
| 106 | + print('\nDataset description: {}'.format(response)) |
| 107 | + |
| 108 | + # [END automl_vision_iod_get_dataset] |
| 109 | + |
| 110 | + |
| 111 | +def list_datasets(project_id, compute_region, filter_): |
| 112 | + """Lists all datasets.""" |
| 113 | + # [START automl_vision_iod_lists_dataset] |
| 114 | + ## To do: Uncomment and set the following variables |
| 115 | + # project_id = '[PROJECT_ID]' |
| 116 | + # compute_region = '[COMPUTE_REGION]' |
| 117 | + # filter_ = '' |
| 118 | + |
| 119 | + from google.cloud import automl_v1beta1 as automl |
| 120 | + |
| 121 | + client = automl.AutoMlClient() |
| 122 | + |
| 123 | + # A resource that represents Google Cloud Platform location. |
| 124 | + project_location = client.location_path(project_id, compute_region) |
| 125 | + |
| 126 | + # List all the datasets available in the region by applying filter. |
| 127 | + response = client.list_datasets(project_location, filter_=filter_) |
| 128 | + |
| 129 | + print('List of datasets:') |
| 130 | + for dataset in response: |
| 131 | + # Display the dataset information. |
| 132 | + print('Dataset name: {}'.format(dataset.name)) |
| 133 | + print('Dataset id: {}'.format(dataset.name.split('/')[-1])) |
| 134 | + print('Dataset display name: {}'.format(dataset.display_name)) |
| 135 | + print('Dataset example count: {}'.format(dataset.example_count)) |
| 136 | + print('Dataset create time:
F438
39;) |
| 137 | + print('\tseconds: {}'.format(dataset.create_time.seconds)) |
| 138 | + print('\tnanos: {}'.format(dataset.create_time.nanos)) |
| 139 | + |
| 140 | + # [END automl_vision_iod_lists_dataset] |
| 141 | + |
| 142 | + |
| 143 | +def import_data(project_id, compute_region, dataset_id, paths): |
| 144 | + """Imports images and bounding boxes.""" |
| 145 | + # [START automl_vision_iod_import_data] |
| 146 | + ## To do: Uncomment and set the following variables |
| 147 | + # project_id = '[PROJECT_ID]' |
| 148 | + # compute_region = '[COMPUTE_REGION]' |
| 149 | + # dataset_id = '[DATASET_ID]' |
| 150 | + # paths = '[Storage path. For example: gs://path/to/file.csv]' |
| 151 | + |
| 152 | + from google.cloud import automl_v1beta1 as automl |
| 153 | + |
| 154 | + client = automl.AutoMlClient() |
| 155 | + |
| 156 | + # Get the full path of the dataset. |
| 157 | + dataset_full_id = client.dataset_path(project_id, compute_region, dataset_id) |
| 158 | + |
| 159 | + # Get the multiple Google Cloud Storage URIs. |
| 160 | + input_config = {'gcs_source': {'input_uris': paths}} |
| 161 | + |
| 162 | + # Import data from the input URI. |
| 163 | + response = client.import_data(dataset_full_id, input_config) |
| 164 | + |
| 165 | + print('Processing import...') |
| 166 | + # synchronous check of operation status. |
| 167 | + print('Data imported. {}'.format(response.result())) |
| 168 | + |
| 169 | + # [END automl_vision_iod_import_data] |
| 170 | + |
| 171 | + |
| 172 | +def delete_dataset(project_id, compute_region, dataset_id): |
| 173 | + """Deletes a dataset.""" |
| 174 | + # [START automl_vision_iod_delete_dataset] |
| 175 | + ## To do: Uncomment and set the following variables |
| 176 | + # project_id = '[PROJECT_ID]' |
| 177 | + # compute_region = '[COMPUTE_REGION]' |
| 178 | + # dataset_id = '[DATASET_ID]' |
| 179 | + |
| 180 | + from google.cloud import automl_v1beta1 as automl |
| 181 | + |
| 182 | + client = automl.AutoMlClient() |
| 183 | + |
| 184 | + # Get the full path of the dataset. |
| 185 | + dataset_full_id = client.dataset_path(project_id, compute_region, dataset_id) |
| 186 | + |
| 187 | + # Delete a dataset. |
| 188 | + response = client.delete_dataset(dataset_full_id) |
| 189 | + |
| 190 | + # synchronous check of operation status. |
| 191 | + print('Dataset deleted. {}'.format(response.result())) |
| 192 | + |
| 193 | + # [END automl_vision_iod_delete_dataset] |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == '__main__': |
| 197 | + parser = argparse.ArgumentParser( |
| 198 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 199 | + subparsers = parser.add_subparsers(dest='command') |
| 200 | + create_dataset_parser = subparsers.add_parser( |
| 201 | + 'create_dataset', help=create_dataset.__doc__) |
| 202 | + create_dataset_parser.add_argument('dataset_name') |
| 203 | + get_dataset_parser = subparsers.add_parser( |
| 204 | + 'get_dataset', help=get_dataset.__doc__) |
| 205 | + get_dataset_parser.add_argument('dataset_id') |
| 206 | + import_data_parser = subparsers.add_parser( |
| 207 | + 'import_data', help=import_data.__doc__) |
| 208 | + import_data_parser.add_argument('path') |
| 209 | + import_data_parser.add_argument('dataset_id') |
| 210 | + list_datasets_parser = subparsers.add_parser( |
| 211 | + 'list_datasets', help=list_datasets.__doc__) |
| 212 | + delete_dataset_parser = subparsers.add_parser( |
| 213 | + 'delete_dataset', help=delete_dataset.__doc__) |
| 214 | + delete_dataset_parser.add_argument('dataset_id') |
| 215 | + |
| 216 | + args = parser.parse_args() |
| 217 | + |
| 218 | + # Setup. |
| 219 | + project_id = os.environ['PROJECT_ID'] |
| 220 | + compute_region = 'us-central1' |
| 221 | + |
| 222 | + if args.command == 'create_dataset': |
| 223 | + create_dataset(project_id, compute_region, args.dataset_name) |
| 224 | + if args.command == 'get_dataset': |
| 225 | + get_dataset(project_id, compute_region, args.dataset_id) |
| 226 | + if args.command == 'delete_dataset': |
| 227 | + delete_dataset(project_id, compute_region, args.dataset_id) |
| 228 | + if args.command == 'list_datasets': |
| 229 | + list_datasets(project_id, compute_region, '') |
| 230 | + if args.command == 'import_data': |
| 231 | + import_data(project_id, compute_region, args.dataset_id, [args.path]) |
0 commit comments