Ejemplo n.º 1
0
def async_detect_document(gcs_source_uri, gcs_destination_uri):
    # Supported mime_types are: 'application/pdf' and 'image/tiff'
    mime_type = 'application/pdf'

    # How many pages should be grouped into each json output file.
    # With a file of 5 pages
    batch_size = 2

    client = vision.ImageAnnotatorClient()

    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)

    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(gcs_source=gcs_source,
                                            mime_type=mime_type)

    gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.types.OutputConfig(gcs_destination=gcs_destination,
                                              batch_size=batch_size)

    async_request = vision.types.AsyncAnnotateFileRequest(
        features=[feature],
        input_config=input_config,
        output_config=output_config)

    client.async_batch_annotate_files(requests=[async_request])
Ejemplo n.º 2
0
    def test_async_batch_annotate_files(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.AsyncBatchAnnotateFilesResponse(
            **expected_response)
        operation = operations_pb2.Operation(
            name='operations/test_async_batch_annotate_files', done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = vision_v1p2beta1.ImageAnnotatorClient(channel=channel)

        # Setup Request
        requests = []

        response = client.async_batch_annotate_files(requests)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.AsyncBatchAnnotateFilesRequest(
            requests=requests)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_async_batch_annotate_files(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.AsyncBatchAnnotateFilesResponse(
            **expected_response)
        operation = operations_pb2.Operation(
            name="operations/test_async_batch_annotate_files", done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = vision_v1p2beta1.ImageAnnotatorClient()

        # Setup Request
        requests = []

        response = client.async_batch_annotate_files(requests)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.AsyncBatchAnnotateFilesRequest(
            requests=requests)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Ejemplo n.º 4
0
def _google_pdf_ocr(guid, account, train, thread_storage):
    if getattr(thread_storage, 'vision_client', None) is None:
        thread_storage.vision_client = gv.ImageAnnotatorClient()
    if getattr(thread_storage, 'storage_client', None) is None:
        thread_storage.storage_client = gs.Client()
    if getattr(thread_storage, 'logger', None) is None:
        thread_storage.logger = get_logger('utils.ocr')
    if _check_duplicate(guid, thread_storage):
        thread_storage.logger.warning(
            'Skipping {} because it has already been OCRd'.format(guid))
        return

    src_uri = _locate_src_file(guid, account, train, thread_storage)
    gcs_src = gv.types.GcsSource(uri=src_uri)
    input_config = gv.types.InputConfig(gcs_source=gcs_src,
                                        mime_type=GOOGLE_MIME_TYPE)
    dst_uri = _locate_dst_file(guid)
    gcs_dst = gv.types.GcsDestination(uri=dst_uri)
    output_config = gv.types.OutputConfig(gcs_destination=gcs_dst,
                                          batch_size=1)
    feature = gv.types.Feature(
        type=gv.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)

    async_request = gv.types.AsyncAnnotateFileRequest(
        features=[feature],
        input_config=input_config,
        output_config=output_config)
    thread_storage.vision_client.async_batch_annotate_files(
        requests=[async_request])
Ejemplo n.º 5
0
def run_google_vision(img_urls_dict):
    print("Classifying Imgs. w. Google Vision API...")

    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()

    for search_term in img_urls_dict.keys():
        img_urls = img_urls_dict[search_term]

        img_classified_dict = {}
        img_classified_dict[search_term] = {}

        for image_uri in img_urls:
            try:
                image.source.image_uri = image_uri
                response = client.label_detection(image=image)
                img_classified_dict[image_uri] = {}

                for label in response.label_annotations:
                    img_classified_dict[search_term][image_uri] = {}
                    img_classified_dict[search_term][image_uri][
                        label.description] = label.score

            except:
                pass

    return img_classified_dict
    def test_batch_annotate_images(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response
        )

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = vision_v1p2beta1.ImageAnnotatorClient()

        # Setup Request
        requests = []

        response = client.batch_annotate_images(requests)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Ejemplo n.º 7
0
    def test_batch_annotate_images_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = vision_v1p2beta1.ImageAnnotatorClient(channel=channel)

        # Setup request
        requests = []

        with pytest.raises(CustomException):
            client.batch_annotate_images(requests)
def sample_batch_annotate_images():
    # Create a client
    client = vision_v1p2beta1.ImageAnnotatorClient()

    # Initialize request argument(s)
    request = vision_v1p2beta1.BatchAnnotateImagesRequest()

    # Make the request
    response = client.batch_annotate_images(request=request)

    # Handle the response
    print(response)
    def test_batch_annotate_images_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = vision_v1p2beta1.ImageAnnotatorClient()

        # Setup request
        requests = []

        with pytest.raises(CustomException):
            client.batch_annotate_images(requests)
Ejemplo n.º 10
0
def async_detect_document(gcs_source_uri, gcs_destination_uri):
    """Attempt ocr on a single gs:// uri

  Makes an async call to a gs:// uri to do ocr

  Args:
    gcs_source_uri: gs:// uri of file to attempt ocr on
    gcs_destination_uri: gs:// uri of where to write .json output
      output-x-to-y.json will be added to the end of this, where
      x is first page in the json and y is the last page. Up to
      100 pages are in each json file

  Raises:
    error: catches all Exceptions (should exclude KeyboardInterrupt and other
      trivial exceptions)
  """
    # adapted from https://github.com/GoogleCloudPlatform/python-docs-samples/
    #   blob/7405c0011ef6a691444349a91d8d6e90923f636c/vision/cloud-client/
    #   detect/detect_pdf.py

    try:
        # Supported mime_types are: 'application/pdf' and 'image/tiff'
        mime_type = 'application/pdf'

        # How many pages should be grouped into each json output file.
        batch_size = 100

        client = vision.ImageAnnotatorClient()

        feature = vision.types.Feature(
            type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
        gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
        input_config = vision.types.InputConfig(gcs_source=gcs_source,
                                                mime_type=mime_type)

        gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
        output_config = vision.types.OutputConfig(
            gcs_destination=gcs_destination, batch_size=batch_size)

        async_request = vision.types.AsyncAnnotateFileRequest(
            features=[feature],
            input_config=input_config,
            output_config=output_config)

        operation = client.async_batch_annotate_files(requests=[async_request])
        # todo(michaelsherman) catch and log the operation ID
        response = operation.result(timeout=150)
        return response
    # intentional, catch everything and pass up the stack for report
    except Exception as error:
        raise error
Ejemplo n.º 11
0
def sample_async_batch_annotate_files():
    # Create a client
    client = vision_v1p2beta1.ImageAnnotatorClient()

    # Initialize request argument(s)
    request = vision_v1p2beta1.AsyncBatchAnnotateFilesRequest()

    # Make the request
    operation = client.async_batch_annotate_files(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
Ejemplo n.º 12
0
    def test_async_batch_annotate_files_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_async_batch_annotate_files_exception',
            done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = vision_v1p2beta1.ImageAnnotatorClient(channel=channel)

        # Setup Request
        requests = []

        response = client.async_batch_annotate_files(requests)
        exception = response.exception()
        assert exception.errors[0] == error
Ejemplo n.º 13
0
def run_google_vision(
        img_urls_dict: Dict[str, List[str]]) -> Dict[str, Dict[str, Any]]:
    """
       Use the Google vision API to return a set of classification labels for each image collected from 
       Google using the search_and_download function. Each label assigned by Google vision is associated 
       with a score indicating Google's confidence in the fit fo the label for the image.
       
       img_urls_dict: dictionary containing image_urls
    """

    log = get_logger("run_google_vision")

    log.info("Classifying Imgs. w. Google Vision API...")

    # copy environment variable from configuration to the name where google API expects to find it
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getenv(
        "COMPSYN_GOOGLE_APPLICATION_CREDENTIALS")

    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()

    for search_term in img_urls_dict.keys():
        img_urls = img_urls_dict[search_term]
        img_classified_dict = {}
        img_classified_dict[search_term] = {}
        log.info(f"Classifying {len(img_urls)} images for {search_term}")

        for image_uri in img_urls:
            try:
                image.source.image_uri = image_uri
                response = client.label_detection(image=image)
                img_classified_dict[image_uri] = {}

                for label in response.label_annotations:
                    img_classified_dict[search_term][image_uri] = {}
                    img_classified_dict[search_term][image_uri][
                        label.description] = label.score

            except:
                pass

    return img_classified_dict
Ejemplo n.º 14
0
def settings(
    application_cred_name: str,
    driver_browser: str,
    driver_executable_path: str,
    driver_options: Optional[List[str]] = None,
) -> None:
    # This client for the Google API needs to be set for the VISION classification
    # but it is not necessary for the selenium scaper for image downloading
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = application_cred_name
    client = vision.ImageAnnotatorClient(
    )  # authentification via environment variable

    # See here for scraper details:
    # https://towardsdatascience.com/image-scraping-with-python-a96feda8af2d
    wd = get_webdriver(
        driver_browser=driver_browser,
        driver_executable_path=driver_executable_path,
        driver_options=driver_options,
    )
    wd.quit()
Ejemplo n.º 15
0
    def test_batch_annotate_images(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = vision_v1p2beta1.ImageAnnotatorClient(channel=channel)

        # Setup Request
        requests = []

        response = client.batch_annotate_images(requests)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_async_batch_annotate_files_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name="operations/test_async_batch_annotate_files_exception",
            done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = vision_v1p2beta1.ImageAnnotatorClient()

        # Setup Request
        requests = []

        response = client.async_batch_annotate_files(requests)
        exception = response.exception()
        assert exception.errors[0] == error
Ejemplo n.º 17
0
def run_google_vision(
        img_urls_dict: Dict[str, List[str]]) -> Dict[str, Dict[str, Any]]:
    """
       Use the Google vision API to return a set of classification labels for each image collected from 
       Google using the search_and_download function. Each label assigned by Google vision is associated 
       with a score indicating Google's confidence in the fit fo the label for the image.
       
       img_urls_dict: dictionary containing image_urls
    """

    get_logger("run_google_vision").info(
        "Classifying Imgs. w. Google Vision API...")

    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()

    for search_term in img_urls_dict.keys():
        img_urls = img_urls_dict[search_term]
        img_classified_dict = {}
        img_classified_dict[search_term] = {}

        for image_uri in img_urls:
            try:
                image.source.image_uri = image_uri
                response = client.label_detection(image=image)
                img_classified_dict[image_uri] = {}

                for label in response.label_annotations:
                    img_classified_dict[search_term][image_uri] = {}
                    img_classified_dict[search_term][image_uri][
                        label.description] = label.score

            except:
                pass

    return img_classified_dict
Ejemplo n.º 18
0
def OCRfromPILImage(pilImg):
    img_data = GetPILImageBytes(pilImg)
    req_body = {
        'image': {
            'content': img_data
        },
        'features': [{
            'type': 'TEXT_DETECTION'
        }]
    }

    try:
        cred = get_credential()
    except SSTRAuthException as sstrex:
        return str(sstrex)

    client = vision_v1p2beta1.ImageAnnotatorClient(credentials=cred)
    result = client.annotate_image(req_body)

    fw = open(str(responselog_path), "w", encoding="utf-8")
    fw.write(str(result))
    fw.close()
    pilImg.save(imagelog_path)
    # jsonl = json.loads(res.text)
    jsonl = result
    try:
        if len(jsonl.text_annotations) == 0:
            return ""
        ocr = jsonl.text_annotations[0].description
        ocr = ocr.replace("\n", " ")
        return ocr
    except:
        import traceback
        traceback.print_exc()
        #jsonl内容はLogを見ること
        return "OCR ERROR!!"
Ejemplo n.º 19
0
import json
import rapidjson
import os
from pprint import pprint
from IPython.display import clear_output
from IPython.display import Image
sns.set(style='whitegrid')
from google.cloud import vision_v1p2beta1 as vision
from google.protobuf.json_format import MessageToDict
from google_images_download import google_images_download  #importing the library

# In[2]:

# on the line below, replace the file directory with the directory for your own credential JSON file
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'COMPSYN2-18e251c693df.json'
client = vision.ImageAnnotatorClient(
)  # authentification via environment variable

# In[3]:


# PHASE 1:
def build_image(input_string):
    '''builds image object'''

    if input_string.startswith('http') or input_string.startswith(
            'gs:'):  # online images
        image = vision.types.Image()
        image.source.image_uri = input_string
    else:  # local images
        with open(input_string, 'rb') as image_file:
            content = image_file.read()
Ejemplo n.º 20
0
def async_detect_document(gcs_source_uri, gcs_destination_uri):
    # Supported mime_types are: 'application/pdf' and 'image/tiff'
    mime_type = 'application/pdf'

    # How many pages should be grouped into each json output file.
    # With a file of 5 pages
    batch_size = 2

    client = vision.ImageAnnotatorClient()

    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)

    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(gcs_source=gcs_source,
                                            mime_type=mime_type)

    gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.types.OutputConfig(gcs_destination=gcs_destination,
                                              batch_size=batch_size)

    async_request = vision.types.AsyncAnnotateFileRequest(
        features=[feature],
        input_config=input_config,
        output_config=output_config)

    operation = client.async_batch_annotate_files(requests=[async_request])

    print('Waiting for the operation to finish.')
    operation.result(timeout=90)

    # Once the request has completed and the output has been
    # written to GCS, we can list all the output files.
    storage_client = storage.Client()

    match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
    bucket_name = match.group(1)
    prefix = match.group(2)

    bucket = storage_client.get_bucket(bucket_name=bucket_name)

    # List objects with the given prefix.
    blob_list = list(bucket.list_blobs(prefix=prefix))
    print('Output files:')
    for blob in blob_list:
        print(blob.name)

    # Process the first output file from GCS.
    # Since we specified batch_size=2, the first response contains
    # the first two pages of the input file.
    output = blob_list[0]

    json_string = output.download_as_string()
    response = json_format.Parse(json_string,
                                 vision.types.AnnotateFileResponse())

    # The actual response for the first page of the input file.
    first_page_response = response.responses[0]
    annotation = first_page_response.full_text_annotation

    # Here we print the full text from the first page.
    # The response contains more information:
    # annotation/pages/blocks/paragraphs/words/symbols
    # including confidence scores and bounding boxes
    print(u'Full text:\n{}'.format(annotation.text))