예제 #1
0
    def writeWords( self, name, words, filename ):
        os.makedirs( name )
        os.chdir( name ) 
        output_file = { 'author': filename.split('-')[0], 'word_count':0, 'words': [], 'features': [] }   

        word_count = 0
        for line in words:
            for word in line:
                image = word[ 'image' ]
                if( np.shape( image )[1] > 0 and np.shape( image )[0] > 0 ):
                    if( np.shape( word[ 'image' ] )[ 1 ] < self.env.settings.content[ 'extract' ][ 'width'] ):
                        image = self.buff( image )                      

                    output_filename = '{0}_{1}.tif'.format( filename, word_count )

                    try:
                        cv2.imwrite( output_filename, image )
                    except:
                        error( f'Could not write {output_filename} to a file.' )

                    output_file[ 'words' ].append( '{0}_{1}.tif'.format( filename, word_count ) )
                    word_count += 1

        output_file[ 'word_count' ] = word_count
        open( '{0}.json'.format( filename ), 'a' ).write( json.dumps( output_file, indent=4, sort_keys = True ) )
        os.chdir( '..' )  

        return    
예제 #2
0
 def __init__( self, image_path, env ):
     state( 'Opening image...' )
     self.env = env
     if( self.setImage( image_path ) ):
         comment( '- Image opened successfully.')
     else:
         error( 'Could not set image from path in ProcessImage.' )
    def getMetric(self, image): 
        
        try:
            rows, cols = np.shape(image) 
            open_image = image_utils.Threshold(image, self.env.settings.content['signature']['threshold'])
            
            if open_image.success == False:
                return
            image = open_image.payload

            signature_out = ''

            for cols_counter in range(cols):
            
                signature_total = 0

                for row_counter in range(rows):
                    signature_total += image[row_counter][cols_counter]

                signature_total =  255 - int(round(signature_total / rows))

                signature_out += f'{signature_total}'
                
            return signature_out

        except Exception as e:
            error(e)
예제 #4
0
def showImage(image: object):
    try:
        cv2.imshow('Showing image', image)
        cv2.waitKey(0)
        return TestMessage(True, 'Image shown successfully.')
    except Exception as e:
        error(e)
        return TestMessage(False, e)
예제 #5
0
 def getJson(self, path: str):
     try:
         for file in os.listdir(path):
             if file.endswith('.json'):
                 path_to_file = f'{path}/{file}'
                 return path_to_file
     except:
         error(f"Could not find a .json file in {path}")
예제 #6
0
    def writeLines( self, name, lines, filename ):
        os.makedirs( name )
        os.chdir( name )    
        count = 1

        output_file = { 'author': filename.split('-')[0], 'line_count':0, 'lines': [], 'features': [] }
        for line in lines:
            output_file[ 'lines' ].append( '{0}_{1}.tif'.format( filename, count ) )
            try:
                cv2.imwrite( '{0}_{1}.tif'.format( filename, count ), line[ 'image' ])
                count += 1
            except:
                error( f'Could not write line to {filename}_{count}.tif')
        output_file[ 'line_count' ] = count-1
        open( '{0}.json'.format( filename ), 'a' ).write( json.dumps( output_file, indent = 4, sort_keys = True ) )
        os.chdir( '..' )  
 def test_error(self):
     """
         Test whether a successful error message can be made to the console.
     """
     self.assertEqual(
         error('Testing a error message').success, True,
         'A successful error message should return a success status of True.'
     )
예제 #8
0
 def writeFiles( self, bundle ):
     try:
         os.makedirs( bundle.getPath() )
         os.chdir( bundle.getPath() )
         files = Files( bundle.getPath() , self.env )
         comment( '- Writing content for subdirectories.' )
         files.writeWords( 'words', bundle.words, bundle.folder_name )
         files.writeLines( 'lines', bundle.lines, bundle.folder_name )
     except:
         error( 'Directory already exists.' )
         rm_file = input( 'Do you want to remove the directory [Y/n]?' )
         if( rm_file.upper() == 'Y' or rm_file == '' ):
             os.system( f'rm { bundle.getPath() } -r')
             os.makedirs( bundle.getPath() )
             os.chdir( bundle.getPath() )
             files = Files( bundle.getPath(), self.env )
             comment( '- Writing content for subdirectories.' )
             files.writeWords( 'words', bundle.words, bundle.folder_name )
             files.writeLines( 'lines', bundle.lines, bundle.folder_name )
예제 #9
0
    def __init__(self, env, feature: str, path=None):
        state('Setting up the image pipeline.')
        self._e = env

        if (path != None):
            directories = self.getDirectories(path)
            if (directories.success):
                # Inside each sample folder there are images for lines and words, which are folders.
                self.subject = ask('Would you like to use [words] or [lines]?',
                                   readInput.readline())
                while (not (self.subject.payload == 'words'
                            or self.subject.payload == 'lines')):
                    self.subject = ask('Type either [words] or [lines]?',
                                       readInput.readline())

                tmp_create_paths = self.setPaths(self.subject.payload)
                if (tmp_create_paths.success):
                    comment(tmp_create_paths.payload)
                    feature_files = self.getFeatureFiles()

                    available_features = self.getListOfAvailableFeatures()

                    comment('Adding features.')

                    for feature_class in available_features:
                        self.save(
                            self.generateFeatures(feature_files,
                                                  feature_class),
                            feature_class)

                else:
                    error(tmp_create_paths.payload)

                    raise IOError(tmp_create_paths.payload)
            else:
                error(directories.payload)
예제 #10
0
 def __init__( self, env, directory, dataset ):
     self.env = env
     try:
         self.processImages( f'{directory}{dataset}/', 'tif' )
     except:
         error( 'Could not process selected dataset.' )       
예제 #11
0
import os
import cv2
import numpy as np
from PIL import Image

out.state('Running the main application.')
e = Environment(out, os.getcwd())

if (out.confirm('Extract words from images').success):
    dataset = out.ask('What dataset would you like to use?')
    if (dataset.success):
        try:
            for key in e.paths.content.keys():
                if (key == dataset.payload):
                    ImagePipeline(e, f'{os.getcwd()}/images/', dataset.payload)
        except:
            out.error('Extracting failed')

if (out.confirm('Would you like to add a feature?').success):
    dataset = out.ask('What dataset would you like to use?')
    if (dataset.success):
        try:
            for key in e.paths.content.keys():
                if (key == dataset.payload):
                    features_pipe = FeaturePipeline(
                        e, f'{os.getcwd()}/images/{ dataset.payload }')
                    feature_files = features_pipe.getFeatureFiles()
        except:
            out.error('Failed to add feature to the dataset.')