Ejemplo n.º 1
0
def run_quickstart(file_name):
    import io
    import os

    # 구글 라이브러리 import
    from google.cloud import vision
    from google.cloud.vision_v1 import types

    # 사용할 클라이언트 설정
    client = vision.ImageAnnotatorClient()

    # 이미지 읽기
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    # label 뽑아냄.
    response = client.label_detection(image=image)
    labels = response.label_annotations

    result = []

    print('Labels:')
    for label in labels:
        print(label.description + " = " + str(int(label.score*100)) + "%")
        result.append(label.description + " = " +
                      str(int(label.score*100)) + "%\n")

    result = '\n'.join(result)
    return result
def get_document_bounds(image_file, feature):

    client = vision_v1.ImageAnnotatorClient()

    bounds = []

    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)
    document = response.full_text_annotation

    for page in document.pages:
        for block in page.blocks:
            for paragraph in block.paragraphs:
                for word in paragraph.words:
                    for symbol in word.symbols:
                        if (feature == FeatureType.SYMBOL):
                            bounds.append(symbol.bounding_box)

                    if (feature == FeatureType.WORD):
                        bounds.append(word.bounding_box)

                if (feature == FeatureType.PARA):
                    bounds.append(paragraph.bounding_box)

            if (feature == FeatureType.BLOCK):
                bounds.append(block.bounding_box)

        if (feature == FeatureType.PAGE):
            bounds.append(block.bounding_box)

    return bounds
Ejemplo n.º 3
0
def faceCrop(tempDir, classDir):
    # print("tempDir :"+tempDir)
    # print("classDir :"+classDir)

    tempImg = cv2.imread(tempDir)
    height, width, channel = tempImg.shape

    with io.open(tempDir, 'rb') as image_file:
        content = image_file.read()
    image = types.Image(content=content)
    client = vision.ImageAnnotatorClient()
    response = client.text_detection(image=image)
    texts = response.text_annotations

    print(len(texts))
    if len(texts) > 2 and len(texts) < 50:
        firstVertices = texts[1].bounding_poly.vertices
        f_cornerX = firstVertices[3].x
        secondVertices = texts[2].bounding_poly.vertices
        s_cornerX = secondVertices[3].x

        dx = s_cornerX - (f_cornerX + firstVertices[2].x)
        dy = int(dx * 0.6)

        if not os.path.isdir(classDir + "/students"):
            os.mkdir(classDir + "/students")
            print(classDir + "/students" + "폴더 만들어짐")
        print("if문 안걸림")
        # print(texts[1:])
        # COUNT = 0
        for text in texts[1:]:
            print("facecrop for문 안으로 들어옴")
            # cv2.rectangle(img=tempImg, pt1=(text.bounding_poly.vertices[0].x,text.bounding_poly.vertices[0].y), pt2=(text.bounding_poly.vertices[2].x,text.bounding_poly.vertices[2].y),color=(255,0,0))
            print(text.description)
            print("In")
            path = classDir + "/students/"
            vertex = text.bounding_poly.vertices[3]
            leftTopY = vertex.y - dy - 5
            rightBotX = vertex.x + dx + 5
            if leftTopY < 0:
                leftTopY = 0
            if rightBotX > width:
                rightBotX = width
            face = tempImg[leftTopY:vertex.y, vertex.x:rightBotX]
            if rightBotX - vertex.x > width / 7 and vertex.y - leftTopY > height / 7:
                print(text.description)
                # print(leftTopY, vertex.y, vertex.x, rightBotX)
                cv2.imwrite(path + text.description + '.png', face)
                print(text.description + "작성 중..")
                # cv2.putText(tempImg, str(COUNT), (leftTopY, vertex.x), cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 0, 0))
                # cv2.rectangle(img=tempImg, pt1=(vertex.x, (leftTopY)), pt2=((rightBotX), vertex.y), color=(0, 255, 0))
                # COUNT += 1
            else:
                print("HI!")

        # cv2.imshow("img", tempImg)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
    else:
        print("Error: too many")
