Example #1
0
 def __getitem__(self, item):
     if isinstance(item, np.ndarray) and item.ndim == 2:
         H, W = item.shape
         if (H, W) in known_dims:
             # looking for just one letter in a numpy array
             if item.dtype.name.startswith(
                     "int") or item.dtype.name == "bool":
                 item = [["#" if val else "." for val in row]
                         for row in item]
             item = "\n".join(["".join(row) for row in item])
         elif H in {h for (h, w) in known_dims}:
             widths = {w for (h, w) in known_dims if H == h}
             for w in sorted(widths, reverse=True):
                 n, rem = divmod(W, w)
                 if rem == 0:
                     # could be a word? retry OCR per character
                     chars = [item[:, w * i:w * (i + 1)] for i in range(n)]
                     txt = "".join([self[char] for char in chars])
                     return txt
     if isinstance(item, dict):
         grid = ZGrid(item)
         grid.translate({0: ".", 1: "#"})
         full = np.array(grid)
         full = full[:, 1:-2]  # TODO: template matching
         return self.__getitem__(full)
     if isinstance(item, np.ndarray):
         self.__missing__(array2txt(item))
     return super(DebugDict, self).__getitem__(item)
Example #2
0
 def __getitem__(self, item):
     if isinstance(item, np.ndarray) and item.ndim == 2:
         if item.dtype == "U1":
             item = (item == "#").astype(int)
         item = autocrop(item)
         txt = array2txt(item).replace("0", ".").replace("1", "#")
         if txt in known:
             return known[txt]
         H, W = item.shape
         for k, v in glyphs.items():
             h, w = v.shape
             if h == H and (item[:, :w] == v).all():
                 letter = known[k]
                 return letter + self.__getitem__(item[:, w:])
         item = txt  # trigger fallthrough to __missing__
     if isinstance(item, (dict, ZGrid)):
         grid = ZGrid(item)
         grid.translate({grid.off: 0, grid.on: 1})
         full = np.array(grid)
         full = autocrop(full)
         return self.__getitem__(full)
     if isinstance(item, np.ndarray):
         self.__missing__(array2txt(item))
     return super(DebugDict, self).__getitem__(item)
def test_square():
    grid = ZGrid(minibeam)
    expected = grid.z("O")
    grid.translate({".": 0, "O": 1, "#": 1})
    z = q19.locate_square(beam=grid.get, width=10, hi=grid.height)
    assert z == 25 + 20j == expected
Example #4
0
if __name__ == "__main__":
    print("populating 50x50 zgrid...")
    grid = ZGrid()
    x0 = 0
    for y in range(50):
        on = False
        for x in range(x0, 50):
            z = x + y * 1j
            val = grid[z] = beam(z)
            if not on and val:
                on = True
                x0 = x
                if x0:
                    m = y / x0
            if on and not val:
                break

    print("part a", sum(grid.values()))
    grid.translate({0: ".", 1: "#"})
    grid.draw()

    print("initial gradient is approx -->", m)
    print("refining gradient estimate -->", end=" ")
    z = left_edge_of_beam(2000, gradient=m)
    m = z.imag / z.real
    print(m)

    z = locate_square(beam, width=100, gradient_estimate=m)
    print("part b", int(z.real) * 10000 + int(z.imag))