示例#1
0
def extract(img_path, img_classes, param):
    """
    Function that performs the extraction of an image using the CCOM
    descriptor.
    
    This function transforms the image being extracted to the desired image
    format of the descriptor, performs the extraction of the image, and last,
    transforms the output feature vector of the descriptor into the standard
    output of the framework, a list of floats.
    """

    print "Descriptor: CCOM"

    #CONSTANTS
    #Number of executions of the extraction
    NUM_EXEC = 1

    #PATHS
    #Path for the folder containing the descriptor executable
    ##Not necessary in case that the extraction is present in the plugin code
    descriptor_path = os.path.dirname(__file__)
    #Path for the temporary folder, where the file with the feature vector
    #extracted from the image will be saved
    ##Consider the path with the origin on the directory of the plugin
    ##(os.path.dirname(__file__)) to avoid problems with the directory where
    ##the framework is being executed
    path_temp = os.path.join(os.path.dirname(__file__), "..", "..", "temp")
    if not os.path.isdir(path_temp):
        os.makedirs(path_temp)

    print img_path, "being extract in the process ", os.getpid()

    #Temporary name of the image in the desired image format
    list_classes = ""
    for name_class in img_classes:
        list_classes += str(name_class) + "_"
    list_names = img_path.split(os.sep)
    img_name = list_classes + list_names[-2] + "_" + list_names[-1]

    #Convert the image to the desired format of the descriptor
    temp_img_path, converted = util.convert_desired_format(
        img_path, img_name, "PPM")
    if converted:
        print "\tImage converted to PPM"

    #Path of the file with the feature vector of an image
    fv_path = os.path.join(path_temp, img_name + ".fv")

    # Extraction of the feature vector
    if not os.path.exists(fv_path):
        system_platform = [platform.system(), platform.architecture()[0]]
        if system_platform[0] == 'Linux':
            if system_platform[1] == '32bit':
                plugin_name = 'ccom_32l.so'
            else:
                plugin_name = 'ccom_64l.so'
        else:
            plugin_name = 'ccom_64l.so'

    #Extraction of the feature vector
    if not os.path.exists(fv_path):
        setup = """
ctypes = __import__('ctypes')
plugin = "%s"
lib = ctypes.CDLL("%s%s" + plugin)
img_path = "%s"
fv_path = "%s"
        """ % (plugin_name, descriptor_path, os.sep, temp_img_path, fv_path)

        cmd = """
lib.Extraction(img_path, fv_path)
        """

        t = timeit.Timer(stmt=cmd, setup=setup)
        try:
            t.timeit(number=NUM_EXEC)
            print "\tFeature vector extracted"
        except:
            t.print_exc()

    #Remove the temporary image
    if converted:
        os.remove(temp_img_path)

    #Transforms the feature vector of the descriptor into the standard output
    #of the framework
    fv = fv_transform(fv_path)

    return img_path, len(img_classes), img_classes, fv
示例#2
0
def extract(img_path, img_classes, param):
    """
    Function that performs the extraction of an image using the CEDD
    descriptor.
    
    This function transforms the image being extracted to the desired image
    format of the descriptor, performs the extraction of the image, and last,
    transforms the output feature vector of the descriptor into the standard
    output of the framework, a list of floats.
    """

    print "Descriptor: CEDD"

    #CONSTANTS
    #Number of executions of the extraction
    NUM_EXEC = 1

    #PATHS
    sys.path.append(os.path.dirname(__file__))
    #Path for the folder containing the descriptor executable
    ##Not necessary in case that the extraction is present in the plugin code
    descriptor_path = os.path.dirname(__file__)
    #Path for the temporary folder, where the file with the feature vector
    #extracted from the image will be saved
    ##Consider the path with the origin on the directory of the plugin
    ##(os.path.dirname(__file__)) to avoid problems with the directory where
    ##the framework is being executed
    path_temp = os.path.join(os.path.dirname(__file__), "..", "..", "temp")
    if not os.path.isdir(path_temp):
        os.makedirs(path_temp)

    print img_path, "being extract in the process ", os.getpid()

    #Temporary name of the image in the desired image format
    list_classes = ""
    for name_class in img_classes:
        list_classes += str(name_class) + "_"
    img_name = list_classes + img_path.split(os.sep)[-1]

    #Convert the image to the desired format of the descriptor
    temp_img_path, converted = util.convert_desired_format(
        img_path, img_name, "JPG")
    if converted:
        print "\tImage converted to JPG"

    #Path of the file with the feature vector of an image
    #CEDD does not need a file to write the feature vector

    #Extraction of the feature vector
    #-------------------------------------------------------------------------

    #Compile the .java files
    #   Change the current directory to the directory of the files
    orig_dir = os.getcwd()
    os.chdir(os.path.dirname(__file__))
    os.system("javac CEDD.java")
    print "\tJAVA files compiled"

    #Call subprocess jython
    jython_args = ['jython', 'jython_cedd.py', 'Extraction']
    jython_args.append(temp_img_path)
    jython_args.append(str(param))

    import subprocess
    cedd_process = subprocess.Popen(jython_args, stdout=subprocess.PIPE)
    print "\t", cedd_process
    cedd_extract = cedd_process.communicate()[0]
    #    cedd_process.wait()
    fv = util.fv_string_to_list(cedd_extract)

    #   Return to the original directory
    os.chdir(orig_dir)
    #-------------------------------------------------------------------------

    #Remove the temporary image
    if converted:
        os.remove(temp_img_path)

    #Transforms the feature vector of the descriptor into the standard output
    #of the framework
    #Not necessary. The return of the getDoubleHistogram is already list of
    #floats

    return img_path, len(img_classes), img_classes, fv
