Vision¶
Implement api to access google vision API
-
class
pysuite.vision.
Vision
(service: google.cloud.vision_v1.ImageAnnotatorClient)[source]¶ Bases:
object
Class to interact with Google Vision API.
- Parameters
service – an authorized Google Vision service client.
-
add_request
(image_path: Union[str, pathlib.PosixPath], methods: Union[List[str], str])[source]¶ Add a request to annotate a local or GCS image. Multiple annotation methods can be added at the same time. The request will not be immediately submitted. This method is useful for batch_annotate_image and async_annotate_image.
- Example
>>> vision.add_request("/my/image.png", methods=["LABEL_DETECTION", "test_detection"])
- Example
>>> vision.add_request("gs://my_gcs_bucket/image/path.jpg", methods=["LABEL_DETECTION", "test_detection"])
- Parameters
image_path – Local or GCS path to the image file.
methods – A list of strings representing supported annotation methods. Please view google.cloud.vision_v1.types.Feature.Type for all supported methods. They are case-insensitive.
- Returns
None
-
annotate_image
(image_path: Union[str, pathlib.PosixPath], methods: Union[List[str], str]) → google.cloud.vision_v1.types.image_annotator.AnnotateImageResponse[source]¶ Submit a request to annotate a local image using specified methods.
- Parameters
image_path – Path to the image file.
methods – A list of strings representing supported annotation methods. Please view google.cloud.vision_v1.types.Feature.Type for all supported methods. They are case-insensitive.
- Returns
An AnnotateImageResponse object with annotated content.
-
async_annotate_image
(output_gcs_uri: str, batch_size: int = 0) → Optional[google.api_core.operation.Operation][source]¶ Annotate images asynchronously and place output in Google Cloud Storage in batches.
- Parameters
output_gcs_uri – Target output location on Google Cloud Storage.
batch_size – Maximum number request processed in each batch. If there are 10 requests submitted together, and batch_size is 2, there will be 5 output files (batches) created. Default is 0. Meaning only create one batch.
- Returns
An Operation object. You can use it to wait until the process is completed.
-
batch_annotate_image
() → Optional[google.cloud.vision_v1.types.image_annotator.BatchAnnotateImagesResponse][source]¶ Submit the prepared requests to annotate images and return a response with annotated content. You must first call add_request to prepare the configurations. If no configurations were prepared, this method will return None.
- Returns
An BatchAnnotateImagesResponse object with annotated content. Or None if no requests were prepared.
-
static
load_image
(image_path: Union[str, pathlib.PosixPath]) → google.cloud.vision_v1.types.image_annotator.Image[source]¶ Load a local image as Image class that can be used to submit image annotation requests.
- Parameters
image_path – Path to the image file.
- Returns
Loaded Image object from the target file
-
static
to_json
(response: Union[google.cloud.vision_v1.types.image_annotator.AnnotateImageResponse, google.cloud.vision_v1.types.image_annotator.BatchAnnotateImagesResponse]) → dict[source]¶ Convert possible image responses to dictionary. If it’s not a supported response, a type error will be raised.
- Parameters
response – A response returned from annotate_image or batch_annotate_image.
- Returns
A dictionary containing annotated contents.