gpt4 book ai didi

python - Elasticsearch Shape 查询以从嵌套 JSON 中查找边界多边形和背景颜色(Google OCR 响应)

转载 作者:行者123 更新时间:2023-12-02 22:12:12 24 4
gpt4 key购买 nike

我有一个来自 OCR 应用程序的响应,它是一个类似于 Google Vision OCR 响应的 JSON。我想把它放在 Elasticsearch 中以执行形状查询。虽然我可以将 JSON 放入 Elasticsearch,但我无法想出一个完美的 schemamapping 来根据形状或boundingPolybgColor

我是 Elasticsearch 的新手,几乎没有什么问题。

(1) 如何根据 boundingPolybgColor 执行搜索?

(2) 我是否需要更改执行搜索的模式,还是可以保持原样?适合我的目的的最佳架构和映射是什么?

(3) 此外,我想根据 bgColor 执行搜索。我怎样才能做到这一点?

I tried with Geo-shape query but failed to implement it with the proper result. Also, there is a restriction in Geo-shape query that the values must lie between 90 - 180. I think that part we can handle by normalizing the values.

示例 JSON:

{
"responses": [{
"textAnnotations": [{
"description": "were",
"boundingPoly": {
"vertices": [{
"x": 112,
"y": 5
},
{
"x": 333,
"y": 5
},
{
"x": 333,
"y": 93
},
{
"x": 112,
"y": 93
}
],
"confidence": 99
},
"wordInfo": {
"length": 4,
"width": 221,
"height": 88,
"bgColor": [
255,
255,
251
]
}
},
{
"description": "gonna",
"boundingPoly": {
"vertices": [{
"x": 338,
"y": 5
},
{
"x": 589,
"y": 5
},
{
"x": 589,
"y": 93
},
{
"x": 338,
"y": 93
}
],
"confidence": 99
},
"wordInfo": {
"length": 5,
"width": 251,
"height": 88,
"bgColor": [
255,
255,
255
]
}
}
]
}]
}

提前致谢!

最佳答案

当您在图像上使用单词的边界框坐标时,我建议您使用 shape数据类型,因为没有坐标值的限制。

"vertices": {
"type": "shape"
}

还要确保以下面的格式操作边界框坐标。

[[x1,y1],[x2,y2],[x3,y3],[x4,y4],[x1,y1]]

您可能想要更改 elasticsearch 中的架构,因为它可以更轻松地搜索文档中的不同字段。

将数据发布到文档中,例如:

POST /example/_doc
{
"vertices" : {
"type" : "polygon",
"coordinates" : [
[ [1000.0, -1001.0], [1001.0, -1001.0], [1001.0, -1000.0], [1000.0, -1000.0], [1000.0, -1001.0] ]
]
}
}

对于搜索,您可以使用 envelope键入查询,这样您就不必写出边界框的所有坐标,您可以设置要在其中搜索的信封(矩形),它将为您提供信封中包含的所有文档。注意:用于信封类型搜索的坐标输入有点不同,它采用[[minX,maxY],[maxX,minY]]这种格式。

例子:

{
"query":{
"bool": {
"must": {
"match_all": {}
},
"filter": {
"shape": {
"prefix.vertices": {
"shape": {
"type": "envelope",
"coordinates": [
[40,60],
[100,40]
]
}
}
}
}
}
}
}

由于 elasticsearch 中的每个字段都可以有单个或多个(以数组的形式)值,因此可以使用 must-match 查询来搜索 bgColor 以将所有值匹配到 bgColor 元素。

例子:

{
"query": {
"bool": {
"must": [
{"match": {"prefix.wordInfo.bgColor": 1}},
{"match": {"prefix.wordInfo.bgColor": 2}},
{"match": {"prefix.wordInfo.bgColor": 3}}
]
}
}
}

希望对您有所帮助。

关于python - Elasticsearch Shape 查询以从嵌套 JSON 中查找边界多边形和背景颜色(Google OCR 响应),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61038131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com