示例#3
0
def extract(img_path, img_classes, param):
    """
    Function that performs the extraction of an image using the GIST
    descriptor.
    
    This function transforms the image being extracted to the desired image
    format of the descriptor, performs the extraction of the image, and last,
    transforms the output feature vector of the descriptor into the standard
    output of the framework, a list of floats.
    """

    print "Descriptor: GIST"

    #CONSTANTS
    #Number of executions of the extraction
    NUM_EXEC = 1

    #PARAMETERS
    cell_size = param["Cell Size"]
    orientations = param["Orientations"]

    #PATHS
    #Path for the folder containing the descriptor executable
    ##Not necessary in case that the extraction is present in the plugin code
    descriptor_path = os.path.dirname(__file__)
    #Path for the temporary folder, where the file with the feature vector
    #extracted from the image will be saved
    ##Consider the path with the origin on the directory of the plugin
    ##(os.path.dirname(__file__)) to avoid problems with the directory where
    ##the framework is being executed
    path_temp = os.path.join(os.path.dirname(__file__), "..", "..", "temp")
    if not os.path.isdir(path_temp):
        os.makedirs(path_temp)

    print img_path, "being extract in the process ", os.getpid()

    #Temporary name of the image in the desired image format
    list_classes = ""
    for name_class in img_classes:
        list_classes += str(name_class) + "_"
    img_name = list_classes + img_path.split(os.sep)[-1]

    #Convert the image to the desired format of the descriptor
    temp_img_path, converted = util.convert_desired_format(
        img_path, img_name, "PPM")
    if converted:
        print "\tImage converted to PPM"

    #Path of the file with the feature vector of an image
    fv_path = os.path.join(path_temp, img_name + ".fv")

    #Extraction of the feature vector
    if not os.path.exists(fv_path):
        cmd = 'cd "%s"; ./compute_gist "%s" "%s" -nblocks %d \
            -orientationsPerScale "%s"' % (descriptor_path, temp_img_path,
                                           fv_path, cell_size, orientations)

        subprocess.call(cmd, shell=True)

    #Remove the temporary image
    if converted:
        os.remove(temp_img_path)

    #Transforms the feature vector of the descriptor into the standard output
    #of the framework
    fv = fv_transform(fv_path)

    return img_path, len(img_classes), img_classes, fv
