def test_add_ids_to_relationships(caplog): tdocument = t2.TDocument() page_block = t2.TBlock( id=str(uuid4()), block_type="PAGE", geometry=t2.TGeometry( bounding_box=t2.TBoundingBox(width=1, height=1, left=0, top=0), polygon=[t2.TPoint(x=0, y=0), t2.TPoint(x=1, y=1)]), ) tblock = t2.TBlock(id=str(uuid4()), block_type="WORD", text="sometest", geometry=t2.TGeometry( bounding_box=t2.TBoundingBox(width=0, height=0, left=0, top=0), polygon=[t2.TPoint(x=0, y=0), t2.TPoint(x=0, y=0)]), confidence=99, text_type="VIRTUAL") tdocument.add_block(page_block) tdocument.add_block(tblock) page_block.add_ids_to_relationships([tblock.id]) tblock.add_ids_to_relationships(["1", "2"]) assert page_block.relationships and len(page_block.relationships) > 0 assert tblock.relationships and len(tblock.relationships) > 0
def test_ratio(caplog): p1: t2.TPoint = t2.TPoint(x=0.5, y=0.5) p2: t2.TPoint = t2.TPoint(x=5, y=5) p2.ratio(doc_width=10, doc_height=10) assert (p1 == p2) b1: t2.TBoundingBox = t2.TBoundingBox(width=0.1, height=0.1, left=0.5, top=0.5) b2: t2.TBoundingBox = t2.TBoundingBox(width=1, height=1, left=5, top=5) b2.ratio(doc_width=10, doc_height=10) assert (b1 == b2) p1: t2.TPoint = t2.TPoint(x=0.5, y=0.5) p2: t2.TPoint = t2.TPoint(x=5, y=5) b1: t2.TBoundingBox = t2.TBoundingBox(width=0.1, height=0.1, left=0.5, top=0.5) b2: t2.TBoundingBox = t2.TBoundingBox(width=1, height=1, left=5, top=5) g1: t2.TGeometry = t2.TGeometry(bounding_box=b1, polygon=[p1]) g2: t2.TGeometry = t2.TGeometry(bounding_box=b2, polygon=[p2]) g2.ratio(doc_width=10, doc_height=10) assert (g1 == g2)
def test_rotate(): points = [] width = 0.05415758863091469 height = 0.011691284365952015 left = 0.13994796574115753 top = 0.8997916579246521 origin: t2.TPoint = t2.TPoint(x=0.5, y=0.5) degrees: float = 180 points.append( t2.TPoint(x=left, y=top).rotate(origin_x=origin.x, origin_y=origin.y, degrees=degrees)) points.append( t2.TPoint(x=left + width, y=top).rotate(origin_x=origin.x, origin_y=origin.y, degrees=degrees)) points.append( t2.TPoint(x=left, y=top + height).rotate(origin_x=origin.x, origin_y=origin.y, degrees=degrees)) points.append( t2.TPoint(x=left + width, y=top + height).rotate(origin_x=origin.x, origin_y=origin.y, degrees=degrees)) assert not None in points
def test_serialization(): """ testing that None values are removed when serializing """ bb_1 = t2.TBoundingBox( 0.4, 0.3, 0.1, top=None) # type:ignore forcing some None/null values bb_2 = t2.TBoundingBox(0.4, 0.3, 0.1, top=0.2) p1 = t2.TPoint(x=0.1, y=0.1) p2 = t2.TPoint(x=0.3, y=None) # type:ignore geo = t2.TGeometry(bounding_box=bb_1, polygon=[p1, p2]) geo_s = t2.TGeometrySchema() s: str = geo_s.dumps(geo) assert not "null" in s geo = t2.TGeometry(bounding_box=bb_2, polygon=[p1, p2]) s: str = geo_s.dumps(geo) assert not "null" in s
def test_scale(caplog): p1: t2.TPoint = t2.TPoint(x=0.5, y=0.5) p1.scale(doc_width=10, doc_height=10) assert (p1 == t2.TPoint(x=5, y=5)) b1: t2.TBoundingBox = t2.TBoundingBox(width=0.1, height=0.1, left=0.5, top=0.5) b1.scale(doc_width=10, doc_height=10) assert (b1 == t2.TBoundingBox(width=1, height=1, left=5, top=5)) p1: t2.TPoint = t2.TPoint(x=0.5, y=0.5) b1: t2.TBoundingBox = t2.TBoundingBox(width=0.1, height=0.1, left=0.5, top=0.5) g1: t2.TGeometry = t2.TGeometry(bounding_box=b1, polygon=[p1]) g1.scale(doc_width=10, doc_height=10) assert (g1 == t2.TGeometry(bounding_box=t2.TBoundingBox(width=1, height=1, left=5, top=5), polygon=[t2.TPoint(x=5, y=5)]))
def rotate_points_to_page_orientation( t_document: t2.TDocument) -> t2.TDocument: # TODO add rotation information to document (degree and center) logger.debug("rotate_points_to_page_orientation") for page in t_document.pages: logger.debug(page) if page.custom: logger.debug("page.custom") page_rotation = -page.custom['Orientation'] logger.debug(f"page_rotation: {page_rotation}") t_document.rotate(page=page, origin=t2.TPoint(0.5, 0.5), degrees=float(page_rotation)) page.custom['Rotation'] = { 'Degrees': page_rotation, 'RotationPointX': 0.5, 'RotationPointY': 0.5 } return t_document
def test_rotate_point(): assert t2.TPoint(2, 2) == t2.TPoint(2, 2) p = t2.TPoint(2, 2).rotate(degrees=180, origin_y=0, origin_x=0, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(-2, -2) p = t2.TPoint(3, 4).rotate(degrees=-30, origin_y=0, origin_x=0, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(5, 2) p = t2.TPoint(3, 4).rotate(degrees=-77, origin_y=0, origin_x=0, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(5, -2) p = t2.TPoint(3, 4).rotate(degrees=-90, origin_y=0, origin_x=0, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(4, -3) p = t2.TPoint(3, 4).rotate(degrees=-270, origin_y=0, origin_x=0, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(-4, 3) p = t2.TPoint(2, 2).rotate(degrees=180, origin_x=1, origin_y=1) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(0, 0) p = t2.TPoint(3, 4).rotate(degrees=-30, origin_y=0, origin_x=0, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(5, 2) p = t2.TPoint(3, 4).rotate(degrees=-77, origin_x=4, origin_y=4, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(4, 5) p = t2.TPoint(3, 4).rotate(degrees=-90, origin_x=4, origin_y=6, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(2, 7) p = t2.TPoint(3, 4).rotate(degrees=-270, origin_x=4, origin_y=6, force_limits=False) assert t2.TPoint(x=round(p.x), y=round(p.y)) == t2.TPoint(6, 5)