当前位置: 首页>>代码示例>>Python>>正文


Python Algorithm.value方法代码示例

本文整理汇总了Python中algorithm.Algorithm.value方法的典型用法代码示例。如果您正苦于以下问题:Python Algorithm.value方法的具体用法?Python Algorithm.value怎么用?Python Algorithm.value使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在algorithm.Algorithm的用法示例。


在下文中一共展示了Algorithm.value方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: WebSudoku

# 需要导入模块: from algorithm import Algorithm [as 别名]
# 或者: from algorithm.Algorithm import value [as 别名]
class WebSudoku(object):

    def __init__(self, verbose):
        self._verbose = verbose
        self._values, self._centroids = self._grab_board()
        self._algo = Algorithm(self, self._values)

    def solve(self):
        self._algo.solve()
        if self._verbose:
            return

        for row in range(9):
            for col in range(9):
                if (row, col) in self._algo.board_default:
                    continue
                y, x = self._centroids[(row, col)]
                pyautogui.moveTo(x, y)
                pyautogui.click()
                pyautogui.press(str(self._algo.value(row, col)))

    def fill_cell(self, row, col, default=False):
        if not self._verbose and not default:
            return

        if random.random() < 0.2 and not default:
            #time.sleep(5)
            pass

        y, x = self._centroids[(row, col)]
        pyautogui.moveTo(x, y)
        pyautogui.click()

        value = self._algo.value(row, col)
        pyautogui.press('backspace')
        pyautogui.press('delete')
        pyautogui.press(str(value))

    def _grab_board(self):
        time.sleep(3)

        samples = np.float32(np.loadtxt('vectors.data'))
        responses = np.float32(np.loadtxt('samples.data'))

        model = cv2.KNearest()
        model.train(samples, responses)

        window = gtk.gdk.get_default_root_window()
        x, y, width, height, _ = window.get_geometry()
        ss = gtk.gdk.Pixbuf.get_from_drawable(gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, width, height),
                                              gtk.gdk.get_default_root_window(),
                                              gtk.gdk.colormap_get_system(),
                                              0, 0, x, y, width, height)
        ss.save(TMP_FILE, 'png')

        raw = cv2.imread(TMP_FILE)
        gray = cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY)
        threshold = cv2.adaptiveThreshold(gray, 255, 1, 1, 11, 15)
        cache = threshold.copy()
        contours, _ = cv2.findContours(threshold,
                                       cv2.RETR_LIST,
                                       cv2.CHAIN_APPROX_SIMPLE)

        squares = []
        for c in contours:
            c = cv2.approxPolyDP(c, 4, True)
            if len(c) != 4:
                continue
            if not cv2.isContourConvex(c):
                continue
            squares.append(c)

        board_size = max(cv2.contourArea(s) for s in squares)
        board = [s for s in squares if cv2.contourArea(s) == board_size][0]

        min_x = min(s[0][0] for s in board)
        max_x = max(s[0][0] for s in board)
        min_y = min(s[0][1] for s in board)
        max_y = max(s[0][1] for s in board)

        step_x = (max_x - min_x) / 9.0
        step_y = (max_y - min_y) / 9.0

        values = {}
        centroids = {}
        for y in range(9):
            values[y] = {}
            for x in range(9):
                local_min_y = min_y + (y * step_y) + 5
                local_max_y = min_y + ((y+1) * step_y) - 5
                local_min_x = min_x + (x * step_x) + 5
                local_max_x = min_x + ((x+1) * step_x) - 5

                roi = cache[
                    local_min_y:local_max_y,
                    local_min_x:local_max_x]

                centroids[(y, x)] = (
                    int((local_min_y + local_max_y) / 2.0),
                    int((local_min_x + local_max_x) / 2.0))
#.........这里部分代码省略.........
开发者ID:leongold,项目名称:websudoku,代码行数:103,代码来源:main.py


注:本文中的algorithm.Algorithm.value方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。