Exemplo n.º 1
0
def get_texts_batch(rects, img, Y):
    texts = []
    features = [
        types.Feature(type=enums.Feature.Type.TEXT_DETECTION),
    ]

    requests = []
    for i, rect in enumerate(rects):
        x, y, w, h = rect
        tag = img[int(y):int((y+h)), int(x):int((x+w))]
        cv2.imwrite('temp.png', tag)
        with open('temp.png', 'rb') as image_file:
            imageContext = types.ImageContext(language_hints=["en"])
            image = types.Image(content = image_file.read())
            request = types.AnnotateImageRequest(image=image, features=features, image_context=imageContext)
            requests.append(request)
        #print(labelSet[np.argmax(Y[i])])
        #texts.append(detect_text('temp.png').strip())

    client = vision.ImageAnnotatorClient()
    response = client.batch_annotate_images(requests)

    for response in response.responses:
        if len(response.full_text_annotation.text) > 0:
            texts.append(response.full_text_annotation.text.strip())
        else:
            texts.append("none")
        
    os.remove('temp.png')
    return texts
Exemplo n.º 2
0
def detect_crop_hints(path, frame_id, user_id):
    print "Detects crop hints in an image." + path
    client = vision.ImageAnnotatorClient()

    # [START migration_crop_hints]
    with io.open(path, 'rb') as image_file:
        content = image_file.read()
    image = types.Image(content=content)

    # worth looking at this bullshit constant right here
    crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
    image_context = types.ImageContext(crop_hints_params=crop_hints_params)

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

    cropHintsList = []
    count = 0
    for n, hint in enumerate(hints):
        print('\nCrop Hint: {}'.format(n))
        crop_to_hint(path, hint.bounding_poly.vertices, count)
        count = count + 1
        vertices = ([
            '({},{})'.format(vertex.x, vertex.y)
            for vertex in hint.bounding_poly.vertices
        ])
        print('bounds: {}'.format(','.join(vertices)))
        vertexes = []
        for vertex in hint.bounding_poly.vertices:
            vertexes.append({"x": vertex.x, "y": vertex.y})
        cropHintsList.append(vertexes)

    cropHintsJSON = {"crophints": cropHintsList}
    spitJSONAPIResulttoMDB(cropHintsJSON, "crophints_ocr_google", frame_id,
                           user_id)
def cropImage(imagepath):

    client = vision.ImageAnnotatorClient()
    hints = None
    try:
        with io.open(imagepath, 'rb') as image_file:
            content = image_file.read()

        image = types.Image(content=content)

        image_context = types.ImageContext()

        response = client.label_detection(image=image)

        labels = response.label_annotations
        hints = None
        for label in labels:
            if label.description == "Pizza":
                response2 = client.crop_hints(image=image,
                                              image_context=image_context)
                hints = response2.crop_hints_annotation.crop_hints

    except:
        print("Error opening file: " + str(imagepath))
        return None

    if (hints is not None):
        print(hints)
        verticies = hints[0].bounding_poly.vertices
    else:
        verticies = None
        os.remove(imagepath)

    return verticies
Exemplo n.º 4
0
def info(request):
    os.environ[
        'GOOGLE_APPLICATION_CREDENTIALS'] = 'kal-kal-1532846232334-738abd5f7e5a.json'
    image_context = types.ImageContext(language_hints=["iw"])
    client = vision.ImageAnnotatorClient()

    myfile = request.FILES["pic"]
    print("myfile name data>>>>>>>>>>>>>>>>>>>>>>>>>", myfile)
    fs = FileSystemStorage()
    filename = fs.save(myfile.name, myfile)
    file_name = fs.url(filename)
    #image_file= r'C:\Users\mjha3\Desktop\Django_project\VisionAPI_Test\PDFtoTEXT\pdftotext\pdftotext\media\2020_07_30 00_23 Office Lens (3)3.jpg'
    print("Image file location>>>>>>>", image_file)

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

    image = types.Image(content=content)

    response = client.document_text_detection(image=image,
                                              image_context=image_context)
    document = response.full_text_annotation

    bounds = []
    doctext = []

    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            block_words = []
            for paragraph in block.paragraphs:
                block_words.extend(paragraph.words)
                #print(u'Paragraph Confidence: {}\n'.format(paragraph.confidence))

                block_text = ''
                block_symbols = []
                for word in block_words:
                    block_symbols.extend(word.symbols)
                    word_text = ''
                    for symbol in word.symbols:
                        word_text = word_text + symbol.text
                        #print(u'\tSymbol text: {} (confidence: {})'.format(symbol.text, symbol.confidence))

                    #print(u'Word text: {} (confidence: {})\n'.format(word_text, word.confidence))

                    block_text += ' ' + word_text

                #print(u'Block Content: {}\n'.format(block_text))
                #print(u'Block Confidence:\n {}\n'.format(block.confidence))
                bounds.append(block.bounding_box)
                doctext.append(block_text + "\n")
                print("data value of doctext >>>>>>>>>>>>>>>..",
                      doctext.append(block_text + "\n"))

    #return (bounds,doctext)
    return render(request, 'results.html', {
        "labels": doctext,
        'image': image_file
    })
