Beispiel #1
0
def ocr():

    client = vision.ImageAnnotatorClient()
    path = './naver_login.png'
    # path = './6-1 로그인.png'
    # path = './9-1 상품연결.png'
    # path = './naver.png'
    # path = './google_lee.png'

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

    image = vision.Image(content=content)

    # price_candidate = []
    # card_number_candidate = []
    # date_candidate = []

    response = client.text_detection(image=image)
    # serialize / deserialize proto (binary)
    serialized_proto_plus = AnnotateImageResponse.serialize(response)
    response = AnnotateImageResponse.deserialize(serialized_proto_plus)
    print(response.full_text_annotation.text)

    # serialize / deserialize json
    response_json = AnnotateImageResponse.to_json(response)
    response = json.loads(response_json)
    print(response['fullTextAnnotation']['text'])

    with open("textInImage.json", "w") as json_file:
        json.dump(response, json_file)
Beispiel #2
0
def ocr(trigger):
    image_path = 'https://firebasestorage.googleapis.com/v0/b/img2code-326013.appspot.com/o/Web_images%2F' + trigger + '.png?alt=media'
    client = vision.ImageAnnotatorClient.from_service_account_json(
        "./img2code-326013-000adcb01d14.json")

    os.makedirs(os.path.dirname('/tmp/temp.png'), exist_ok=True)
    with urllib.request.urlopen(image_path) as url:
        with open('/tmp/temp.png', 'wb') as f:
            f.write(url.read())
    if os.path.exists('/tmp/temp.png'):
        with open('/tmp/temp.png', 'rb') as f:
            content = f.read()
    image = vision.Image(content=content)

    response = client.text_detection(image=image)
    # serialize / deserialize proto (binary)
    serialized_proto_plus = AnnotateImageResponse.serialize(response)
    response = AnnotateImageResponse.deserialize(serialized_proto_plus)
    #print(response.full_text_annotation.text)

    # serialize / deserialize json
    response_json = AnnotateImageResponse.to_json(response)
    response = json.loads(response_json)
    #print(response['fullTextAnnotation']['text'])

    os.makedirs(os.path.dirname('/tmp/textInImage.json'), exist_ok=True)
    with open("/tmp/textInImage.json", "w") as json_file:
        json.dump(response, json_file)
Beispiel #3
0
def ocr():

    client = vision.ImageAnnotatorClient()
    # path = './6-1 로그인.png'
    path = './naver_login.png'

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

    image = vision.Image(content=content)

    price_candidate = []
    card_number_candidate = []
    date_candidate = []

    response = client.text_detection(image=image)
    # serialize / deserialize proto (binary)
    serialized_proto_plus = AnnotateImageResponse.serialize(response)
    response = AnnotateImageResponse.deserialize(serialized_proto_plus)
    print(response.full_text_annotation.text)
    # print(response.full_text_annotation.text)

    # serialize / deserialize json
    response_json = AnnotateImageResponse.to_json(response)
    response = json.loads(response_json)
    print(response['fullTextAnnotation']['text'])

    with open("student_file.json", "w") as json_file:
        json.dump(response, json_file)

    # print(type(response))

    f = open("new.txt", 'w')
    f2 = open("xy.txt", 'w')
    # for i in range(0,count):
    # f2.write(string)
    # f2.write(texts[i])
    # print(i)
    # print(texts[i])
    f2.write(response)
    f2.close()
    # print('Texts:')

    for text in texts:
        content = text.description
        content = content.replace(',', '')
        f.write('"{}"'.format(content))

    f.close()
    if response.error.message:
        raise Exception('{}\nFor more info on error messages, check: '
                        'https://cloud.google.com/apis/design/errors'.format(
                            response.error.message))
Beispiel #4
0
def ocr():

    client = vision.ImageAnnotatorClient.from_service_account_json(
        "./img2code-326013-000adcb01d14.json")
    print(1)
    # path = './naver_login.png'

    # path = './9-1 상품연결.png'
    # path = './naver.png'
    # path = './google_lee.png'

    # with io.open(image_path, 'rb') as image_file:
    #     content = image_file.read()
    # content = Image.open(io.BytesIO(image_path))
    os.makedirs(os.path.dirname('/tmp/temp.png'), exist_ok=True)
    with urllib.request.urlopen(image_path) as url:
        with open('/tmp/temp.png', 'wb') as f:
            f.write(url.read())
    if os.path.exists('/tmp/temp.png'):
        with open('/tmp/temp.png', 'rb') as f:
            content = f.read()

    print(2)
    image = vision.Image(content=content)
    print(3)
    # price_candidate = []
    # card_number_candidate = []
    # date_candidate = []

    response = client.text_detection(image=image)
    print(4)
    # serialize / deserialize proto (binary)
    serialized_proto_plus = AnnotateImageResponse.serialize(response)
    print(5)
    response = AnnotateImageResponse.deserialize(serialized_proto_plus)
    print(response.full_text_annotation.text)

    # serialize / deserialize json
    response_json = AnnotateImageResponse.to_json(response)
    response = json.loads(response_json)
    print(response['fullTextAnnotation']['text'])

    os.makedirs(os.path.dirname('/tmp/textInImage.json'), exist_ok=True)
    with open("/tmp/textInImage.json", "w") as json_file:
        json.dump(response, json_file)
Beispiel #5
0
def detect_text_uri(uri):
    """Detects text in the file located in Google Cloud Storage or on the Web.
    """
    image = vision.Image()
    image.source.image_uri = uri

    response = client.text_detection(
        image=image,
        image_context={"language_hints": ["bn"]},
    )

    serialized_proto_plus = AnnotateImageResponse.serialize(response)
    response = AnnotateImageResponse.deserialize(serialized_proto_plus)
    # print(response.full_text_annotation.text)

    # serialize / deserialize json
    response_json = AnnotateImageResponse.to_json(response)
    response = json.loads(response_json)
    l_response = response['fullTextAnnotation']['text']
    l_response = l_response.replace('\n', ' ')
    return l_response