Example #1
0
 def loadSingle( self, filename, inverse = False ):
   key = filename + ": inverse=%i" % inverse
   if not key in self.images:
     data  = Data()
     image = pygame.image.load( filename ).convert_alpha()
     if inverse:
       image = pygame.transform.flip( image, True, False )
     data.image = image
     data.rect  = image.get_rect()
     data.tightImage = image.subsurface( image.get_bounding_rect() )
     data.tightRect  = data.tightImage.get_rect()
     self.images[key] = data
   return key
Example #2
0
 def scaled( self, key, width, height ):
   scaledKey = key + ", width=%i, height=%i" % ( width, height )
   try:
     return self.images[scaledKey]
   except KeyError:
     data = Data()
     image = pygame.transform.smoothscale( self.images[key].tightImage, (width, height ) )
     data.image = image
     data.rect  = image.get_rect()
     data.tightImage = image.subsurface( image.get_bounding_rect() )
     data.tightRect  = data.tightImage.get_rect()
     self.images[scaledKey] = data
     return data
Example #3
0
 def block( self, width, height, color ):
   key = "block: width=%i, height=%i" % ( width, height )
   key += " (r:%i, g:%i, b:%i, a:%i)" % color
   if not key in self.images:
     data  = Data()
     image = pygame.Surface( ( width, height ) ).convert_alpha()
     image.fill( color )
     data.image = image
     data.rect  = image.get_rect()
     data.tightImage = image.subsurface( image.get_bounding_rect() )
     data.tightRect  = data.tightImage.get_rect()
     self.images[key] = data
   return key
Example #4
0
 def loadMulti( self, filename, count, inverse = False ):
   keys = [
     filename + ": count=%i, inverse=%i, frame=%i" % ( count, inverse, i)
     for i in xrange( count )
   ]
   if not keys[0] in self.images:
     full = pygame.image.load( filename ).convert_alpha()
     width     = full.get_width() / count
     height    = full.get_height()
     indices = range( count )
     for i in indices:
       data  = Data()
       image = full.subsurface( ( i  * width, 0, width, height ) )
       if inverse:
         image = pygame.transform.flip( image, True, False )
       data.image = image
       data.rect  = image.get_rect()
       data.tightImage = image.subsurface( image.get_bounding_rect() )
       data.tightRect  = data.tightImage.get_rect()
       self.images[keys[i]] = data
   return keys
Example #5
0
from utility import Data, evaluate
import svm_models
import time
if __name__ == "__main__":
    start_time = time.time()
    data = Data()
    data.lda_transform()
    acc = []
    for X_train, X_test, y_train, y_test in data.load():
        svm = svm_models.SklearnSVC(X_train, y_train)
        acc.append(svm.score(X_test, y_test))
    end_time = time.time()
    evaluate(acc)
    print('total time: %.4fs' % (end_time - start_time))
from utility import Data, evaluate
import svm_models
import time
if __name__ == "__main__":
    start_time = time.time()
    data = Data()
    data.pca_transform(0.9)
    acc = []
    for X_train, X_test, y_train, y_test in data.load():
        svm = svm_models.SklearnLinearSVC(X_train, y_train)
        acc.append(svm.score(X_test, y_test))
    end_time = time.time()
    evaluate(acc)
    print('total time: %.4fs' % (end_time-start_time))
Example #7
0
from utility import Data, evaluate, convert_to_ovr
from svm_models import LinearSVM
import time
if __name__ == "__main__":
    start_time = time.time()
    data = Data()
    acc, correct = [], []
    for X_train, X_test, y_train, y_test in data.load():
        correct_sum, acc_sum = 0, 0
        for z_train, z_test in convert_to_ovr(y_train, y_test):
            clf = LinearSVM()
            clf.fit(X_train, z_train)
            correct_cnt, single_acc = clf.score(X_test, z_test)
            correct_sum += correct_cnt
            acc_sum += single_acc
        acc.append(acc_sum / 10)
        correct.append(0.1 * correct_sum / len(y_test))
    end_time = time.time()
    print('Overall Accuracy:')
    evaluate(correct)
    print('Average Accuracy:')
    evaluate(acc)
    print('total time: %.4fs' % (end_time - start_time))