Exemplo n.º 1
0
def loadObjects(training_folder):
    if training_folder[-1] != '/':
        training_folder += '/'

    ids = helpers.loadObject(training_folder + 'ids.txt')
    linear_clf = helpers.loadObject(training_folder + 'svm.txt')
    centroids = helpers.loadObject(training_folder + 'centroids.txt')

    return (ids, linear_clf, centroids)
Exemplo n.º 2
0
def loadObjects(training_folder):
    if training_folder[-1] != '/':
        training_folder += '/'

    ids = helpers.loadObject(training_folder + 'ids.txt')
    linear_clf = helpers.loadObject(training_folder + 'svm.txt')
    centroids = helpers.loadObject(training_folder + 'centroids.txt')

    return (ids, linear_clf, centroids)
Exemplo n.º 3
0
 def make_fruit( self ):
     randNumber = randrange(0, 10,1)
   
     if randNumber <4:
         self.fruit = loadObject( "cat", self.mode, pos=Point2( self.snake.fruit[ X ], self.snake.fruit[ Y ] ) )
         self.set_timer()
     elif 3< randNumber <7:
         self.fruit = loadObject( "cat1", self.mode,pos=Point2( self.snake.fruit[ X ], self.snake.fruit[ Y ] ) )
         self.speed_up()
     elif 6<randNumber<8:
         self.fruit = loadObject( "cat4", self.mode,pos=Point2( self.snake.fruit[ X ], self.snake.fruit[ Y ] ) )
         self.gen_bomb()
         self.draw_bomb()
     elif 7 < randNumber < 9:
         self.fruit = loadObject( "cat3", self.mode, pos=Point2( self.snake.fruit[ X ], self.snake.fruit[ Y ] ) )
         self.speed_down()
     else:
         self.fruit = loadObject( "cat2", self.mode, pos=Point2( self.snake.fruit[ X ], self.snake.fruit[ Y ] ) )
         self.change_keys()
Exemplo n.º 4
0
    def game_loop( self, task ):
        #check that player has chosen a mode/theme
        if self.start == True:
            dt = task.time - task.last
            #if timer power-up triggered, count down from 10 
            if self.timer_flag:
                if self.timer_start == 0:
                    self.timer_start = task.time
                timer_dt = task.time - self.timer_start
                self.update_timer(timer_dt)
                if (timer_dt >= 10.00):
                    self.check_top_score()
                    return task.done
            if not self.snake.alive: 
                self.check_top_score()
                return task.done
            if self.pause:
                return task.cont
            elif dt >= self.period:
                task.last = task.time
                #check if snake has hit bomb
                self.check_bomb( )
                self.snake.move_forward( )
                self.snake.check_state( )
                self.update_snake( )
                self.update_fruit( )
                self.update_score( )
                return task.cont
            else:
                return task.cont

        else:
            if self.choose_mode:
                self.background     = loadObject( "background2", self.mode, scale=140, depth=200, transparency=False )
                self.gameboard      = loadObject( "background", self.mode, scale=39.5, depth=100, transparency=False )
                self.draw_snake( )
                self.make_fruit( )
                self.start = True
                self.mode_text.removeNode( )
                return task.cont

            return task.cont
Exemplo n.º 5
0
    def __init__(self):
        ShowBase.__init__(self)

        self.disableMouse()
        self.snake = snake.Snake(body=[(-7, 1), (-8, 1), (-9, 1)], dot=(-7, 1))
        self.snake.gen_dot()

        self.background = loadObject("background",
                                     scale=9000,
                                     depth=200,
                                     transparency=False)
        self.gameboard = loadObject("background",
                                    scale=39.5,
                                    depth=100,
                                    transparency=False)
        self.escape_text = genLabelText("ESC  : Quit", 0)
        self.pause_text = genLabelText("SPACE: Pause", 1)
        self.score = genLabelText("SCORE: %s" % self.snake.get_score(),
                                  0,
                                  left=False)

        self.bricks = deque()
        self.make_dot()

        self.draw_snake()
        self.accept("escape", sys.exit)
        self.accept("enter", self.restart)
        self.accept("arrow_up", self.snake.turn, [POS_Y])
        self.accept("arrow_down", self.snake.turn, [NEG_Y])
        self.accept("arrow_left", self.snake.turn, [NEG_X])
        self.accept("arrow_right", self.snake.turn, [POS_X])
        self.accept("space", self.tooggle_pause)

        self.game_task = taskMgr.add(self.game_loop, "GameLoop")
        self.game_task.last = 0
        self.period = 0.15
        self.pause = False
Exemplo n.º 6
0
def train_routine(training_file, output_folder):
    """The main training routine.
    Input: training_file: <class>;<path to image>
    output_folder: A previous created folder where output files
    are written.
    Runs the routine: Get SURF features, calculate bag of words,
    then train the linear SVM using those.
    Then writes the required objects onto files.
    """
    if output_folder[-1] != '/':
        output_folder += '/'

    svm_file = output_folder + 'svm.txt'
    centroid_file = output_folder + 'centroids.txt'
    ids_file = output_folder + 'ids.txt'

    surf = cv2.SURF(250, extended=False)
    categories = dict()
    ids = dict()
    id = 1
    features = list()

    print "Extracting features"
    for line in open(training_file):
        try:
            category, path = line.split(';')
        except:
            print "Error: File not in proper format. Ensure: <category/class name>; <path to image of said category>"
            sys.exit(0)
        path = path.strip()

        try:
            img = cv2.imread(path)
            #img = cv2.resize(img, (500, 500))
        except Exception as e:
            print e
            continue

        keypoints, descriptors = surf.detectAndCompute(img, None)

        if not category in categories:
            categories[category] = Category(label=category)
            ids[category] = id
            id += 1
        categories[category].add_feature(descriptors)

    #for category in categories:
        #f = categories[category].yield_features()
        ##features.extend(f)
        #for i in f:
            #features.extend(i)

    print "Calculating centroids"
    #np_features = numpy.array(features)
    #print "Features: ", np_features.shape
    #centroids, labels = kmeans2(np_features, FEATURE_TYPES)
    centroids = helpers.loadObject(output_folder + 'centroids.txt')
    print centroids.shape

    print "Forming bag of words"
    X, Y = [], []
    for category in categories:
        categories[category].calc_bagofwords(centroids)
        for bow in categories[category].bagofwords:
            X.append(bow)
            Y.append(ids[category])
    print "Fitting linear SVMs onto the bag of words"
    lin_clf = svm.LinearSVC()
    lin_clf.fit(X, Y)

    helpers.saveObject(lin_clf, svm_file)
    helpers.saveObject(centroids, centroid_file)
    helpers.saveObject(ids, ids_file)
Exemplo n.º 7
0
 def make_dot(self):
     self.dot = loadObject("brick",
                           pos=Point2(self.snake.dot[X], self.snake.dot[Y]))
Exemplo n.º 8
0
 def draw_snake(self):
     for point in self.snake.body:
         brick = loadObject("brick", pos=Point2(point[X], point[Y]))
         self.bricks.append(brick)
Exemplo n.º 9
0
 def draw_bomb( self ):
     for square in self.wall:
         bomb = loadObject( "bomb", self.mode, pos=Point2( square[ X ], square[ Y ] ) )
         self.bombs.append( bomb )
Exemplo n.º 10
0
 def draw_snake( self ):
     for point in self.snake.body:
         brick = loadObject( "cat", self.mode, pos=Point2( point[ X ], point[ Y ] ) )
         self.bricks.append( brick )