使用trp模块无法提取Amazon Textract中Polygon的实际坐标值?
问题描述
我有一份Amazon Textract响应,想要从中提取Polygon的实际坐标值。我的代码如下:
doc = Document(response) polygons= [] # Iterate over elements in the document for page in doc.pages: for field in page.form.fields: polygons.append(field.geometry.polygon)
执行后得到的输出如下:
[[<trp.Polygon at 0x22671e9fb80>, <trp.Polygon at 0x22671e9fd90>, <trp.Polygon at 0x22671e9fe50>, <trp.Polygon at 0x22671e9feb0>], [<trp.Polygon at 0x22671e9bd30>, <trp.Polygon at 0x22671e9bfa0>, <trp.Polygon at 0x22671e9bac0>, <trp.Polygon at 0x22671e9bb20>], [<trp.Polygon at 0x22672e017f0>, <trp.Polygon at 0x22672e018b0>, <trp.Polygon at 0x22672e01700>, <trp.Polygon at 0x22672e01910>], ... <trp.Polygon at 0x22672e02070>], [<trp.Polygon at 0x22672dfa1c0>, <trp.Polygon at 0x22672dfa8e0>, <trp.Polygon at 0x22672dfa9a0>, <trp.Polygon at 0x22672dfa7f0>]]
我尝试使用field.geometry.polygon.text但没有效果,请问如何提取Polygon的实际坐标值?
解决方法
你看到的<trp.Polygon>是trp库封装的多边形对象,它的坐标点存储在points属性里,每个点对象包含x和y两个数值属性。修改代码即可提取坐标:
doc = Document(response) polygon_coords = [] # 遍历文档元素提取坐标 for page in doc.pages: for field in page.form.fields: # 获取当前字段的多边形对象 polygon = field.geometry.polygon # 提取每个点的x、y坐标,组成列表 coords = [(point.x, point.y) for point in polygon.points] polygon_coords.append(coords)
执行后,polygon_coords里会存储每个多边形的坐标点列表,格式类似:
[[(x1, y1), (x2, y2), (x3, y3), (x4, y4)], [(x1, y1), ...], ...]
如果需要单独提取横坐标或纵坐标,直接访问对应属性即可,比如point.x获取横坐标,point.y获取纵坐标。
内容的提问来源于stack exchange,提问作者InterestingPenguin80
相关产品推荐
相关产品推荐