Exemplo n.º 5
0
    async def transcribe(self, document):
        """Detects document features in images and returns extracted text
        Input:
        --------
        `document`: bytes - The file object to be sent to Google Vision API

        Output:
        --------
        `transcribed_text`: str - Transcribed text from Google Vision API

        """
        # read the file's content and cast into Image type
        # use async friendly await function to fetch read
        image = types.Image(content=document)
        # adding refined language specification to sort out Non-English
        # characters from transcription responses
        language = types.ImageContext(language_hints=["en-t-i0-handwrit"])
        # Connect to Google API client with the file that is built above
        response = self.client.document_text_detection(image=image,
                                                       image_context=language)
        # check if there are transcriptions from google
        if response.text_annotations:
            # store and process the response
            transcribed_text = response.text_annotations[0].description
            transcribed_text = transcribed_text.replace("\n", " ")
        else:
            # forward no text in image exception to caller
            raise NoTextFoundException("No Text Was Found In Image")

        flagged = False
        # List of confidence levels of each character
        symbol_confidences = []
        for page in response.full_text_annotation.pages:
            for block in page.blocks:
                for paragraph in block.paragraphs:
                    for word in paragraph.words:
                        # check moderation status of word in paragraph
                        if self.text_moderator.check_word(str(word).lower()):
                            flagged = True
                        for symbol in word.symbols:
                            symbol_confidences.append(symbol.confidence)
        # Calculate the overall confidence for the page
        page_confidence = sum(symbol_confidences) / len(symbol_confidences)

        # return flag: True under 85% confident, False 85% confident or over
        # return text transcription
        return (page_confidence < 0.85), flagged, transcribed_text
Exemplo n.º 6
0
def do_documnet_ocr(image_file, feature):

    image_context = types.ImageContext(language_hints=["dv"])
    client = vision.ImageAnnotatorClient()

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

    image = types.Image(content=content)

    response = client.document_text_detection(image=image,
                                              image_context=image_context)
    document = response.full_text_annotation

    bounds = []
    doctext = []

    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            block_words = []
            for paragraph in block.paragraphs:
                block_words.extend(paragraph.words)
                print(u'Paragraph Confidence: {}\n'.format(
                    paragraph.confidence))

            block_text = ''
            block_symbols = []
            for word in block_words:
                block_symbols.extend(word.symbols)
                word_text = ''
                for symbol in word.symbols:
                    word_text = word_text + symbol.text
                    print(u'\tSymbol text: {} (confidence: {})'.format(
                        symbol.text, symbol.confidence))

                print(u'Word text: {} (confidence: {})\n'.format(
                    word_text, word.confidence))

                block_text += ' ' + word_text

            print(u'Block Content: {}\n'.format(block_text))
            print(u'Block Confidence:\n {}\n'.format(block.confidence))
            bounds.append(block.bounding_box)
            doctext.append(block_text + "\n")

    return (bounds, doctext)
Exemplo n.º 7
0
def get_crops(image_data, aspect_ratio, n_results=50):
    client = vision.ImageAnnotatorClient()

    response = client.crop_hints(
        image=types.Image(content=image_data),
        max_results=n_results,
        image_context=types.ImageContext(
            crop_hints_params={"aspect_ratios": [aspect_ratio]}))

    result = []
    for crop_hint in response.crop_hints_annotation.crop_hints:
        entry = {}
        entry["score"] = crop_hint.confidence
        entry["crop"] = [(vertex.x, vertex.y)
                         for vertex in crop_hint.bounding_poly.vertices]
        result.append(entry)

    return result
Exemplo n.º 8
0
def get_crop_hint(path):
    """Detect crop hints on a single image and return the first result."""
    client = vision.ImageAnnotatorClient()

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

    image = types.Image(content=content)

    crop_hints_params = types.CropHintsParams()
    image_context = types.ImageContext(crop_hints_params=crop_hints_params)

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

    vertices = hints[0].bounding_poly.vertices

    return vertices
