def init_path(): return HandwrittenPath(curves=[ Curve([Point(1, 1), Point(1, 1), Point(1, 1)]), Curve(start=Point(10, 10)), Curve([Point(1, 1)]) ])
def test_path_with_shift_to_bytes(self): self.path.set_position(Point(10, 10)) a = copy.deepcopy(self.path) bt = a.get_bytes() b = HandwrittenPath.read_next(io.BytesIO(bt)) self.assertEqual(a, b)
def test_empty_path_filled(self): path = HandwrittenPath() itr = path.get_lines() itr._new_curve(Point(50, 50)) itr.append_absolute(Point(55, 55)) itr.append_absolute(Point(60, 80)) itr.append_absolute(Point(65, 85)) lines = self.get_lines(itr) self.assertListEqual(lines, [ (Point(50, 50), Point(55, 55)), (Point(55, 55), Point(60, 80)), (Point(60, 80), Point(65, 85)) ])
def create_path_variant(self, path_name): """must be called only if group exists""" current_group = self.dictionary_manager.iterator.get_group_or_raise() current_group.append_path(HandwrittenPath(path_name)) self.dictionary_manager.iterator.select_variant(len(current_group) - 1) self._redraw_path_callback() self._update_menus_callback()
def init_path_plus_5(): return HandwrittenPath(curves=[ Curve([ Point(1, 0), Point(1, 0), Point(1, 0), Point(1, 0), Point(1, 0) ]) ])
def setUp(self) -> None: self.path = HandwrittenPath() self.path.new_curve(Point(0, 0)) self.path.append_shift(Point(1, 2)) self.path.append_shift(Point(1, 2)) self.path.new_curve(Point(10, 20)) self.path.new_curve(Point(10, 20)) self.path.append_shift(Point(1, 2)) self.path.append_shift(Point(1, 2)) self.path.get_last_point() self.path_iter = self.path.get_lines() self.expected_lines = [ (Point(0, 0), Point(1, 2)), (Point(1, 2), Point(2, 4)), (Point(22, 44), Point(23, 46)), (Point(23, 46), Point(24, 48)) ]
def load_paths_file(file_path, is_shift=False): paths = {} for path in HandwrittenPath.from_file(file_path): path = shift_path_rect(let, (0, 0)) if path.name[0] in paths: paths[path.name[0]].append(path) else: paths[path.name[0]] = [path] return paths
class TestPathShiftBox(TestCase): def setUp(self) -> None: self.path = HandwrittenPath() self.path.new_curve(Point(20, 20)) self.path.append_absolute(Point(25, 30)) # (+5, +10) self.path.append_absolute(Point(28, 28)) # (+3, -2) def test_get_path_box(self): box = PathShiftBox.get_lines_box(self.path.get_lines()) self.assertEqual(Box(min_x=20, max_x=28, min_y=20, max_y=30), box) def test_get_shifted_path_box(self): self.path.set_position(Point(0, 0)) box = PathShiftBox.get_lines_box(self.path.get_lines()) self.assertEqual(Box(min_x=0, max_x=8, min_y=0, max_y=10), box) def test_get_rectangle_shift(self): box = PathShiftBox.get_lines_box(self.path.get_lines()) shift = PathShiftBox.get_rectangle_shift(box, (50, 50)) self.assertEqual(Point(-20, 20), shift)
def transform_path(path, angle_rad=0, scale=(1, 1), inplace=True): new_points = list(path.shifts) r = np.matrix([[np.cos(angle_rad), np.sin(angle_rad)], [-np.sin(angle_rad), np.cos(angle_rad)]]) r = r.dot(np.matrix([[scale[0], 0], [0, scale[1]]])) r = r.round(5) for i in range(len(new_points)): if new_points[i] != path.break_point: lst = np.dot(r, path.shifts[i]).tolist() new_points[i] = (round(lst[0]), round(lst[1])) if inplace: path.set_points(new_points) return path else: return HandwrittenPath(path.name, new_points)
def test_path_to_bytes(self): a = copy.deepcopy(self.path) bt = a.get_bytes() b = HandwrittenPath.read_next(io.BytesIO(bt)) self.assertEqual(a, b)
def setUp(self): self.path = self.init_path() self.curve = self.path[0] self.empty_path = HandwrittenPath() self.abs_points = self.init_absolute_points()
class TestHandwrittenPath(unittest.TestCase): @staticmethod def init_path(): return HandwrittenPath(curves=[ Curve([Point(1, 1), Point(1, 1), Point(1, 1)]), Curve(start=Point(10, 10)), Curve([Point(1, 1)]) ]) @staticmethod def init_absolute_points(): return [ Point(*elem) for elem in [(15, 15), (20, 15), (30, 40), (80, 60)] ] def setUp(self): self.path = self.init_path() self.curve = self.path[0] self.empty_path = HandwrittenPath() self.abs_points = self.init_absolute_points() def test_create_empty_path(self): self.assertEqual(self.empty_path.get_last_point(), Point(0, 0)) self.assertTrue(self.empty_path.empty()) def test_set_path_position(self): self.path.set_position(Point(10, 10)) self.assertEqual(self.path.get_last_point(), Point(24, 24)) def test_append_shift_to_path(self): self.empty_path.append_shift(Point(1, 1)) self.assertEqual(self.empty_path.get_last_point(), Point(1, 1)) self.empty_path.append_shift(Point(10, 10)) self.assertEqual(self.empty_path.get_last_point(), Point(11, 11)) def test_curve_last_point(self): self.assertEqual(self.curve.get_last_point(), Point(3, 3)) self.assertEqual(self.curve.get_last_point(Point(10, 10)), Point(13, 13)) def test_path_to_bytes(self): a = copy.deepcopy(self.path) bt = a.get_bytes() b = HandwrittenPath.read_next(io.BytesIO(bt)) self.assertEqual(a, b) def test_path_with_shift_to_bytes(self): self.path.set_position(Point(10, 10)) a = copy.deepcopy(self.path) bt = a.get_bytes() b = HandwrittenPath.read_next(io.BytesIO(bt)) self.assertEqual(a, b) def test_curve_from_absolute_points(self): curve = Curve.from_absolute(list(self.abs_points)) curve_iter = CurvePointsIterator(curve, curve.start) abs_points_iter = iter(self.abs_points) while True: try: self.assertEqual(next(curve_iter), next(abs_points_iter)) except StopIteration: break
def get_border_path(self): path = HandwrittenPath() cur_point = self.box_position.copy() path.append_absolute(cur_point) cur_point.x += self.box_size[0] # go left path.append_absolute(cur_point.copy()) cur_point.y += self.box_size[1] # go down path.append_absolute(cur_point.copy()) cur_point.x -= self.box_size[0] # go right path.append_absolute(cur_point.copy()) cur_point.y -= self.box_size[1] # go up path.append_absolute(cur_point.copy()) return path
def init_test_path_group(name): curve = Curve([Point(0, 0), Point(2, 2), Point(1, -1), Point(1, 0)]) path = HandwrittenPath(curves=[curve]) path_group = PathGroup(name, [path]) return path_group
class TestPathLinesIterator(TestCase): def setUp(self) -> None: self.path = HandwrittenPath() self.path.new_curve(Point(0, 0)) self.path.append_shift(Point(1, 2)) self.path.append_shift(Point(1, 2)) self.path.new_curve(Point(10, 20)) self.path.new_curve(Point(10, 20)) self.path.append_shift(Point(1, 2)) self.path.append_shift(Point(1, 2)) self.path.get_last_point() self.path_iter = self.path.get_lines() self.expected_lines = [ (Point(0, 0), Point(1, 2)), (Point(1, 2), Point(2, 4)), (Point(22, 44), Point(23, 46)), (Point(23, 46), Point(24, 48)) ] @staticmethod def get_lines(path_iter): ls = [] try: while True: ls.append(next(path_iter)) except StopIteration: return ls def test_path_correct(self): self.assertListEqual(self.get_lines(self.path_iter), self.expected_lines) def test_create_new_curve_from_iterator(self): self.get_lines(self.path_iter) self.path_iter._new_curve(Point(50, 50)) self.path_iter.append_absolute(Point(55, 55)) self.path_iter.append_absolute(Point(60, 80)) self.path_iter.append_absolute(Point(65, 85)) lines = self.get_lines(self.path_iter) self.assertListEqual(lines, [ (Point(50, 50), Point(55, 55)), (Point(55, 55), Point(60, 80)), (Point(60, 80), Point(65, 85)) ]) def test_empty_path_filled(self): path = HandwrittenPath() itr = path.get_lines() itr._new_curve(Point(50, 50)) itr.append_absolute(Point(55, 55)) itr.append_absolute(Point(60, 80)) itr.append_absolute(Point(65, 85)) lines = self.get_lines(itr) self.assertListEqual(lines, [ (Point(50, 50), Point(55, 55)), (Point(55, 55), Point(60, 80)), (Point(60, 80), Point(65, 85)) ])
def setUp(self) -> None: self.path = HandwrittenPath() self.path.new_curve(Point(20, 20)) self.path.append_absolute(Point(25, 30)) # (+5, +10) self.path.append_absolute(Point(28, 28)) # (+3, -2)
import sys sys.path.append(r"D:\coding\Python_codes\Handwriting_extractor_project") from handwriting.path.curve import Curve from handwriting.path.handwritten_path import HandwrittenPath from handwriting.path.curve.point import Point test_path = HandwrittenPath('hello', [ Curve( [Point(1, 10), Point(1, 10), Point(1, 10), Point(1, 10)]), Curve([Point(10, 100), Point(10, 100)]) ])
while i < len(path.components): if len(path.components[i]) == 0: path.components.pop(i) else: i += 1 gap_point = Point(65535, 65535) for file in input_files: with file.open('rb') as fin: letters_dict = pickle.load(fin) new_path_group = PathGroup(file.name[:file.name.index('.')]) for letter, points in letters_dict.items(): new_path = HandwrittenPath(letter) point = Point(0, 0) prev_point = Point(0, 0) is_first_point = True i = 0 while i < len(points): prev_point = point point = Point(*points[i]) i += 1 if point == gap_point: is_first_point = True # take next point if i < len(points): point = Point(*points[i])