Example #1
0
 def create_xs_ys(self):
     self.files_list = \
     lslR.get_files(directory=self.IMAGE_DIRECTORY,include=self.include,\
             exclude = self.exclude,ftype=self.ftypes)
     #print files_list
     for file in self.files_list:
         for item in self.keywords.iteritems():
             #the 1st entry is the class number 0,1,2,3...
             key = item[0]
             #the second entry is the reg-exp identifying the class 
             value = item[1]
             if file.rfind(value) != -1 :
                 #print key , file
                 self.xs.append(initial_processing.imageToVector(file))
                 self.ys.append(key)
Example #2
0
    def test(self,image_directory,classKeys,include=None,exclude=None):
        """
        The method implements the testing part of the algorithm and returns
        the successrate in fractions between 0 and 1
        """
        #the following reassignment is done to keep the __init__
        #method and the test method arguments consistent
        test_path = image_directory
        test_classes = classKeys
        includ = include
        exclud = exclude

        print "Starting to Test"
        #test_path = raw_input("Enter the Path containing test images:")
        #test_pattern = list(raw_input("Enter the regexp identifying \
        #test images in the directory above:"))
        test_files = \
        lslR.get_files(directory=test_path,ftype=self.ftypes,include=includ,exclude=exclud)

        num = len(test_files)
        i = 0
        success = 0
        failure = 0
        for fil in test_files:
            #if num - i > 0 :
            #    print "processing Image number: (%d) , Corresponding to File name: %s " % (i,fil)
            xTest = \
            numpy.matrix(initial_processing.imageToVector(fil))
            xTest_class = -1
            #----------------debug code------------
            #print type(xTest)
            #--------------------------------------
            #project testing image on A
            #yTest = numpy.transpose(self.A * xTest.transpose())
            yTest = xTest * self.A.transpose()
            #find the distance of yTest from each Yi
            distAndYPair = [] #inf denotes infinity
            #self.y_Transpose = numpy.transpose(self.y)
            #for y in self.y_Transpose:
            for y in self.y:
                #print type(yTest),type(y),yTest.shape,y.shape
                dist = spatial.distance.euclidean(yTest,y)
                #if dist < minPair[0]:
                #    print "iterating to find the minimum match"
                #    print "y :",y
                #    print "yTest :",yTest
                #    minPair[0] = dist
                #    minPair[1] = tuple(y)
                distAndYPair.append([dist,y])
            #print "type of minPair[1]"+str(type(minPair[1]))
            #print minPair[1] in self.y_Transpose
            distAndYPair.sort()
            #print minPair[1] in numpy.transpose(self.y)
            #print type(self.y[0])
            #print "Minimum distance: ",minPair[0]

            recognized_class = self.yZipYs[distAndYPair[0][1]]
            #because the class number starts from 0 and the numbering of 
            #of 
            for key in test_classes.keys():
                if fil.find(key) != -1:
                    xTest_class = test_classes[key]
                    break
            if recognized_class == xTest_class :
                success += 1
            else:
                failure += 1
            #print "Success %d , Failure %d" % (success,failure) 
            #print "Recognized Class: %d And xTest_Class: %d " % \
            #(recognized_class,xTest_class) 
            
            i +=1
        rate = (float(success)/float(success+failure))
        #print "Success Rate is : %f" % rate
        return rate