Exemplo n.º 9
0
def detect_crop_hints_uri(uri):
    """Detects crop hints in the file located in Google Cloud Storage."""
    client = vision.ImageAnnotatorClient()
    image = types.Image()
    image.source.image_uri = uri

    crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
    image_context = types.ImageContext(crop_hints_params=crop_hints_params)

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

    for n, hint in enumerate(hints):
        print('\nCrop Hint: {}'.format(n))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in hint.bounding_poly.vertices])

        print('bounds: {}'.format(','.join(vertices)))
Exemplo n.º 10
0
    def detect_crop_hints(latest_file, image_file):
        #Detect crop hints on a single image and return the first result.
        client = vision.ImageAnnotatorClient()

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

        image = types.Image(content=content)

        crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
        image_context = types.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.º 11
0
def get_crop_hints(path, ratios):
    """Detect crop hints on a single image and return the first result."""
    client = vision.ImageAnnotatorClient()

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

    image = types.Image(content=content)

    crop_hints_params = types.CropHintsParams(aspect_ratios=ratios)
    image_context = types.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 = (a.bounding_poly.vertices for a in hints)

    return vertices
Exemplo n.º 12
0
def get_ocr(temp_image_file, temp_source):

    client = vision.ImageAnnotatorClient()
    with io.open(temp_image_file, 'rb') as image_file:
        content = image_file.read()
    image = types.Image(content=content)
    image_context = types.ImageContext(language_hints=[temp_source])
    response = client.text_detection(image=image, image_context=image_context)
    texts = response.text_annotations
    source_text = []
    for text in texts:
        text.description = text.description.strip()
        if text.description in source_text:
            pass
        else:
            source_text.append(text.description)
    source_text = ' '.join(source_text)

    return source_text
    def detect_crop_hints_uri(self, uri):
        """
    Description: 
       Detects crop hints in an image.
    Args: 
       uri: url to an image on the Internet
    Returns:
       crop hints json object
    """

        image = types.Image()
        image.source.image_uri = uri

        crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
        image_context = types.ImageContext(crop_hints_params=crop_hints_params)

        response = self.client.crop_hints(image=image,
                                          image_context=image_context)

        return response
Exemplo n.º 14
0
    def get_crop_hint(self, path):
        """Detect crop hints on a single image and return the first result."""
        client = vision.ImageAnnotatorClient()

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

        image = types.Image(content=content)

        crop_hints_params = types.CropHintsParams(aspect_ratios=[0.3])
        image_context = types.ImageContext(crop_hints_params=crop_hints_params)

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

        try:
            vertices = hints[0].bounding_poly.vertices
        except:
            raise AssertionError("the image doesn't exist")
        return vertices
Exemplo n.º 15
0
def get_crop_hint(image, aspect_ratios=''):
    """Detect crop hints on a single image and return the first result."""

    try:
        aspect_ratios = [float(x) for x in aspect_ratios.split(',')]
        crop_hints_params = types.CropHintsParams(aspect_ratios=aspect_ratios)
        image_context = types.ImageContext(crop_hints_params=crop_hints_params)

        response = client.crop_hints(image=image_properties.my_image(image),
                                     image_context=image_context)
    except ValueError:
        response = client.crop_hints(image=image_properties.my_image(image), )

    hints = response.crop_hints_annotation.crop_hints

    # Get bounds for the first crop hint using an aspect ratio of 1.77.
    return {
        str(ratio): hint.bounding_poly.vertices
        for ratio, hint in zip(aspect_ratios, hints)
    }
    def detect_crop_hints(self, path):
        """
    Description: 
       Detects crop hints in an image.
    Args: 
       path: path to a local image
    Returns:
       crop hints json object
    """

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

        crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
        image_context = types.ImageContext(crop_hints_params=crop_hints_params)

        response = self.client.crop_hints(image=image,
                                          image_context=image_context)

        return response
Exemplo n.º 17
0
def detect_text(path, lang):
    """Detects text in the file."""
    client = vision.ImageAnnotatorClient()

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

    image = vision.types.Image(content=content)
    image_context = types.ImageContext(language_hints=[lang])

    response = client.text_detection(image=image, image_context=image_context)

    texts = response.text_annotations

    output = []

    for text in texts:
        if len(text.description.split(" ")) > 1:
            output.append(text.description)

    return output
Exemplo n.º 18
0
def _detect_cropping(client, image):
    """
    Get cropping hints for 1:1 ratio from vision api.
    :param client:
    :param image:
    :return:
    """
    crop_hints_params = types.CropHintsParams(aspect_ratios=[1.0])
    image_context = types.ImageContext(crop_hints_params=crop_hints_params)

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

    vertices = []
    for v in hints[0].bounding_poly.vertices:
        vertices.append({
            "x": v.x,
            "y": v.y,
        })

    return vertices
