コード例 #1
0
def main():

    # load obstacles
    template_files = os.listdir(r"images/obstacle_images")
    for index, fileName in enumerate(template_files):
        template_files[index] = r'images/obstacle_images/' + fileName

    dinosaur_path = './images/dinosaur.PNG'

    # Create image processing object to use detection with
    imgProcess = ImageProcess(template_files, dinosaur_path)

    print('focus game')
    time.sleep(2)
    i = 0
    gameOver = False
    while gameOver != -1:
        img = ScreenCapture.get_screen(top=172,
                                       left=-1543,
                                       width=600,
                                       height=125,
                                       delay=0)
        gameOver = imgProcess.get_distance(img)
        if gameOver:
            print(gameOver)
        i += 1
コード例 #2
0
ファイル: main.py プロジェクト: C-Advait/SuperSpeedDino
def main():

    # load obstacles
    template_files = os.listdir("images/obstacle_images")
    for index, fileName in enumerate(template_files):
        template_files[index] = "images/obstacle_images/" + fileName

    # Create image processing object to use detection with
    imgProcess = ImageProcess(template_files, "images/dinosaur.PNG")

    #get image and find distance to all obstacles
    img = ScreenCapture.get_screen(
        top = 172, left = -1543, width = 600,
        height = 125, delay = 0
        )

    cv2.imshow('test', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    res = imgProcess.get_distance(img, drawRect = False)
    print(res)
    # raise IndexError
    #get score of the run
    score_img = ScreenCapture.get_screen(
        top=142, left= -1009, width=65, height=20, delay=0
    )
    # imgProcess.show_image(cv2.cvtColor(score_img, cv2.COLOR_BGRA2GRAY))
    score = imgProcess.get_score(score_img, show_score=True)
    print(int(score))
コード例 #3
0
    def __init__(self, create_info=True):
        self.score = None

        # Create image processing object to use detection with
        template_files = os.listdir("images/obstacle_images")
        for index, fileName in enumerate(template_files):
            template_files[index] = "images/obstacle_images/" + fileName
        dinosaur_image_path = r"images/dinosaur.PNG"

        self.game_vision = ImageProcess(template_files, dinosaur_image_path)

        self.obs_names = self.game_vision.get_obs_names()

        try:
            self.obs_names.remove('game_over')
        except ValueError:
            pass

        self.decisionGenes = {name: None for name in self.obs_names}
        # print(self.decisionGenes)

        for key in self.decisionGenes.keys():
            self.decisionGenes[key] = np.empty([650, 100], dtype=object)

        logging.basicConfig(filename='play_excepts.log',
                            filemode='w',
                            format='%(name)s - %(levelname)s - %(message)s')

        if create_info:
            self.create_genetic_info()
コード例 #4
0
    def button_spit_num(self, bool):
        image = self.paint.getPixMap()
        log.info_out(image)

        self.image_spit = ImageProcess(image, self.spit_Box.currentIndex() + 1)

        if len(self.image_spit) == 0:
            QMessageBox.warning(self, "waring", "无法正确分割数字,或者无数字")
            return

        point = self.image_spit.get_rect_point()

        for x, y, x1, y1 in point:
            self.paint.paintLine(x, y, x1 - x, y1 - y)
コード例 #5
0
    def button_recognize(self, bool):
        image = self.paint.getPixMap()
        self.image_spit = ImageProcess(image, self.spit_Box.currentIndex() + 1)
        if len(self.image_spit) == 0:
            QMessageBox.warning(self, "waring", "无法正确分割数字,或者无数字")
            return

        num = []
        for index, (image) in enumerate(self.image_spit):
            # 自动膨胀和腐蚀
            if self.AutoED.isChecked():
                kernel = np.ones((2, 2), np.uint8)
                image = cv2.dilate(image, kernel)
                kernel = np.ones((1, 1), np.uint8)
                image = cv2.erode(image, kernel)

            if self.BinProcess.isChecked():
                image[image[:, :] < 100] = 0
                image[image[:, :] > 100] = 255

            # 显示测试图片预览
            if self.StudyPreView.isChecked():
                plt.figure(index)
                plt.imshow(image, cmap="gray")
                plt.show()

            # 数据转化为Torch格式
            data = torch.tensor([[image]]).float()

            # 开始识别
            out = self.net(data)

            # 找到最大值
            num.append(torch.max(out.data, 1)[1][0].item())

        # text控件显示结果
        s = ""
        for i in num:
            s = s + str(i)

        self.lineEdit.setText(s)
コード例 #6
0
    def myUI(self, w1, h1, w2, h2, name_input, name_output):
        '''
        given function performs the following tasks:
        1. read the image in BGR format
        2. convert w1, h1, w2, h2 window size in respective pixel format
        3. convert BGR image into Luv image
        4. find histogram of entire image on Luv domain,
           where L is in range of given window
        5. convert Luv image into BGR image
        6. write output image
        '''
        # 1. read the image in BGR format
        myIO = MyIO()
        bgrImg = myIO.readImage(name_input)
        
        # debug
        print("bgrImg =\n {}".format(bgrImg))
        # debug -ends
                
        # debug
        myIO.showImage(bgrImg, "BGR Image")
        # debug -ends

        # 2. convert w1, h1, w2, h2 window size in respective pixel format
        W1, H1, W2, H2 = myIO.windowsSizeMapping(inputImage = bgrImg,\
                                                 w1 = w1, h1=h1,\
                                                 w2 = w2, h2=h2)
        # debug
        print("W1 = {}, H1={}, W2={}, H2={}".format(W1, H1, W2, H2))
        # debug -ends


        # 3. convert BGR image into Luv image
        colorProcess=ColorProcess()
        LuvImg = colorProcess.bgrToLuv(bgrImg = bgrImg)
        # debug
        print("-----------------------------------------------------")
        print("\nLuvImg = \n{}".format(LuvImg))
        # debug -ends

        #4. find histogram of entire image on Luv domain,
        #   where L is in range of given window
        imageProcess = ImageProcess()
        HELuvImg = imageProcess.histogramEqualizationInLuv(LuvImg, W1, H1, W2, H2)
        
        # debug
        print("-----------------------------------------------------")
        print("HELuvImg = \n{}".format(HELuvImg))
        # debug -ends
        
        # 5. convert Luv image into BGR image
        HEBGRImage = colorProcess.LuvToBGR(LuvImage = HELuvImg)
        
        # debug
        myIO.showImage(HEBGRImage, "Histogram Equalized BGR Image")
        cv2.waitKey(0)
        # debug -ends

        # debug
        print("-----------------------------------------------------")
        print("HEBGRImage =\n {}".format(HEBGRImage))
        # debug -ends

        #6. write output image
        myIO.writeImage(outputImage = HEBGRImage, name_output = name_output)
コード例 #7
0
class mainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(mainWindow, self).__init__()
        self.setupUi(self)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("UI/ico/windows.png"), QtGui.QIcon.Normal,
                       QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.setWindowTitle("手写数字识别")
        self.paint = Painter(750, 520)
        self.gridLayout.addWidget(self.paint)
        self.net = Net()
        self.net.load_state_dict(torch.load("net/parameters.pt"))

        self.image_spit = None
        self.spinBox_px.setValue(10)

        with open("UI/css/mainwindow.css", "r") as file_css:
            self.setStyleSheet(file_css.read())

    def button_spit_num(self, bool):
        image = self.paint.getPixMap()
        log.info_out(image)

        self.image_spit = ImageProcess(image, self.spit_Box.currentIndex() + 1)

        if len(self.image_spit) == 0:
            QMessageBox.warning(self, "waring", "无法正确分割数字,或者无数字")
            return

        point = self.image_spit.get_rect_point()

        for x, y, x1, y1 in point:
            self.paint.paintLine(x, y, x1 - x, y1 - y)

    def button_clear(self, bool):
        self.paint.clearWidget()
        self.lineEdit.clear()

    def button_recognize(self, bool):
        image = self.paint.getPixMap()
        self.image_spit = ImageProcess(image, self.spit_Box.currentIndex() + 1)
        if len(self.image_spit) == 0:
            QMessageBox.warning(self, "waring", "无法正确分割数字,或者无数字")
            return

        num = []
        for index, (image) in enumerate(self.image_spit):
            # 自动膨胀和腐蚀
            if self.AutoED.isChecked():
                kernel = np.ones((2, 2), np.uint8)
                image = cv2.dilate(image, kernel)
                kernel = np.ones((1, 1), np.uint8)
                image = cv2.erode(image, kernel)

            if self.BinProcess.isChecked():
                image[image[:, :] < 100] = 0
                image[image[:, :] > 100] = 255

            # 显示测试图片预览
            if self.StudyPreView.isChecked():
                plt.figure(index)
                plt.imshow(image, cmap="gray")
                plt.show()

            # 数据转化为Torch格式
            data = torch.tensor([[image]]).float()

            # 开始识别
            out = self.net(data)

            # 找到最大值
            num.append(torch.max(out.data, 1)[1][0].item())

        # text控件显示结果
        s = ""
        for i in num:
            s = s + str(i)

        self.lineEdit.setText(s)

    def button_withdraw(self, bool):
        self.paint.withDraw()

    def button_clear_alltemp(self, bool):
        self.paint.disImage()

    def button_writeTemp(self, bool):
        self.paint.mergeTemp2Image()

    def button_preView(self, bool):
        self.paint.ProcessPic(self.YH_Box.currentIndex())

    def spin_num_change(self, num):
        self.paint.paintSize = num
        self.paint.update()
コード例 #8
0
class Player:
    def __init__(self, create_info=True):
        self.score = None

        # Create image processing object to use detection with
        template_files = os.listdir("images/obstacle_images")
        for index, fileName in enumerate(template_files):
            template_files[index] = "images/obstacle_images/" + fileName
        dinosaur_image_path = r"images/dinosaur.PNG"

        self.game_vision = ImageProcess(template_files, dinosaur_image_path)

        self.obs_names = self.game_vision.get_obs_names()

        try:
            self.obs_names.remove('game_over')
        except ValueError:
            pass

        self.decisionGenes = {name: None for name in self.obs_names}
        # print(self.decisionGenes)

        for key in self.decisionGenes.keys():
            self.decisionGenes[key] = np.empty([650, 100], dtype=object)

        logging.basicConfig(filename='play_excepts.log',
                            filemode='w',
                            format='%(name)s - %(levelname)s - %(message)s')

        if create_info:
            self.create_genetic_info()

    def jump(self, time, keypressMut):  # press up key for `time` seconds
        if keypressMut.acquire(blocking=False):
            # print("action is jump, time = ", time)
            keyDown("up")
            keyup_timer = threading.Timer(time, keyUp, args=("up", ))
            lock_release_timer = threading.Timer(time, keypressMut.release)
            keyup_timer.start()
            lock_release_timer.start()
        else:
            # print('another action has not released')
            pass

    def duck(self, time, keypressMut):  # press the down key for `time` seconds

        if keypressMut.acquire(blocking=False):
            # print('action is duck, time = ', time)
            keyDown("down")
            keyup_timer = threading.Timer(time, keyUp, args=("down", ))
            lock_release_timer = threading.Timer(time, keypressMut.release)
            keyup_timer.start()
            lock_release_timer.start()
        else:
            # print('another action has not released')
            pass

    def do_nothing(self, time, keypressMut):

        if keypressMut.acquire(blocking=False):
            # print('action is nothing, time = ', time)
            lock_release_timer = threading.Timer(time, keypressMut.release)
            lock_release_timer.start()
        else:
            # print('another action has not released')
            pass

    def create_genetic_info(self):

        #ground obstacles can only be jumped over
        #bird can be ducked or jumped over
        ground_obs_actions = [self.jump, self.do_nothing]
        bird_obs_actions = [self.jump, self.do_nothing, self.duck]

        for key in self.decisionGenes.keys():
            if 'bird' not in key:
                for i in range(len(self.decisionGenes[key])):
                    for j in range(len(self.decisionGenes[key][i])):

                        randSleep = random.uniform(0.1, 1.5)

                        self.decisionGenes[key][i][j] = [
                            random.choice(ground_obs_actions), randSleep
                        ]
            else:
                for i in range(len(self.decisionGenes[key])):
                    for j in range(len(self.decisionGenes[key][i])):

                        randSleep = random.uniform(0.1, 1.5)

                        self.decisionGenes[key][i][j] = [
                            random.choice(bird_obs_actions), randSleep
                        ]

    def noneTest(self):
        for key in self.decisionGenes:
            for x_list in self.decisionGenes[key]:
                for y in x_list:
                    if None in y:
                        print('key: ', key)

    @staticmethod
    def copy_player(individual):
        new_player = Player(create_info=False)
        for key in individual.decisionGenes:
            new_player.decisionGenes[key] = copy.deepcopy(
                individual.decisionGenes[key])

        return new_player

    def play(self):

        keypressMut = threading.Lock()
        pattern = re.compile('_\d+')
        game_over = False

        time.sleep(1)  #game actually resets
        press("space")  # start game
        time.sleep(0.5)  #game starts
        print("game started ")

        while not game_over:
            # game starts. find image and take action
            img = ScreenCapture.get_screen(top=172,
                                           left=-1543,
                                           width=600,
                                           height=125,
                                           delay=0.4)
            res = self.game_vision.get_distance(img)
            if res:
                # print(res)
                try:
                    obstacle, distance = res
                    match = pattern.search(obstacle)

                    obs_name = obstacle[:match.span()[0]]
                    action, wait = self.decisionGenes[obs_name][distance[0]][
                        distance[1]]
                    if action == None:
                        print(action, wait, distance[0], distance[1])
                        raise IndexError
                    action(wait, keypressMut)

                #res = -1
                except TypeError as e:
                    game_over = True

        score_img = ScreenCapture.get_screen(top=142,
                                             left=-1009,
                                             width=65,
                                             height=20,
                                             delay=0)
        try:
            score = int(self.game_vision.get_score(score_img))

        #Score could not be converted to int
        #Issue with OCR software
        except ValueError as e:
            with open('./errors/score_read.log', 'a+') as scoreFail:
                scoreFail.write(str(e))
            t = time.localtime()
            timestamp = time.strftime('%b-%d-%Y_%H%M', t)
            FILE_NAME = ("score_fail-" + timestamp)
            cv2.imwrite(r'./errors' + FILE_NAME + '.bmp', score_img)
            score = 42

        print('game done', score, '\n')

        return score

    def __len__(self):
        return 95100