Exemplo n.º 1
0
    def test_explicit_features(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = vision_v1.Image(source={"image_uri": "http://foo.com/img.jpg"})
        request = vision_v1.AnnotateImageRequest(
            image=image,
            features=[
                vision_v1.Feature(type_=1),
                vision_v1.Feature(type_=2),
                vision_v1.Feature(type_=3),
            ],
        )

        # Perform the single image request.
        self.client.annotate_image(request)

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(kwargs["requests"]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = kwargs["requests"][0]
        assert request_sent.image == request.image
        assert len(request_sent.features) == 3
        for feature, i in zip(request_sent.features, range(1, 4)):
            assert feature.type_ == i
            assert feature.max_results == 0
Exemplo n.º 2
0
def detect_document(path):
    """Detects document features in an image."""
    client = vision_v1.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision_v1.Image(content=content)

    response = client.document_text_detection(image=image)
    """
    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            print('\nBlock confidence: {}\n'.format(block.confidence))

            for paragraph in block.paragraphs:
                print('Paragraph confidence: {}'.format(
                    paragraph.confidence))

                for word in paragraph.words:
                    word_text = ''.join([
                        symbol.text for symbol in word.symbols
                    ])
                    print('Word text: {} (confidence: {})'.format(
                        word_text, word.confidence))

                    for symbol in word.symbols:
                        print('\tSymbol: {} (confidence: {})'.format(
                            symbol.text, symbol.confidence))
"""
    if response.error.message:
        raise Exception('{}\nFor more info on error messages, check: '
                        'https://cloud.google.com/apis/design/errors'.format(
                            response.error.message))
Exemplo n.º 3
0
    def _get_labels_of_batch(self, image_docs):
        """ Labels the images in the batch using one call to the Google Vision API.
            Used to label each batch in the class's process method.

        Args:
            image_docs: list of up to _MAX_IMAGES_IN_BATCH image docs represented by dictionaries as
            stored in the database_schema.COLLECTION_IMAGES.

        """
        features = [{"type_": vision_v1.Feature.Type.LABEL_DETECTION}]
        requests = []
        results = []
        docs = []
        for doc in image_docs:
            url = doc[constants.FIELD_URL_FOR_RECOGNITION_API]
            image = vision_v1.Image()
            image.source.image_uri = url
            request = vision_v1.AnnotateImageRequest(image=image,
                                                     features=features)
            requests.append(request)
            docs.append(doc)
        batch_request = vision_v1.BatchAnnotateImagesRequest(requests=requests)
        response = self.client.batch_annotate_images(request=batch_request)
        for i, image_response in enumerate(response.responses):
            all_labels = [
                label.description.lower()
                for label in image_response.label_annotations
            ]
            results.append([(docs[i], all_labels)])
        return results
Exemplo n.º 4
0
def ocr_reading(result):
    client = vision.ImageAnnotatorClient()
    print('client', client, result, vision)
    # image = vision.types.Image()
    image = vision.Image()
    image.source.image_uri = result['secure_url']
    response = client.text_detection(image=image)
    texts = response.text_annotations
    return texts and texts[0].description
Exemplo n.º 5
0
def detect_web(path):
    """Detects web annotations given an image."""
    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision_v1.Image(content=content)

    response = client.web_detection(image=image)
    annotations = response.web_detection

    if annotations.best_guess_labels:
        for label in annotations.best_guess_labels:
            print('\nBest guess label: {}'.format(label.label))

    if annotations.pages_with_matching_images:
        print('\n{} Pages with matching images found:'.format(
            len(annotations.pages_with_matching_images)))

        for page in annotations.pages_with_matching_images:
            print('\n\tPage url   : {}'.format(page.url))

            if page.full_matching_images:
                print('\t{} Full Matches found: '.format(
                    len(page.full_matching_images)))

                for image in page.full_matching_images:
                    print('\t\tImage url  : {}'.format(image.url))

            if page.partial_matching_images:
                print('\t{} Partial Matches found: '.format(
                    len(page.partial_matching_images)))

                for image in page.partial_matching_images:
                    print('\t\tImage url  : {}'.format(image.url))

    if annotations.web_entities:
        print('\n{} Web entities found: '.format(len(
            annotations.web_entities)))

        for entity in annotations.web_entities:
            print('\n\tScore      : {}'.format(entity.score))
            print(u'\tDescription: {}'.format(entity.description))

    if annotations.visually_similar_images:
        print('\n{} visually similar images found:\n'.format(
            len(annotations.visually_similar_images)))

        for image in annotations.visually_similar_images:
            print('\tImage url    : {}'.format(image.url))

    if response.error.message:
        raise Exception('{}\nFor more info on error messages, check: '
                        'https://cloud.google.com/apis/design/errors'.format(
                            response.error.message))
Exemplo n.º 6
0
def img(bot, update):
    chat_id = update.message.chat_id
    bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
    image_id = update.message.photo[-1].get_file()
    image_id = image_id.file_id

    bot.send_message(chat_id=chat_id, text='🔥 Analyzing image, be patient ! 🔥')

    # prepare image for downlading
    file_path = bot.get_file(image_id).file_path

    # generate image download url
    #image_url = "https://api.telegram.org/file/bot{0}/{1}".format(TELEGRAM_TOKEN, file_path)
    image_url = file_path
    print(image_url)

    # create folder to store pic temporary, if it doesnt exist
    if not os.path.exists(result_storage_path):
        os.makedirs(result_storage_path)

    # retrieve and save image
    image_name = "{0}.jpg".format(image_id)
    urllib.request.urlretrieve(
        image_url, "{0}/{1}".format(result_storage_path, image_name))

    #return image_name

    print(image_name)
    image_name = result_storage_path + '/' + image_name
    print(image_name)
    #r = recog(image_name)
    #reply = out

    with io.open(os.path.join(image_name), 'rb') as image_file:
        content = image_file.read()
    # image = vision_v1.types.Image(content=content)
    image = vision_v1.Image(content=content)
    response = client.web_detection(image=image)
    annotations = response.web_detection
    res = annotations.__class__.to_json(annotations)
    dataimg = json.loads(res)
    jdump = json.dumps(res, indent=4)
    #print(dataimg["webEntities"][0]["description"])
    print(res)
    print(dataimg)
    print(jdump)

    out = dataimg["webEntities"][0]["description"] + '\n' + '\n'
    out = out + dataimg["webEntities"][1]["description"] + '\n' + '\n'
    out = out + dataimg["webEntities"][2]["description"] + '\n' + '\n'
    out = out + dataimg["webEntities"][3]["description"] + '\n' + '\n'

    bot.send_message(chat_id=chat_id, text=out)
Exemplo n.º 7
0
def recog(image_name):
    with io.open(os.path.join(image_name), 'rb') as image_file:
        content = image_file.read()
    #image = vision_v1.types.Image(content=content)
    image = vision_v1.Image(content=content)
    response = client.web_detection(image=image)
    annotations = response.web_detection
    res = annotations.__class__.to_json(annotations)
    dataimg = json.loads(res)
    jdump = json.dumps(res, indent=4)
    print(dataimg["webEntities"][0]["description"])
    print(dataimg)
    print(res)
    print(jdump)
    out = dataimg["webEntities"][0]["description"] + '\n' + '\n'
Exemplo n.º 8
0
    def get_crop_hint(crop_hints):
        """Detect crop hints on a single image and return the first result."""
        with io.open(crop_hints, 'rb') as image_file1:
            content = image_file1.read()

        # content = crop_hints
        image = vision.Image(content=content)

        image_context = vision.ImageContext(
            crop_hints_params=crop_hints_params)

        response = client.crop_hints(image=image, image_context=image_context)
        hints = response.crop_hints_annotation.crop_hints

        # Get bounds for the first crop hint using an aspect ratio of 1.77.
        vertices = hints[0].bounding_poly.vertices

        return vertices
Exemplo n.º 9
0
    def test_all_features_default(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = vision_v1.Image(source={"image_uri": "http://foo.com/img.jpg"})
        request = vision_v1.AnnotateImageRequest(image=image)
        assert not request.features

        # Perform the single image request.
        self.client.annotate_image(request)

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(kwargs["requests"]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = kwargs["requests"][0]
        all_features = self.client._get_all_features()
        assert request_sent.image == request.image
        assert len(request_sent.features) == len(all_features)
Exemplo n.º 10
0
    def extract_text(self, path):
        '''
        Single image text extraction
        PARAMS:
            str path: filepath of image
        '''

        with io.open(path, 'rb') as image_file:
            content = image_file.read()

        image = vision.Image(content=content)

        response = self.client.text_detection(image=image)

        if response.error.message:
            raise Exception(
                '{}\nFor more info on error messages, check: '
                'https://cloud.google.com/apis/design/errors'.format(
                    response.error.message))

        return response.full_text_annotation.text
Exemplo n.º 11
0
 def process(self, element):
     features = [{"type_": vision_v1.Feature.Type.LABEL_DETECTION}]
     requests = []
     results = []
     urls = []
     for url in element[1]:
         if url:
             image = vision_v1.Image()
             image.source.image_uri = url
             request = vision_v1.AnnotateImageRequest(image=image,
                                                      features=features)
             requests.append(request)
             urls.append(url)
     batchRequest = vision_v1.BatchAnnotateImagesRequest(requests=requests)
     response = self.client.batch_annotate_images(request=batchRequest)
     for i, image_response in enumerate(response.responses):
         allLabels = [
             label.description for label in image_response.label_annotations
         ]
         results.append([(urls[i], allLabels)])
     return results
Exemplo n.º 12
0
def detect_logo(source_image, max_results):
    """Detects popular product logos within an image"""

    # Instantiates a client
    client = vision.ImageAnnotatorClient()

    # Loads the image from local
    with io.open(source_image, 'rb') as image_file:
        content = image_file.read()
    image = vision.Image(content=content)

    # Perform logo detection
    response = client.logo_detection(
        image=image, max_results=max_results)
    logos = response.logo_annotations

    # Count detected logo
    if logos:
        print("found {} logo{}\n".format(
            len(logos), "" if len(logos) == 1 else "s")) 
    else:
        print("no logo detected.")

    # Show logo information
    for logo in logos:
        confidence = int(logo.score * 100)
        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in logo.bounding_poly.vertices])
        print("{} ({}% confidence)".format(logo.description, confidence))
        print("\tBounds : {}".format(','.join(vertices))) 

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

    return logos
Exemplo n.º 13
0
import os
import io

from google.cloud import vision_v1

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'VisionAPI/token.json'

client = vision_v1.ImageAnnotatorClient()

#### REMOTE IMAGE #####

image = vision_v1.Image()
image.source.image_uri = 'https://n.nordstrommedia.com/id/sr3/33bc5277-2935-4c23-8d11-560ff131fd02.jpeg?crop=pad&pad_color=FFF&format=jpeg&trim=color&trimcolor=FFF&w=780&h=838'

#### LABEL DETECTION ####

response_label = client.label_detection(image=image)

for label in response_label.label_annotations:
    print({'label': label.description, 'score': label.score})

#### LOGO DETECTION ####

response = client.logo_detection(image=image)

logos = response.logo_annotations
print('Logos:')

for logo in logos:
    print(logo.description)
Exemplo n.º 14
0
import os
import io

from google.cloud import vision_v1

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'VisionAPI/token.json'

client = vision_v1.ImageAnnotatorClient()

#### LOCAL IMAGE ####

with io.open('VisionAPI/images/bag.webp', 'rb') as image_file:
    content = image_file.read()

image = vision_v1.Image(content=content)

#### LABEL DETECTION ####

response_label = client.label_detection(image=image)

for label in response_label.label_annotations:
    print({'label': label.description, 'score': label.score})

#### LOGO DETECTION ####

response = client.logo_detection(image=image)

logos = response.logo_annotations
print('Logos:')

for logo in logos:
Exemplo n.º 15
0
    def result(self, path, result=None):
        '''Returns the result from calling the service on the 'file_path'.
        The result is returned as an TRResult named tuple.
        '''

        # Delay loading the API packages until needed because they take time to
        # load.  Doing this speeds up overall application start time.
        import google
        from google.cloud import vision_v1 as gv
        from google.api_core.exceptions import PermissionDenied
        from google.protobuf.json_format import MessageToDict

        if not result:
            # Read the image and proceed with contacting the service.
            (image, error) = self._image_from_file(path)
            if error:
                return error

            if __debug__:
                log(f'building Google API object for {relative(path)}')
            try:
                client = gv.ImageAnnotatorClient()
                params = gv.TextDetectionParams(
                    mapping={'enable_text_detection_confidence_score': True})
                context = gv.ImageContext(language_hints=['en-t-i0-handwrit'],
                                          text_detection_params=params)
                img = gv.Image(content=image)
                if __debug__:
                    log(f'sending image to Google for {relative(path)} ...')
                response = client.document_text_detection(
                    image=img, image_context=context)
                if __debug__:
                    log(f'received result from Google for {relative(path)}')
                result = dict_from_response(response)
            except google.api_core.exceptions.PermissionDenied as ex:
                text = 'Authentication failure for Google service -- {}'.format(
                    ex)
                raise AuthFailure(text)
            except google.auth.exceptions.DefaultCredentialsError as ex:
                text = 'Credentials file error for Google service -- {}'.format(
                    ex)
                raise AuthFailure(text)
            except google.api_core.exceptions.ServiceUnavailable as ex:
                text = 'Network, service, or Google configuration error -- {}'.format(
                    ex)
                raise ServiceFailure(text)
            except KeyboardInterrupt as ex:
                raise
            except Exception as ex:
                if isinstance(ex, KeyError):
                    # Can happen if you control-C in the middle of the Google call.
                    # Result is "Exception ignored in: 'grpc._cython.cygrpc._next'"
                    # printed to the terminal and we end up here.
                    raise KeyboardInterrupt
                else:
                    text = 'Error: {} -- {}'.format(str(ex), path)
                    return TRResult(path=path,
                                    data={},
                                    boxes=[],
                                    text='',
                                    error=text)

        raise_for_interrupts()
        boxes = []
        # See this page for more information about the structure:
        # https://cloud.google.com/vision/docs/handwriting#python
        if len(result['full_text_annotation']['pages']) > 1:
            warn('More than one page received from Google; using only first.')
        for block in result['full_text_annotation']['pages'][0]['blocks']:
            for para in block['paragraphs']:
                corners = corner_list(para['bounding_box']['vertices'])
                boxes.append(
                    Box(bb=corners,
                        kind='para',
                        text='',
                        score=para['confidence']))
                for word in para['words']:
                    text = ''
                    for symbol in word['symbols']:
                        text += symbol['text']
                    corners = corner_list(word['bounding_box']['vertices'])
                    if corners:
                        boxes.append(
                            Box(bb=corners,
                                kind='word',
                                text=text,
                                score=para['confidence']))
                    else:
                        # Something is wrong with the vertex list.
                        # Skip it and continue.
                        if __debug__: log(f'bad bb for {text}: {bb}')
        full_text = result['full_text_annotation']['text']
        return TRResult(path=path,
                        data=result,
                        boxes=boxes,
                        text=full_text,
                        error=None)