Exemplo n.º 19
0
def detect_crop_hints(path):
    """Detects crop hints in an image."""
    client = vision.ImageAnnotatorClient()

    # [START migration_crop_hints]
    with io.open(path, 'rb') as image_file:
        content = image_file.read()
    image = types.Image(content=content)

    crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
    image_context = types.ImageContext(crop_hints_params=crop_hints_params)

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

    for n, hint in enumerate(hints):
        print('\nCrop Hint: {}'.format(n))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in hint.bounding_poly.vertices])

        print('bounds: {}'.format(','.join(vertices)))
def detect_document_text(image_file, textfile, jsonfile):
    """Returns document text given an image."""
    client = vision.ImageAnnotatorClient()

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

    image = types.Image(content=content)
    image_context = types.ImageContext(language_hints=['en'])

    response = client.document_text_detection(image=image,
                                              timeout=300,
                                              image_context=image_context)
    document = response.full_text_annotation

    if textfile is not 0:
        with io.open(textfile, 'wb') as f:
            f.write(document.text.encode('utf-8'))

    if jsonfile is not 0:
        with io.open(jsonfile, 'wb') as f:
            f.write(str(document))

    return document
Exemplo n.º 21
0
def annotate(basename):
    # The name of the image file to annotate
    file_name = os.path.join(
        os.path.dirname(__file__),
        'resources/{}'.format(basename))

    # Loads the image into memory
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    # Specify language hints
    image_context = types.ImageContext(language_hints=['ja'])

    response = client.text_detection(image=image,image_context=image_context)

    # Write JSON File
    serialized = MessageToJson(response)
    data = json.loads(serialized)

    f = open('results/{}.json'.format(os.path.splitext(basename)[0]), 'w')
    f.write(json.dumps(data,ensure_ascii=False))
    f.close()
def async_detect_document_text(bucket_name, image_file, textfile, jsonfile):
    # Supported mime_types are: 'application/pdf' and 'image/tiff'
    mime_type = 'image/tiff'

    tmp_dir = tempfile._get_default_tempdir()

    png_fn = os.path.basename(image_file)
    fn = os.path.splitext(png_fn)[0]
    tif_fn = fn + '.tif'
    prefix_fn = fn + '-'
    tif_path = os.path.join(tmp_dir, tif_fn)

    gcs_src_uri = 'gs://{}/{}'.format(bucket_name, tif_fn)
    gcs_dst_uri = 'gs://{}/{}'.format(bucket_name, prefix_fn)

    logging.info('Converting... {!s}'.format(png_fn))
    with Image.open(image_file) as im:
        im.save(tif_path, compression='tiff_lzw', tiffinfo={317: 2, 278: 1})
    logging.info('Uploading... {!s}'.format(tif_fn))
    upload_blob(bucket_name, tif_path, tif_fn)

    os.unlink(tif_path)

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

    client = vision.ImageAnnotatorClient()

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

    gcs_src = types.GcsSource(uri=gcs_src_uri)
    input_config = types.InputConfig(gcs_source=gcs_src, mime_type=mime_type)

    gcs_dst = types.GcsDestination(uri=gcs_dst_uri)
    output_config = types.OutputConfig(gcs_destination=gcs_dst,
                                       batch_size=batch_size)

    image_context = types.ImageContext(language_hints=['en'])

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

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

    logging.info('Waiting... {!s}'.format(gcs_src_uri))
    result = operation.result(timeout=GOOGLE_OPERATION_TIMEOUT)
    logging.debug('{!s}'.format(result))

    delete_blob(bucket_name, tif_fn)

    # 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_dst_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))

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

    logging.info('Downloading... {!s}'.format(output.name))

    json_string = output.download_as_string()

    logging.debug('JSON len={:d}'.format(len(json_string)))

    response = json_format.Parse(json_string, types.AnnotateFileResponse())

    error = response.responses[0].error
    if error.code != 0:
        logging.error("Vision API code: {!s}, msg: {!s}".format(
            error.code, error.message))
        return None

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

    if textfile is not 0:
        logging.info('Saving... {!s}'.format(textfile))
        with io.open(textfile, 'wb') as f:
            f.write(document.text.encode('utf-8'))

    if jsonfile is not 0:
        logging.info('Saving... {!s}'.format(jsonfile))
        with io.open(jsonfile, 'wb') as f:
            f.write(json_format.MessageToJson(document))

    output.delete()

    return document