示例#4
0
def extract(img_path, img_classes, param):
    """
    Function that performs the extraction of an image using the EXAMPLE
    descriptor.
    
    This function transforms the image being extracted to the desired image
    format of the descriptor, performs the extraction of the image, and last,
    transforms the output feature vector of the descriptor into the standard
    output of the framework, a list of floats.
    """
    
    print "Descriptor: EXAMPLE"
    
    #CONSTANTS
    #Number of executions of the extraction
    NUM_EXEC = 1
    
    #PATHS
    #Path for the folder containing the descriptor executable
    ##Not necessary in case that the extraction is present in the plugin code
    descriptor_path = os.path.dirname(__file__)
    #Path for the temporary folder, where the file with the feature vector
    #extracted from the image will be saved
    ##Consider the path with the origin on the directory of the plugin
    ##(os.path.dirname(__file__)) to avoid problems with the directory where
    ##the framework is being executed
    path_temp = os.path.join(os.path.dirname(__file__), "..", "..", "temp")
    if not os.path.isdir(path_temp):
        os.makedirs(path_temp)
    
    print img_path, "being extract in the process ", os.getpid()
    
    #Temporary name of the image in the desired image format
    list_classes = ""
    for name_class in img_classes:
        list_classes += name_class + "_"
    img_name = list_classes + img_path.split(os.sep)[-1]
    
    #Convert the image to the desired format of the descriptor
    temp_img_path, converted = util.convert_desired_format(img_path, img_name,
                                                          "EXAMPLE")
    if converted:
        print "\tImage converted to EXAMPLE"
    
    #Path of the file with the feature vector of an image
    fv_path = os.path.join(path_temp, img_name + ".fv")
    
    #Extraction of the feature vector
    if not os.path.exists(fv_path):
        #Example:
#        setup = """
#ctypes = __import__('ctypes')
#plugin = "EXAMPLE.so"
#lib = ctypes.CDLL("%s%s" + plugin)
#img_path = "%s"
#fv_path = "%s"
#        """%(descriptor_path, os.sep, temp_img_path, fv_path)
#        
#        cmd = """
#lib.Extraction(img_path, fv_path)
#        """
#        
#        t = timeit.Timer(stmt=cmd, setup=setup)
#        try:
#            t.timeit(number=NUM_EXEC)
#            print "\tFeature vector extracted"
#        except:
#            t.print_exc()
    
    #Remove the temporary image
    if converted:
        os.remove(temp_img_path)
    
    #Transforms the feature vector of the descriptor into the standard output
    #of the framework
    fv = fv_transform(fv_path)
    
    return img_path, len(img_classes), img_classes, fv

def fv_transform(fv_path):
    """
    Receive the path with the feature vector in the descriptor output and
    return the feature vector in the framework standard output, a list of
    floats.
    """
    
    list_fv = []
    
    #Open the file created by the descriptor, save the feature vector in the 
    #standard output, remove the file and return the new feature vector
    try:
        file_fv = open(fv_path, "rb")
    except IOError:
        print "ERROR"
        sys.exit(1)
    
    #Performs the necessary operations to transform the feature vector into
    #the standard output
    
    file_fv.close()
    os.remove(fv_path)
            
    print "\tFeature vector transformed in the standard output"
    
    return list_fv
    
def distance(fv1, fv2):
    """
    Performs the calculation of distance between the two feature vectors,
    according to the Distance function of the executable.
    
    Inputs:
        - fv1 (list of floats): First feature vector
        - fv2 (list of floats): Second feature vector
    
    Output:
        - distance (double): Distance between the two feature vectors
    """
    
    #Imports
    import ctypes
    
    descriptor_path = os.path.dirname(__file__)
    plugin_name = "EXAMPLE.so"
    plugin_path = os.path.join(descriptor_path, plugin_name)
    plugin = ctypes.CDLL(plugin_path)
    
    #Descriptor exclusive
    #-------------------------------------------------------------------------
    #Creating class requested by the Distance function
    class Class_Example(ctypes.Structure):
        #Example: Pointer to a float vector and an integer
        _fields_ = [("first_field", ctypes.POINTER(ctypes.c_double)),
                    ("second_field", ctypes.c_int)]
    
    #First Class
    Class1 = Class_Example()
    
    len_fv1 = len(fv1)
    fv1_int = map(float, fv1)
    c_first1 = (ctypes.c_double * len_fv1)(*fv1_int)
    
    Class1.first_field = ctypes.cast(c_first1, ctypes.POINTER(ctypes.c_double))
    Class1.second_field = ctypes.c_int(len_fv1)
    
    p_Class1 = ctypes.pointer(Class1)
    
    #Second Class
    Class2 = Class_Example()
    
    len_fv2 = len(fv2)
    fv2_int = map(float, fv2)
    c_first2 = (ctypes.c_double * len_fv2)(*fv2_int)
    
    Class2.first_field = ctypes.cast(c_first2, ctypes.POINTER(ctypes.c_double))
    Class2.second_field = ctypes.c_int(len_fv2)
    
    p_Class2 = ctypes.pointer(Class2)
    
    #Parameters of the Distance function
    plugin.Distance.restype = ctypes.c_double
    #-------------------------------------------------------------------------
    
    #Execution
    distance = plugin.Distance(p_Class1, p_Class2)
    
    return distance