예제 #1
0
    def process_image(self, image):
        file_converter = FileConverter()
        #convert the image to byte string
        image_bytes = file_converter.png_to_jpeg(image)

        scanner = Scanner()
        #scan the image and give it a birds eye view, returns a np of pixels that makes up the image
        scan_np = scanner.scan(image_bytes)

        #extract the individual answers from the scanned test
        extractor = Extract()
        answers = extractor.get_all_answers(scan_np, 5)

        color = Color()
        bw_answers = color.all_ans_to_bw(answers)

        size = Size()
        DIM = (28, 28)
        shrunk_images = size.shrink_images(bw_answers, DIM)

        #convert the answer images to a single array, which we used in training our model
        answers_flat = file_converter.convert_images(
            shrunk_images)  #returns image as (1, 28, 28, 1) and as type float

        #now that we have a list of images of the answers as bw 1D numpy arrays,
        # we can run them through our model and grade them
        # first we need to load our model
        model_loader = ModelLoader()
        MODEL_JSON = 'models/modified_model_98.json'
        MODEL_WEIGHTS = 'models/modified_model_98.h5'
        model = model_loader.load_model_2(MODEL_JSON, MODEL_WEIGHTS)
        #compile model
        model.compile(optimizer=RMSprop(lr=0.001),
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

        grader = Grader()
        answers = grader.get_answers(answers_flat, model)

        #get the images as a 784 (28x28) length string so we can store the data in a database
        ans_strings = file_converter.get_string_images(answers_flat)
        compressed_images = file_converter.compress_images(ans_strings)

        #add the images to database so we can create a large dataset of handwritten letters
        # storage = Storage()
        # storage.insert(answers, compressed_images)

        return answers
예제 #2
0
 def __init__(self):
     self.pygame = pygame
     self.pygame.init()
     self.ga = GeneticTrainer()
     self.bounds = (640, 480)
     self.fittest_index = 0
     self.screen = pygame.display.set_mode(self.bounds, 0, 32)
     self.pop = 10
     self.rockets = [Rocket(self.bounds, (self.bounds[0] / 2 - 35, 0)) for _ in range(self.pop)]
     self.tick = GameTickPacket(self.rockets)
     self.grader = Grader(self.tick)
     self.running = True
     self.clock = pygame.time.Clock()
     self.bot = 0
     self.num_best = 8
     self.gen = 0
     self.gen_cap = 1000
     self.timeout = 100
     self.running = True
     self.plotter = Plotter()
                  "WHERE login = '******' AND problem = '{}';".format(login, name))
  for i in cur:
    sql_id   = i[0]
    sql_date = i[1]
    sql_stat = i[2]
    sql_name = i[3]

  if a != 0 and sql_name != user:
    cur.execute("UPDATE practice SET name = '{}' WHERE id='{}';" \
                .format(user, sql_id))
    conn.commit()

  if a == 0 or (sql_stat != 'complete' and float(sql_date) < date):
    tmp_dir = tempfile.mkdtemp()
    Repo.clone_from(repo.clone_url, tmp_dir)
    sub = Grader(tmp_dir)
    stat = sub.run()

    if stat < 8:
      message = messages[stat]
    else:
      message = 'internal error 999'

    print(message)

    if a == 0:
      cur.execute("INSERT INTO practice (name, login, problem, status, date) " \
                  "VALUES ('{}', '{}', '{}', '{}', '{}')"\
                  .format(user, login, name, message, date))
    else:
      cur.execute("UPDATE practice SET status='{}', date='{}' WHERE id='{}';" \
    def run(self):
        while True:
            #grabs job from queue, when they appear
            file_name, info = self.queue.get()

            #set description(used by gui)
            self.description = '{} {:02} ({:03})'.format(
                info['team_name'], info['problem_id'], info['attempts'])

            #create a grader obj(to grade the submission)
            submission = Grader(file_name, self.problem_dir,
                                info['problem_id'], self.timeout)
            '''
      Values for result:
        0: not graded
        1: good(complete)
        2: formatting error
        3: compile error
        4: no main class found
        5: run time error
        6: ran for too long
        7: outputs do not match
        other: very very bad error
      '''

            #possible return messages(should be a class thing)
            messages = [
                'not graded', 'complete', 'formatting error', 'compile error',
                'no main class found', 'run time error', 'ran for too long',
                'outputs do not match'
            ]

            #As I distrust Oracle, I have created a xml file to store in the archive
            xml = '''<submission>
  <team id=\"{team_id}\">{team_name}</team>
  <problem>{problem_id}</problem>
  <grade code={grade_code}>{grade_message}</grade>
  <attempt>{attempt}</attempt>
  <time>{submission_time}</time>
</submission>'''

            if submission.extract_info(
            ):  #find main method(return true if found)
                if submission.compile():  #attempt to compile(true if comiles)
                    result = submission.run(
                    )  #run submission and get status(some number)
                else:
                    result = 3  #could not compile
            else:
                result = 4  #main not found

            #set archive name for submission
            archive_name = '{team_id}_{problem_id}_{attempt}.zip'.format(
                team_id=info['team_id'],
                problem_id=info['problem_id'],
                attempt=info['attempts'])
            #set absolute path for submission archive
            archive_name = os.path.join(self.archive_dir, archive_name)
            info_file = tempfile.mkstemp()[1]  #temporary xml file
            with open(info_file, 'w') as f:  #write to temp xml file
                f.write(xml.format(team_name=info['team_name'],
                                   team_id=info['team_id'],
                                   problem_id=info['problem_id'],
                                   attempt=info['attempts'],
                                   grade_code=result,
                                   grade_message=messages[result] if result < 8 and \
                                     result > 0 else 'Unknown ERROR(bad)',
                                   submission_time=info['time']))

            zipper = zipfile.ZipFile(archive_name, 'w',
                                     zipfile.ZIP_DEFLATED)  #open arch
            for root, dirs, files in os.walk(
                    submission.get_dir()):  #add files to arch
                for file in files:  # from submission
                    zipper.write(os.path.join(root, file), arcname=file)
            zipper.write(info_file, arcname='info.xml')  #add xml file to arch
            zipper.close()

            info['result'] = result  #set the result
            self.done.append(info)  #tell gui we done

            #delete tmp stuffs
            os.remove(info_file)  #info file(inside of new archive)

            shutil.rmtree(
                file_name)  #original submission(graded copy is in archive_dir)
            if os.path.exists(file_name):
                os.rmdir(file_name)

            shutil.rmtree(
                submission.get_dir())  #grading dir(already in new archive)
            if os.path.exists(submission.get_dir()):
                os.rmdir(submission.get_dir())

            self.cursor = self.sql.cursor(
            )  #prepare to tell db the status of stuff
            #updates results into 'graded' column of /table/
            query = 'UPDATE {} SET grade=\'{}\' WHERE submission_name=\'{}\''
            self.cursor.execute(
                query.format(self.subs_table, result, info['submission_name']))

            #notification command for VM
            command = 'notify.sh {} {}'.format(info['problem_id'],
                                               info['result'])

            #send notification command to VM using ssh using pub key
            if 'submission_ip' in info.keys() and info['submission_ip']:
                ssh = subprocess.Popen([
                    'ssh', '-i', 'client_rsa', '-o',
                    'StrictHostKeyChecking=no',
                    'guest@' + info['submission_ip'], command
                ],
                                       shell=False,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
            else:  #if failure, print
                message = 'Please tell team \"{}\" that the result for problem {} is: {}'
                print(message.format(info['team_name'], info['problem_id'],
                                     messages[result] if result < 8 and result > 0 \
                                                      else 'Unknown ERROR(bad)'))

            self.cursor.close()
            self.sql.commit()  #not sure, but this fixed an error
            #signals to queue job is done
            self.queue.task_done()
            self.description = None  #not working on stuff anymore
                    "WHERE login = '******' AND problem = '{}';".format(login, name))
    for i in cur:
        sql_id = i[0]
        sql_date = i[1]
        sql_stat = i[2]
        sql_name = i[3]

    if a != 0 and sql_name != user:
        cur.execute("UPDATE practice SET name = '{}' WHERE id='{}';" \
                    .format(user, sql_id))
        conn.commit()

    if a == 0 or (sql_stat != 'complete' and float(sql_date) < date):
        tmp_dir = tempfile.mkdtemp()
        Repo.clone_from(repo.clone_url, tmp_dir)
        sub = Grader(tmp_dir)
        stat = sub.run()

        if stat < 8:
            message = messages[stat]
        else:
            message = 'internal error 999'

        print(message)

        if a == 0:
            cur.execute("INSERT INTO practice (name, login, problem, status, date) " \
                        "VALUES ('{}', '{}', '{}', '{}', '{}')"\
                        .format(user, login, name, message, date))
        else:
            cur.execute("UPDATE practice SET status='{}', date='{}' WHERE id='{}';" \
예제 #6
0
class GameManager:
    def __init__(self):
        self.pygame = pygame
        self.pygame.init()
        self.ga = GeneticTrainer()
        self.bounds = (640, 480)
        self.fittest_index = 0
        self.screen = pygame.display.set_mode(self.bounds, 0, 32)
        self.pop = 10
        self.rockets = [Rocket(self.bounds, (self.bounds[0] / 2 - 35, 0)) for _ in range(self.pop)]
        self.tick = GameTickPacket(self.rockets)
        self.grader = Grader(self.tick)
        self.running = True
        self.clock = pygame.time.Clock()
        self.bot = 0
        self.num_best = 8
        self.gen = 0
        self.gen_cap = 1000
        self.timeout = 100
        self.running = True
        self.plotter = Plotter()

    def draw(self):

        self.pygame.display.update()
        self.clock.tick(60)
        # self.drawText("GEN: " + str(self.gen), 10, 10, 10)
        # self.drawText("BOT: " + str(self.bot), 10, 10, 10)

    def drawText(self, text, x, y, size):
        for char in text:
            charSprite = self.pygame.transform.scale(self.charSprites[char], (size, size))
            self.screen.blit(charSprite, (x, y))
            if char == " ":
                x += size // 2
            else:
                x += size

    def run(self):
        while self.running:
            while self.gen < self.gen_cap:

                self.bot = 0

                while self.bot < self.pop:
                    while not self.rockets[self.bot].isDead:
                        for event in self.pygame.event.get():
                            if event.type == pygame.QUIT:
                                sys.exit()

                        x, y = self.rockets[self.bot].draw_rocket()
                        self.screen.fill((246, 246, 246))
                        self.screen.blit(self.rockets[self.bot].ball, (x, y))
                        self.draw()

                        self.tick.update(self.bot)

                        if self.tick.latest_tick > self.timeout:
                            self.rockets[self.bot].timeout()
                            break

                    #self.plotter.show(self.tick.rockets[self.fittest_index].thrust)
                    self.rockets[self.bot].fitness = self.grader.calc_fitness(self.bot)
                    self.tick.reset()

                    for i in self.rockets:
                        i.isDead = False

                    print(str(self.gen) + " | " + str(self.bot) + " | " + str(
                        sum(sum(self.rockets[self.bot].model.fc1.weight.data))) + " | " +
                          str(self.rockets[self.bot].fitness))

                    self.bot += 1

                print("-----------------")
                print("FITTEST: " + str(self.fittest_index) + " | " +
                      str(sum(sum(self.rockets[self.fittest_index].model.fc1.weight.data))))
                print("-----------------")
                self.fittest_index = self.grader.calc_fittest(self.rockets)
                self.ga.clone(self.rockets, self.fittest_index)
                self.ga.mutate(self.rockets, self.num_best)
                self.gen += 1
예제 #7
0
    def run(self):
        while True:
            # grabs job from queue, when they appear
            file_name, info = self.queue.get()

            # set description(used by gui)
            self.description = "{} {:02} ({:03})".format(info["team_name"], info["problem_id"], info["attempts"])

            # create a grader obj(to grade the submission)
            submission = Grader(file_name, self.problem_dir, info["problem_id"], self.timeout)

            """
      Values for result:
        0: not graded
        1: good(complete)
        2: formatting error
        3: compile error
        4: no main class found
        5: run time error
        6: ran for too long
        7: outputs do not match
        other: very very bad error
      """

            # possible return messages(should be a class thing)
            messages = [
                "not graded",
                "complete",
                "formatting error",
                "compile error",
                "no main class found",
                "run time error",
                "ran for too long",
                "outputs do not match",
            ]

            # As I distrust Oracle, I have created a xml file to store in the archive
            xml = """<submission>
  <team id=\"{team_id}\">{team_name}</team>
  <problem>{problem_id}</problem>
  <grade code={grade_code}>{grade_message}</grade>
  <attempt>{attempt}</attempt>
  <time>{submission_time}</time>
</submission>"""

            if submission.extract_info():  # find main method(return true if found)
                if submission.compile():  # attempt to compile(true if comiles)
                    result = submission.run()  # run submission and get status(some number)
                else:
                    result = 3  # could not compile
            else:
                result = 4  # main not found

            # set archive name for submission
            archive_name = "{team_id}_{problem_id}_{attempt}.zip".format(
                team_id=info["team_id"], problem_id=info["problem_id"], attempt=info["attempts"]
            )
            # set absolute path for submission archive
            archive_name = os.path.join(self.archive_dir, archive_name)
            info_file = tempfile.mkstemp()[1]  # temporary xml file
            with open(info_file, "w") as f:  # write to temp xml file
                f.write(
                    xml.format(
                        team_name=info["team_name"],
                        team_id=info["team_id"],
                        problem_id=info["problem_id"],
                        attempt=info["attempts"],
                        grade_code=result,
                        grade_message=messages[result] if result < 8 and result > 0 else "Unknown ERROR(bad)",
                        submission_time=info["time"],
                    )
                )

            zipper = zipfile.ZipFile(archive_name, "w", zipfile.ZIP_DEFLATED)  # open arch
            for root, dirs, files in os.walk(submission.get_dir()):  # add files to arch
                for file in files:  # from submission
                    zipper.write(os.path.join(root, file), arcname=file)
            zipper.write(info_file, arcname="info.xml")  # add xml file to arch
            zipper.close()

            info["result"] = result  # set the result
            self.done.append(info)  # tell gui we done

            # delete tmp stuffs
            os.remove(info_file)  # info file(inside of new archive)

            shutil.rmtree(file_name)  # original submission(graded copy is in archive_dir)
            if os.path.exists(file_name):
                os.rmdir(file_name)

            shutil.rmtree(submission.get_dir())  # grading dir(already in new archive)
            if os.path.exists(submission.get_dir()):
                os.rmdir(submission.get_dir())

            self.cursor = self.sql.cursor()  # prepare to tell db the status of stuff
            # updates results into 'graded' column of /table/
            query = "UPDATE {} SET grade='{}' WHERE submission_name='{}'"
            self.cursor.execute(query.format(self.subs_table, result, info["submission_name"]))

            # notification command for VM
            command = "notify.sh {} {}".format(info["problem_id"], info["result"])

            # send notification command to VM using ssh using pub key
            if "submission_ip" in info.keys() and info["submission_ip"]:
                ssh = subprocess.Popen(
                    [
                        "ssh",
                        "-i",
                        "client_rsa",
                        "-o",
                        "StrictHostKeyChecking=no",
                        "guest@" + info["submission_ip"],
                        command,
                    ],
                    shell=False,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )
            else:  # if failure, print
                message = 'Please tell team "{}" that the result for problem {} is: {}'
                print(
                    message.format(
                        info["team_name"],
                        info["problem_id"],
                        messages[result] if result < 8 and result > 0 else "Unknown ERROR(bad)",
                    )
                )

            self.cursor.close()
            self.sql.commit()  # not sure, but this fixed an error
            # signals to queue job is done
            self.queue.task_done()
            self.description = None  # not working on stuff anymore
예제 #8
0
def run_standard_grader():
    grader = Grader()
    grader.grade()