Ejemplo n.º 4
0
    def test_explicit_features(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = types.Image(source={
            'image_uri': 'http://foo.com/img.jpg',
        })
        request = types.AnnotateImageRequest(
            image=image,
            features=[
                types.Feature(type=1),
                types.Feature(type=2),
                types.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(args[0]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = args[0][0]
        assert request_sent.image is 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
Ejemplo n.º 5
0
def upload():
    file = request.files['image']
    f = os.path.join(app.config['UPLOAD_LOCATION'], file.filename)
    file.save(f)
    import io

    from google.cloud import vision
    from google.cloud.vision_v1 import types

    client = vision.ImageAnnotatorClient()

    file_name = os.path.join(app.config['UPLOAD_LOCATION'], file.filename)

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

    image = types.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations
    x = print('Texts:')

    for text in texts:
        result = print('\n"{}"'.format(text.description))

    return render_template('success.html', result=result)
Ejemplo n.º 6
0
 def load_file(self, in_file_name):
     self.client = vision.ImageAnnotatorClient()
     with io.open(in_file_name, 'rb') as image_file:
         content = image_file.read()
     self.image = types.Image(content=content)
     self.im = cv2.imread(in_file_name)
     self.response = self.client.logo_detection(image=self.image)
     self.labels = self.response.logo_annotations
Ejemplo n.º 7
0
def init():
    ## TODO: get a key unique to project (not using personal email)
    os.environ[
        'GOOGLE_APPLICATION_CREDENTIALS'] = './fwd-capstone-61889238a64c.json'

    client = vision.ImageAnnotatorClient()

    image = types.Image()
    return client, image
Ejemplo n.º 8
0
def load_image():

    # Using a URL
    #image.source.image_uri = 'https://cdn-prod.medicalnewstoday.com/content/images/articles/270/270609/spinach.jpg'

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

    processed_image = types.Image(content=content)
    return processed_image
def handwriting_solve():

    with io.open('../python/test.png', 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=imageData.read())
    client = vision_v1.ImageAnnotatorClient()
    response = client.text_detection(image=image)

    raw_data = response.text_annotations[0].description
    return raw_data
Ejemplo n.º 10
0
def detect_text(path):
    """Detects text in the file."""
    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)
    response = client.text_detection(image=image)
    texts = response.text_annotations
    string = texts[0].description

    return string
Ejemplo n.º 11
0
def save_frame_camera_cycle(device_num,
                            dir_path,
                            basename,
                            cycle,
                            ext='jpg',
                            delay=1,
                            window_name='frame'):
    cap = cv2.VideoCapture(device_num)

    if not cap.isOpened():
        return

    os.makedirs(dir_path, exist_ok=True)
    base_path = os.path.join(dir_path, basename)

    client = vision.ImageAnnotatorClient()

    likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
                       'LIKELY', 'VERY_LIKELY')

    n = 0
    while True:
        ret, frame = cap.read()
        cv2.imshow(window_name, frame)
        if cv2.waitKey(delay) & 0xFF == ord('q'):
            break
        if n == cycle:
            n = 0
            file_name = '{}_{}.{}'.format(
                base_path,
                datetime.datetime.now().strftime('%Y%m%d%H%M%S%f'), ext)
            cv2.imwrite(file_name, frame)

            with io.open(file_name, 'rb') as image_file:
                content = image_file.read()
            image = types.Image(content=content)
            response = client.face_detection(image=image)

            faces = response.face_annotations

            print('\nFaces:')

            for face in faces:
                print('anger: {}'.format(
                    likelihood_name[face.anger_likelihood]))
                print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
                print('surprise: {}'.format(
                    likelihood_name[face.surprise_likelihood]))
        n += 1

    cv2.destroyWindow(window_name)
Ejemplo n.º 12
0
def faceCrop(tempDir, classDir):

    tempImg = cv2.imread(tempDir)
    height, width, channel = tempImg.shape

    with io.open(tempDir, 'rb') as image_file:
        content = image_file.read()
    image = types.Image(content=content)
    client = vision.ImageAnnotatorClient()
    response = client.text_detection(image=image)
    texts = response.text_annotations

    if len(texts) > 2 and len(texts) < 50:
        firstVertices = texts[1].bounding_poly.vertices
        f_cornerX = firstVertices[3].x
        secondVertices = texts[2].bounding_poly.vertices
        s_cornerX = secondVertices[3].x

        dx = s_cornerX - (f_cornerX + firstVertices[2].x)
        dy = int(dx * 0.6)

        if not os.path.isdir(classDir + "/students"):
            os.mkdir(classDir + "/students")

        for text in texts[1:]:
            path = classDir + "/students/"
            vertex = text.bounding_poly.vertices[3]
            leftTopY = vertex.y - dy - 5
            rightBotX = vertex.x + dx + 5
            if leftTopY < 0:
                leftTopY = 0
            if rightBotX > width:
                rightBotX = width
            face = tempImg[leftTopY:vertex.y, vertex.x:rightBotX]
            if rightBotX - vertex.x > width / 7 and vertex.y - leftTopY > height / 7:
                print(leftTopY, vertex.y, vertex.x, rightBotX)
                cv2.imwrite(path + text.description + '.png', face)
                # cv2.rectangle(img=tempImg, pt1=(vertex.x, (leftTopY)),pt2=((rightBotX), vertex.y),color=(0, 255, 0))

        # cv2.imshow("img",tempImg)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
    else:
        print("Error: too many")
Ejemplo n.º 13
0
def run_quickstart(file_name):


    # 사용할 클라이언트 설정
    client = vision.ImageAnnotatorClient()

    # 이미지 읽기
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    # label 뽑아냄.
    response = client.label_detection(image=image)
    labels = response.label_annotations

    print('Labels:')
    for label in labels:
        print(label.description + " = " + str(int(label.score * 100)) + "%")
Ejemplo n.º 14
0
def findtext(img_dir):
    client = vision.ImageAnnotatorClient()

    file_name = os.path.join(os.path.abspath("__file__"), img_dir)

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

    image = types.Image(content=content)

    respose = client.text_detection(image=image)
    labels = respose.text_annotations

    for label in labels:
        # print(label.description)
        textlist.append(label.description)

    return textlist
    def getTextGoogleVisionOcr(self, img):
        tmp_file = "tmp.png"
        cv2.imwrite(tmp_file, img)
        with io.open(tmp_file, 'rb') as image_file:
            content = image_file.read()

        image = types.Image(content=content)
        response = ocr_client.text_detection(image=image)
        texts = response.text_annotations
        #cv2_imshow(img)
        string = ''
        for idx, text in enumerate(texts):
            string += ' ' + text.description
            break

        string = string.replace('\ufeff', '')
        string = self.filterText(string)
        os.remove(tmp_file)
        #print(string)
        return string
Ejemplo n.º 16
0
def detectText(img):
    with open(img, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations

    df = pd.DataFrame(columns=['locale','description'])

    for text in texts:
        df = df.append(
            dict(
                locale=text.locale,
                description=text.description
            ),
            ignore_index=True
        )
    return df
Ejemplo n.º 17
0
def detect_labels_uri(uri):
    credentials_data = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
    service_account_info = json.loads(credentials_data)
    credentials = service_account.Credentials.from_service_account_info(
        service_account_info)

    client = ImageAnnotatorClient(credentials=credentials)
    image = types.Image()
    image.source.image_uri = uri

    response = client.label_detection(image=image)
    labels = response.label_annotations
    if response.error.message:
        raise Exception(
            f'{response.error.message}For more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors')

    return ({
        'score': label.score,
        'description': label.description,
    } for label in labels)
Ejemplo n.º 18
0
    def test_all_features_default(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = types.Image(source={"image_uri": "http://foo.com/img.jpg"})
        request = types.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(args[0]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = args[0][0]
        all_features = self.client._get_all_features()
        assert request_sent.image is request.image
        assert len(request_sent.features) == len(all_features)
Ejemplo n.º 19
0
def computer_vision(FILE_NAME):

    client = vision.ImageAnnotatorClient()
    with io.open((FILE_NAME), 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)
    count = 0
    count2 = 0
    total_confidence = 0
    response = client.document_text_detection(image=image)
    entire_text = ""

    try:
        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:
                    para_text = ""

                    for word in paragraph.words:
                        word_text = ''.join(
                            [symbol.text for symbol in word.symbols])
                        para_text = para_text + " " + word_text
                    total_confidence = total_confidence + paragraph.confidence

                    count2 += 1
                    entire_text = entire_text + para_text

                    #print('Paragraph text: {}\n'.format(para_text))

        return entire_text
    except (UnicodeEncodeError):

        return entire_text

    flask.run()
Ejemplo n.º 20
0
def detectHandWriting(img):
    with open(img, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    response = client.document_text_detection(image=image)
    docText = response.full_text_annotation.text
    print(docText)
    words_confidences = []
    
    pages = response.full_text_annotation.pages
    for page in pages:
        for block in page.blocks:
            print('block confidence:', block.confidence)

            for paragraph in block.paragraphs:
                print('paragraph confidence:', paragraph.confidence)

                for word in paragraph.words:
                    word_text = ''.join([symbol.text for symbol in word.symbols])
                    words_confidences.append([word_text, word.confidence])
                    print('Word text: {0} (confidence: {1}'.format(word_text, word.confidence))
    return words_confidences
Ejemplo n.º 21
0
def core_License_processor(img1, img2):

    # Path to the two License
    PCO_License = img1
    Driver_License = img2

    # client
    client = vision.ImageAnnotatorClient()
    crop_hints_params = vision.CropHintsParams(aspect_ratios=[1.77])

    # AWS Client
    with open('configfile.json') as f:
        data = json.load(f)

    ACCESS_KEY_ID = data['demz-license-user-access-key']
    ACCESS_SECRET_KEY = data['demz-license-user-secret-access-key']

    pco_BUCKET_NAME = 'pco-license'
    driver_BUCKET_NAME = 'drivers-license'
    user_BUCKET_NAME = 'license-user'

    # Drivers License response
    with io.open(img2, 'rb') as image_file1:
        content = image_file1.read()
    # content = img2
    driver_license_original_image = content
    content_image = types.Image(content=content)
    Drivers_response = client.text_detection(image=content_image)
    Drivers_response_face = client.face_detection(image=content_image)
    Drivers_texts = Drivers_response.text_annotations
    Drivers_faceAnnotations = Drivers_response_face.face_annotations

    # PCO License response
    with io.open(img1, 'rb') as image_file1:
        content = image_file1.read()
    # content = img1
    pco_license_original_image = content
    content_image = types.Image(content=content)
    PCO_response = client.text_detection(image=content_image)
    PCO_response_face = client.face_detection(image=content_image)
    PCO_texts = PCO_response.text_annotations
    PCO_faceAnnotations = PCO_response_face.face_annotations

    # Get crop hint
    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

    def crop_Driver_hint(image_file):
        """Crop the image using the hints in the vector list."""
        vects = get_crop_hint(image_file)
        #     response_face = Drivers_response_face
        faceAnnotations = Drivers_faceAnnotations
        try:
            face_bounds = []
            for face in faceAnnotations:
                face_vertices = ([
                    '({0},{1})'.format(vertex.x, vertex.y)
                    for vertex in face.bounding_poly.vertices
                ])
                face_bounds.append(face_vertices)
            bound_1 = face_bounds[0][0]
            bound_2 = face_bounds[0][2]
            bound_1 = eval(bound_1)
            bound_2 = eval(bound_2)

            im = Image.open(image_file)

            im2 = im.crop(
                [bound_1[0], bound_1[1], bound_2[0] - 1, bound_2[1] - 1])
            #     plt.imshow(im2)
            #     im2.save('output-crop.jpg', 'JPEG')
            print('Saved new image to output-crop.jpg')
            return im2
        except:
            with io.open('media_root/no_face.png', 'rb') as image_file1:
                im2 = image_file1.read()
            print("No Image Detected")
            return im2
            # return "No Image Detected"

    def crop_PCO_hint(image_file):
        """Crop the image using the hints in the vector list."""
        vects = get_crop_hint(image_file)
        #     response_face = PCO_response_face
        faceAnnotations = PCO_faceAnnotations
        try:
            face_bounds = []
            for face in faceAnnotations:
                face_vertices = ([
                    '({0},{1})'.format(vertex.x, vertex.y)
                    for vertex in face.bounding_poly.vertices
                ])
                face_bounds.append(face_vertices)
            bound_1 = face_bounds[0][0]
            bound_2 = face_bounds[0][2]
            bound_1 = eval(bound_1)
            bound_2 = eval(bound_2)

            im = Image.open(image_file)

            im2 = im.crop(
                [bound_1[0], bound_1[1], bound_2[0] - 1, bound_2[1] - 1])
            #     plt.imshow(im2)
            #     im2.save('output-crop.jpg', 'JPEG')
            print('Saved new image to output-crop.jpg')
            return im2
        except:
            with io.open('media_root/no_face.png', 'rb') as image_file1:
                im2 = image_file1.read()
            print("No Image Detected")
            return im2
            # return "No Image Detected"

    Driver_Image = crop_Driver_hint(img2)
    PCO_IMage = crop_PCO_hint(img1)

    def Processor(Drivers_texts, PCO_texts):
        Drivers_contents = []
        PCO_contents = []

        # Extract Driver License Contents
        for text in Drivers_texts:
            Drivers_contents.append(text.description)

        # Extract PCO License Contents
        for text in PCO_texts:
            PCO_contents.append(text.description)

        Driver_String_Content = Drivers_contents[0]
        PCO_String_Content = PCO_contents[0]

        #     print(PCO_contents)

        # Process the PCO License
        def Process_PCO_License(PCO_String_Content):
            try:
                if 'Expire Date' in PCO_String_Content:
                    new_pco_list = PCO_String_Content.split('Expire Date:')
                elif 'Expiry Date:' in PCO_String_Content:
                    new_pco_list = PCO_String_Content.split('Expiry Date:')
                else:
                    new_pco_list = PCO_String_Content.split('Expiry Date')
                new_pco = new_pco_list[-1]
                Expiry_date = new_pco_list[-1].split('\n')
                Expiry_date = [date for date in Expiry_date if date != ''][0]
                new_pco_content = new_pco_list[0]

                def get_pco_license_NO(new_pco_content):
                    license_NO = re.findall(r'\d', new_pco_content)
                    license_NO = ''.join(license_NO)
                    return license_NO

                PCO_license_NO = get_pco_license_NO(new_pco_content)
                new_pco_content = new_pco_content.split(PCO_license_NO)

                new_pco_content = new_pco_content[-1]
                new_pco_content = new_pco_content.split('\n')

                new_pco_content = [
                    name for name in new_pco_content if name != ''
                ]
                First_Name = new_pco_content[0]
                Other_Name = new_pco_content[-1]
                Full_Name = First_Name + ' ' + Other_Name
                Full_Name = Full_Name.split(' ')
                Full_Name = sorted(Full_Name)
                Full_Name = ' '.join(Full_Name)
                print(Full_Name)

                PCO_CONTENT = {}
                PCO_CONTENT['Full_Name'] = Full_Name
                #         PCO_CONTENT['Other Name'] = new_pco_content[-1]
                PCO_CONTENT['License Number'] = PCO_license_NO
                PCO_CONTENT['Expiry Date'] = Expiry_date
                return PCO_CONTENT
            except:
                PCO_CONTENT = {
                    'Contents':
                    "The Uploaded Document does not seem like a PCO License"
                }

    #     print(Process_PCO_License(PCO_String_Content))

        def Process_Driver_License(Driver_String_Content):
            #         print(Driver_String_Content)

            if 'DRIVING LICENCE' in Driver_String_Content:
                if 'DRIVING LICENCE' in Driver_String_Content:
                    Driver_contents = Driver_String_Content.split(
                        'DRIVING LICENCE')[-1]
                elif '1.' in Driver_String_Content:
                    Driver_contents = re.split('1.', Driver_String_Content,
                                               1)[-1]
                else:
                    Driver_contents = Driver_String_Content
                    print('No Split Found')
        #       Dates
                dates = re.findall(
                    r"([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])(\.|-|/)([1-9]|0[1-9]|1[0-2])(\.|-|/)(19[0-9][0-9]|20[0-9][0-9]|[0-9][0-9])",
                    Driver_contents)
                new_dates = []
                for date in dates:
                    dat = ''.join(date)
                    new_dates.append(dat)
                if len(new_dates) >= 3:
                    date_and_POB = re.search(
                        f'{new_dates[0]}.*\n',
                        Driver_contents).group(0).split('\n')[0]
                    DOB = new_dates[0]
                    POB = date_and_POB.split(DOB)[-1]
                    issued_date = new_dates[1]
                    Renew_due_date = new_dates[2]
                else:
                    dates = re.findall("\d\d.*\d\d", Driver_contents)
                    date_and_POB = re.search(
                        f'{dates[0]}.*\n',
                        Driver_contents).group(0).split('\n')[0]
                    DOB = dates[0]
                    POB = date_and_POB.split(DOB)[-1]
                    issued_date = dates[1]
                    Renew_due_date = dates[2]

        #       Names
                Names = Driver_contents.split(date_and_POB)[0]
                Names = re.split('\d.', Names)
                Names = ' '.join(Names)
                Names = Names.split('\n')
                Names = [name.strip() for name in Names if name]
                Names = [name for name in Names if name]
                if Names[0] == 'UK':
                    Names = Names[1:]
                if len(Names) > 2:
                    First_Name = Names[0]
                    if not 'UK' in Names:
                        Other_Name = ' '.join(Names[1:])
                    else:
                        Other_Name = Names[1]
                    Full_Name = First_Name + ' ' + Other_Name
                    Full_Name = Full_Name.split(' ')
                    Full_Name = sorted(Full_Name)
                    Full_Name = ' '.join(Full_Name)
                    print(Full_Name)
                else:
                    First_Name = Names[0]
                    Other_Name = Names[1]
                    Full_Name = First_Name + ' ' + Other_Name
                    Full_Name = Full_Name.split(' ')
                    Full_Name = sorted(Full_Name)
                    Full_Name = ' '.join(Full_Name)
                    print(Full_Name)

        #       Authority
                Authority = re.search(f'{issued_date}.*\n',
                                      Driver_contents).group(0)
                Authority_EX = re.findall("\d\d.*\d\d", Authority)
                authority_date = []
                for date in Authority_EX:
                    dat = ''.join(date)
                    authority_date.append(dat)
                Authority = Authority.split(authority_date[-1])[-1]
                Authority = Authority.split(' ')[-1]
                Authority = Authority.split('\n')[0]

                #       LICENSE NUMBER
                License_No = Driver_contents.split(Renew_due_date)[1]
                License_No = License_No.split('\n')
                License_No = [
                    license.strip() for license in License_No if license
                ]
                match_license = []
                for license in License_No:
                    if re.search("\w.*\d", license):
                        match_license.append(license)
                if len(match_license) > 0:
                    License_No = match_license[0]
                else:
                    License_No = 'None'

        #       Address
                Address = Driver_contents.split(License_No)[-1]
                Address_list = Address.split('\n')

                # Merge the Address if it continues in another line
                output = []
                for i in range(len(Address_list) - 1):
                    if str(Address_list[i]).endswith(','):
                        new_word = Address_list[i] + ' ' + Address_list[i + 1]
                        output.append(new_word)
                        next_word = Address_list[i + 1]
                        Address_list.remove(next_word)
                    elif Address_list[i] == max(Address_list, key=len) and str(
                            Address_list[i]).endswith('.'):
                        new_word = Address_list[i] + ' ' + Address_list[i + 1]
                        output.append(new_word)
                        next_word = Address_list[i + 1]
                        Address_list.remove(next_word)
                    else:
                        new_word = Address_list[i]
                        output.append(new_word)

                Address_list = output
                Address = max(Address_list, key=len)
                if Address.isupper():
                    Address = Address
                elif max(Address_list, key=len):
                    Address = Address
                else:
                    Address = [
                        address for address in Address_list
                        if address.isupper()
                    ][0]

                # Entitlement categoory
                Entitlement_categoory = Driver_contents.split(License_No)[-1]
                Entitlement_categoory = Entitlement_categoory.split('\n')
                Entitlement_categoory = [
                    category for category in Entitlement_categoory if category
                ][-1]
                if Entitlement_categoory == max(Address_list, key=len):
                    Entitlement_categoory = 'None'
                elif re.search(f"{Entitlement_categoory}", Address):
                    Entitlement_categoory = 'None'
                else:
                    Entitlement_categoory = Entitlement_categoory.split(
                        ' ')[-1]
                    Entitlement_categoory = Entitlement_categoory

                DRIVER_CONTENT = {}

                DRIVER_CONTENT['Full_Name'] = Full_Name
                #             DRIVER_CONTENT['Other Name'] = Other_Name
                DRIVER_CONTENT['Date of birth'] = DOB
                DRIVER_CONTENT['Place of birth'] = POB
                DRIVER_CONTENT['Issued Date'] = issued_date
                DRIVER_CONTENT['Renew Due Date'] = Renew_due_date
                DRIVER_CONTENT['Issuing authority'] = Authority
                DRIVER_CONTENT['License Number'] = License_No
                DRIVER_CONTENT['Address'] = Address
                DRIVER_CONTENT[
                    'Entitlement categories'] = Entitlement_categoory

                return DRIVER_CONTENT
            else:
                print('Document is Not a Driving License')
                DRIVER_CONTENT = {
                    'Contents':
                    "The Uploaded Document does not seem like a Driving License"
                }
                return DRIVER_CONTENT

        PCO_CONTENT = Process_PCO_License(PCO_String_Content)
        DRIVER_CONTENT = Process_Driver_License(Driver_String_Content)

        return PCO_CONTENT, DRIVER_CONTENT

    PCO_CONTENT, DRIVER_CONTENT = Processor(Drivers_texts, PCO_texts)

    # PCO_CONTENT
    for x, y in PCO_CONTENT.items():
        print(x + ":  " + colored(y, 'green'))
    #     print(f"{x}\x1b[31m{':  ' + y}\x1b[0m")

    # DRIVER_CONTENT
    for x, y in DRIVER_CONTENT.items():
        print(x + ":  " + colored(y, 'blue'))
    #     print(f"{x}\x1b[31m{':  ' + y}\x1b[0m")

    def Compare_License(Driver_Content, PCO_Content, Driver_Image, PCO_IMage):
        def check_Match_Names(Driver_Content, PCO_Content):
            Driver_Full_Name = Driver_Content['Full_Name'].upper()
            PCO_Full_Name = PCO_Content["Full_Name"].upper()

            Names_Match = {}
            if Driver_Full_Name == PCO_Full_Name:
                Names_Match["Full_Name_Matched"] = True
            else:
                Names_Match["Full_Name_Matched"] = False

            return Names_Match

        def check_Image_Similarities(Driver_Image, PCO_IMage):

            hash0 = imagehash.average_hash(Driver_Image)
            hash1 = imagehash.average_hash(PCO_IMage)
            cutoff = 5

            Image_Similar = {}
            if hash0 - hash1 < cutoff:
                Image_Similar["images_similar"] = True
                print('images_are_similar')
                print('')
            else:
                Image_Similar["images_similar"] = False
                print('images are not similar')
                print('')
            return Image_Similar

        try:
            compared_names = check_Match_Names(Driver_Content, PCO_Content)
        except:
            compared_names = {"Full_Name_Matched": None}
        try:
            compared_images = check_Image_Similarities(Driver_Image, PCO_IMage)
        except:
            compared_images = {"images_similar": None}
        result = {**compared_names, **compared_images}

        return result

    result = Compare_License(DRIVER_CONTENT, PCO_CONTENT, Driver_Image,
                             PCO_IMage)

    # Display Results
    for x, y in result.items():
        if y == True:
            print(x + ":  " + colored(y, 'green'))
        else:
            print(x + ":  " + colored(y, 'green'))

    unique_id = uuid.uuid4().hex
    print(unique_id)

    # data

    pco_original_image = open(img1, 'rb')
    driver_original_image = open(img2, 'rb')

    pco_cropped_image = PCO_IMage
    driver_cropped_image = Driver_Image

    try:
        buf = io.BytesIO()
        pco_cropped_image.save(buf, format='PNG')
        pco_cropped_image = buf.getvalue()
    except:
        pco_cropped_image = pco_cropped_image

    try:
        buf = io.BytesIO()
        driver_cropped_image.save(buf, format='PNG')
        driver_cropped_image = buf.getvalue()
    except:
        driver_cropped_image = driver_cropped_image

    pco_details = json.dumps(PCO_CONTENT)
    driver_details = json.dumps(DRIVER_CONTENT)
    pco_and_driver_license = {
        'PCO_LICENSE': unique_id,
        'DRIVER_LICENSE': unique_id
    }
    pco_and_driver_license = json.dumps(pco_and_driver_license)
    new_pco_details = PCO_CONTENT
    new_driver_details = DRIVER_CONTENT

    if result['Full_Name_Matched'] == True:
        username = DRIVER_CONTENT['Full_Name'].split(' ')
        username = '******'.join(username)
    else:
        username = None

    # Upload to PCO-license bucket
    s3 = boto3.resource('s3',
                        aws_access_key_id=ACCESS_KEY_ID,
                        aws_secret_access_key=ACCESS_SECRET_KEY,
                        config=Config(signature_version='s3v4'))

    s3.Bucket(pco_BUCKET_NAME).put_object(Key=f'{unique_id}/user_image.png',
                                          Body=pco_cropped_image,
                                          Tagging=f'unique_id={unique_id}')
    s3.Bucket(pco_BUCKET_NAME).put_object(Key=f'{unique_id}/user_detail.json',
                                          Body=pco_details,
                                          Tagging=f'unique_id={unique_id}')

    print("Done")

    # Upload to Driver-license bucket
    s3 = boto3.resource('s3',
                        aws_access_key_id=ACCESS_KEY_ID,
                        aws_secret_access_key=ACCESS_SECRET_KEY,
                        config=Config(signature_version='s3v4'))

    s3.Bucket(driver_BUCKET_NAME).put_object(Key=f'{unique_id}/user_image.png',
                                             Body=driver_cropped_image,
                                             Tagging=f'unique_id={unique_id}')
    s3.Bucket(driver_BUCKET_NAME).put_object(
        Key=f'{unique_id}/user_detail.json',
        Body=driver_details,
        Tagging=f'unique_id={unique_id}')

    print("Done")

    # Upload to user-license bucket
    s3 = boto3.resource('s3',
                        aws_access_key_id=ACCESS_KEY_ID,
                        aws_secret_access_key=ACCESS_SECRET_KEY,
                        config=Config(signature_version='s3v4'))

    s3.Bucket(user_BUCKET_NAME).put_object(
        Key=f'{unique_id}/pco_user_image.png',
        Body=pco_original_image,
        Tagging=f'unique_id={unique_id}')
    s3.Bucket(user_BUCKET_NAME).put_object(
        Key=f'{unique_id}/driver_user_image.png',
        Body=driver_original_image,
        Tagging=f'unique_id={unique_id}')
    s3.Bucket(user_BUCKET_NAME).put_object(Key=f'{unique_id}/user_info.json',
                                           Body=pco_and_driver_license,
                                           Tagging=f'unique_id={unique_id}')

    print("Done")

    pco_original_image.close()
    driver_original_image.close()

    return pco_license_original_image, driver_license_original_image, pco_cropped_image, driver_cropped_image, new_pco_details, new_driver_details, result, unique_id
Ejemplo n.º 22
0
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"GoogleYelpDemo.json
# Get GCS bucket
storage_client = storage.Client()
bucket = storage_client.bucket('dimsim')
image_paths = []
for blob in list(bucket.list_blobs()):
image_paths.append("gs://dimsim/"+blob.name)
# We can send a maximum of 16 images per request.
start = 0
end = 16
label_output = []
for i in range(int(np.floor(len(image_paths)/16))+1):
requests = []
client = vision_v1.ImageAnnotatorClient()
for image_path in image_paths[start:end]:
image = types.Image()
image.source.image_uri = image_path
requests.append({
"image": image,
"features":
[{"type_": vision_v1.Feature.Type.LABEL_DETECTION}]
})
response = client.batch_annotate_images(requests=requests)
print("Waiting for operation to complete...")
for image_path, i in zip(image_paths[start:end], response.responses):
labels = [{label.description: label.score} for label in i.label_annotations]
labels = {k: v for d in labels for k, v in d.items()}
filename = os.path.basename(image_path)
l = {'filename': filename, 'labels': labels}
label_output.append(l)
start = start+16
Ejemplo n.º 23
0
def parser(image):

    # Google API client
    client = vision.ImageAnnotatorClient()

    # Open and Read contents on the documents
    with io.open(os.path.join(image), 'rb') as image_file1:
        content = image_file1.read()
    content_image = types.Image(content=content)  # Reading the Image Content
    rc1_response = client.text_detection(image=content_image)  # Text Detection
    rc1_texts = rc1_response.text_annotations  # Text Response

    # face detection
    response_face = client.face_detection(image=content_image)
    faceAnnotations = response_face.face_annotations

    def arrange_the_texts(rc1_response):

        # rearrange the Response using the bbox values
        items = []
        lines = {}

        for text in rc1_response.text_annotations[1:]:
            left_x_axis = text.bounding_poly.vertices[0].x  # top left
            left_y_axis = text.bounding_poly.vertices[0].y  # top left
            top_x_axis = text.bounding_poly.vertices[1].x  # top
            top_y_axis = text.bounding_poly.vertices[1].y  # top
            right_x_axis = text.bounding_poly.vertices[2].x  # right
            right_y_axis = text.bounding_poly.vertices[2].y  # right
            bottom_x_axis = text.bounding_poly.vertices[3].x  # bottom
            bottom_y_axis = text.bounding_poly.vertices[3].y  # bottom

            if left_y_axis not in lines:
                lines[left_y_axis] = [(left_y_axis, bottom_y_axis), []]
            for s_top_y_axis, s_item in lines.items():
                if left_y_axis < s_item[0][1]:
                    lines[s_top_y_axis][1].append(
                        ([left_x_axis, bottom_y_axis,
                          top_x_axis], text.description))
                    break
        for _, item in lines.items():
            if item[1]:
                words = sorted(item[1], key=lambda t: t[0])
                items.append(
                    (item[0], ' '.join([word for _, word in words]), words))

        # Find the most common space
        def find_common_space(items):
            spaces = []
            for i, k in enumerate(items):
                try:
                    calculated_space = items[i][2][0][0][1] - items[
                        i - 1][2][0][0][1]
                    spaces.append(calculated_space)
                except:
                    ...

            common_space = abs(statistics.median(spaces))

            return common_space

        # print(find_common_space(items))

        # Split the content based on horizontal space
        new_content = []

        most_common_space = find_common_space(items)
        # print(most_common_space)

        len_content = len(items) / 5.5

        for i, k in enumerate(items):
            try:
                if abs(items[i][2][0][0][1] - items[i - 1][2][0][0][1]
                       ) >= most_common_space - len_content:
                    new_content.append([])
                    new_content.append(items[i][2])
                else:
                    new_content.append(items[i][2])
            except:
                new_content.append(items[i][2])

        # group the contents based on horizontal space
        new_content = [list(l) for i, l in groupby(new_content, bool) if i]

        # join list that the length is greater than one
        for i in range(len(new_content)):
            if len(new_content[i]) > 1:
                new_content[i] = sum(new_content[i], [])
                new_content[i] = [new_content[i]]
            else:
                new_content[i] = new_content[i]

        # find common width of white space on the document
        def find_common_diff(content):
            diffs = []
            for i, j in enumerate(new_content):
                for u, t in enumerate(new_content[i][0]):
                    try:
                        difference = abs(new_content[i][0][u][0][2] -
                                         new_content[i][0][u - 1][0][0])
                        diffs.append(difference)
                    except:
                        ...
            common_diffs = abs(statistics.median(diffs))
            return common_diffs

        # print(find_common_diff(new_content))

        # find common width of white space on a line
        def find_common_diff_in_line(content):
            diffs = []
            for i, j in enumerate(new_content):
                temp_diffs = []
                for u, t in enumerate(new_content[i][0]):
                    try:
                        difference = abs(new_content[i][0][u][0][0] -
                                         new_content[i][0][u - 1][0][2])
                        temp_diffs.append(difference)
                    except:
                        ...
                diffs.append(temp_diffs)
            return diffs

        # print(find_common_diff_in_line(new_content))

        def check_for_max_width(new_content):
            '''Check for max with of a text in a line'''
            widths = []
            for i, j in enumerate(new_content):
                temp_diffs = []
                for u, t in enumerate(new_content[i][0]):
                    try:
                        text_width = abs(new_content[i][0][u][0][0] -
                                         new_content[i][0][u][0][2])
                        temp_diffs.append(text_width)
                    except:
                        ...
                widths.append(temp_diffs)
            max_width = max(widths)
            return widths

        # print(check_for_max_width(new_content))

        # split line content based on vertical spacing
        contents = []
        max_text_width = 600
        most_common_diffs = find_common_diff(new_content)

        for i, j in enumerate(new_content):
            temp_content = []
            most_common_diffs_in_a_line = abs(
                statistics.median(find_common_diff_in_line(new_content)[i]))
            text_width = check_for_max_width(new_content)[i]
            max_width = max(text_width)
            #     print('')
            for u, t in enumerate(new_content[i][0]):
                try:
                    difference = abs(new_content[i][0][u][0][0] -
                                     new_content[i][0][u - 1][0][2])
                    if most_common_diffs >= most_common_diffs_in_a_line and max_width < max_text_width:
                        if difference >= most_common_diffs_in_a_line + 20:  # getting the min width of white space in that line
                            temp_content.append([])
                            temp_content.append(new_content[i][0][u])

                        else:
                            temp_content.append(new_content[i][0][u])
                    elif most_common_diffs >= most_common_diffs_in_a_line and max_width > max_text_width:
                        if difference > most_common_diffs_in_a_line - 15:  # getting the min width of white space in that line
                            temp_content.append([])
                            temp_content.append(new_content[i][0][u])

                        else:
                            temp_content.append(new_content[i][0][u])
                    else:
                        if difference > most_common_diffs - 65:  # getting the most common width of white space in the doc
                            temp_content.append([])
                            temp_content.append(new_content[i][0][u])

                        else:
                            temp_content.append(new_content[i][0][u])
                except:
                    ...
        #     print(temp_content)
            new_temp_content = [
                list(l) for i, l in groupby(temp_content, bool) if i
            ]
            contents.append(new_temp_content)
        #     contents.append(temp_content)
        return contents

    def extract_keys_and_values(contents):
        """ This functionality extracts they keys and values as in the document structure"""
        def check_for_non_english(new_content):
            """this function Counts the number of non-english and english texts in a line"""
            max_pos = 800  # this is the max starting position that an english word does exceed
            translate_client = translate.Client()
            target = 'en'
            detected_words = []
            for i, j in enumerate(new_content):
                temp_words = []
                for u, t in enumerate(new_content[i]):
                    counts = {'en': 0, 'af': 0}
                    left_bbox = contents[i][u][0][0][0]
                    for s, r in enumerate(new_content[i][u]):
                        try:
                            text = new_content[i][u][s][1]
                            output = translate_client.translate(
                                text, target_language=target)
                            if output[
                                    'detectedSourceLanguage'] == 'en' or left_bbox < max_pos:
                                counts['en'] += 1
                            else:
                                counts['af'] += 1
                        except:
                            ...
                    temp_words.append(counts)
                detected_words.append(temp_words)
            return detected_words

        word_detection = check_for_non_english(contents)

        new_contents = []
        max_position = 1200
        list_of_special_keywords = [
            'RC1', 'RNC', 'RTS', 'NRW'
        ]  # special key words to add to the extracted values

        for i, j in enumerate(contents):  #
            temp_content = []
            word_detected = word_detection[i]
            content = contents[i]
            for u, t in enumerate(contents[i]):
                left_bbox = contents[i][u][0][0][0]
                if word_detected[u]['en'] > word_detected[u][
                        'af'] and left_bbox < max_position or contents[i][u][
                            0][1] in list_of_special_keywords:
                    temp_content.append(content[u])
            new_contents.append(temp_content)

        # get the left bbox of the keys
        left_bbox_lists = []
        for i in range(len(new_contents)):
            try:
                left_bbox = new_contents[i][0][0][0][0]
                left_bbox_lists.append(left_bbox)
            except:
                left_bbox = 0
                left_bbox_lists.append(left_bbox)
        # print(left_bbox_lists)
        max_left_bbox = max(left_bbox_lists)

        # getting the keys and values
        # getting the keys and values
        rc1_contents = []
        max_starting_pos = 250

        for i in range(len(new_contents)):
            keys = ''
            values = ''
            for u in range(len(new_contents[i])):
                left_bbox = new_contents[i][u][0][0][0]
                if left_bbox <= max_left_bbox and left_bbox <= max_starting_pos:
                    try:
                        for j in range(len(new_contents[i][u])):
                            text = new_contents[i][u][j][1]
                            keys += ' ' + text
                    except:
                        ...
                else:
                    try:
                        for j in range(len(new_contents[i][u])):
                            text = new_contents[i][u][j][1]
                            values += ' ' + text
                    except:
                        ...
            # if the content of the current line does not have a key but has a value.
            # if so join the value with the value of the previous item in the rc1_contents.
            # Also check if the current key started with uppercase and the next key started wih lowercase
            # if so merge the current key with the next key
            last_key = ''
            last_value = ''
            try:
                last_key = [i for i, j in rc1_contents[-1].items()
                            ][0]  # previous item key
                last_value = [j for i, j in rc1_contents[-1].items()
                              ][0]  # previous item value
            except:
                ...

            if keys == '' and values != '' and last_key and last_value:  # check for condition
                k = ''
                val = ''
                for key, value in rc1_contents[-1].items():
                    if key and value:
                        val = value
                        val += ' ' + values
                        k = key
        #                 print(key, value)
                    else:
                        k = key
                        val = value
                rc1_contents.remove(rc1_contents[-1])
                rc1_contents.append({k: val})
            elif last_key != '' and keys != '':
                if last_key.strip()[0].isupper() and (
                        keys.strip()[0].islower() and not values):
                    k = ''
                    val = ''
                    for key, value in rc1_contents[-1].items():
                        if key and value:
                            val = value
                            k = key
                            k += ' ' + keys
            #                 print(key, value)
                        else:
                            k = key
                            val = value
                    rc1_contents.remove(rc1_contents[-1])
                    rc1_contents.append({k: val})
                else:
                    rc1_contents.append({keys: values})

            else:
                rc1_contents.append({keys: values})
        return rc1_contents

    contents = arrange_the_texts(rc1_response)
    contents = extract_keys_and_values(contents)

    keys_to_pull_out_RNC = [
        'Registering authority', 'Traffic register number', 'Name',
        'Postal address', 'Street address',
        'Address where notices must be served', 'Control number',
        'Issue number', 'Date of issue'
    ]
    keys_not_to_pull_out_RC1 = [
        "4024", "at Registering which registered authority", "RET(7)(2005/02)",
        "Republic of South Africa"
    ]
    keys_not_to_pull_out_NRW = [
        "NRW(2)(2003/10)", "NOTICE",
        "PARTICULARS (National Road Traffic OF Act, VEHICLE"
    ]

    keys_to_pull_out_RNC = [i.upper().strip() for i in keys_to_pull_out_RNC]
    keys_not_to_pull_out_RC1 = [
        i.upper().strip() for i in keys_not_to_pull_out_RC1
    ]
    keys_not_to_pull_out_NRW = [
        i.upper().strip() for i in keys_not_to_pull_out_NRW
    ]

    extracted_values = []

    # check if the special keys are in the document, if so pull the right keys
    for e, f in contents[0].items():
        if 'RNC' in f:
            for i in range(len(contents)):
                for key, value in contents[i].items():
                    key = ' '.join(key.split())
                    if key.upper() in keys_to_pull_out_RNC:
                        extracted_values.append(contents[i])
        elif 'RC1' in f:
            for i in range(len(contents)):
                for key, value in contents[i].items():
                    key = ' '.join(key.split())
                    if key.upper() in keys_not_to_pull_out_RC1:
                        ...
                    else:
                        if key != '':
                            extracted_values.append(contents[i])
        elif 'NRW' in f:
            for i in range(len(contents)):
                for key, value in contents[i].items():
                    key = ' '.join(key.split())
                    if key.upper() in keys_not_to_pull_out_NRW:
                        ...
                    else:
                        if key != '':
                            extracted_values.append(contents[i])
        else:
            extracted_values.append(
                {'Oops!': 'document has a different structure'})

    return extracted_values
Ejemplo n.º 24
0
def detect_faces(face_content: bytes):
    client = ImageAnnotatorClient()

    image = types.Image(content=face_content)
    results = client.face_detection(image=image)
    return results.face_annotations
Ejemplo n.º 25
0
    def _prepare_image(self):
        with io.open(self.file_name, 'rb') as image_file:
            content = image_file.read()

        return types.Image(content=content)
Ejemplo n.º 26
0
client = vision_v1.ImageAnnotatorClient()


#saving selected image in the program directory for google API processing
def save_uploaded_file(uploadedfile):
  with open(os.path.join("./",uploadedfile.name),"wb") as f:
     f.write(uploadedfile.getbuffer())
  return st.success("Selected image {}".format(uploadedfile.name))

st.title("  Student Answer Evaluator")
st.sidebar.write("# Menu")
img_file = st.sidebar.file_uploader(label='', type=['png', 'jpg'],help="upload image to be evaluated")
if img_file:
    img = Image.open(img_file)
    st.header("Selected Image")
    st.image(img)
    st.write(img_file.name)
    save_uploaded_file(img_file)
    FILE_PATH=img_file.name   
    with io.open(FILE_PATH, 'rb') as image_file:
        content = image_file.read()
 
    image = types.Image(content=content)
    response = client.document_text_detection(image=image)

    docText = response.full_text_annotation.text
    st.write(docText)
else:
    st.header('Select An Image')