我有一个json文件,该文件是来自REST端点的响应,该端点检测Image中的对象。在我的文件中,我需要获取边界框并为Planogram建立货架数据。
目前,我只能过滤我得到的输出。这是我的代码。
let response = [
{
"boundingBox": {
"maxX": 660,
"maxY": 971,
"minX": 582,
"minY": 735
},
"label": "other",
"probability": 0.698079
},
{
"boundingBox": {
"maxX": 407,
"maxY": 969,
"minX": 326,
"minY": 723
},
"label": "other",
"probability": 0.6874929
},
{
"boundingBox": {
"maxX": 569,
"maxY": 963,
"minX": 489,
"minY": 728
},
"label": "other",
"probability": 0.6783268
},
{
"boundingBox": {
"maxX": 476,
"maxY": 960,
"minX": 395,
"minY": 725
},
"label": "other",
"probability": 0.6762803
},
{
"boundingBox": {
"maxX": 507,
"maxY": 643,
"minX": 442,
"minY": 405
},
"label": "soyaSauce",
"probability": 0.9600686
},
{
"boundingBox": {
"maxX": 574,
"maxY": 647,
"minX": 479,
"minY": 428
},
"label": "soyaSauce",
"probability": 0.9461142
},
{
"boundingBox": {
"maxX": 466,
"maxY": 662,
"minX": 363,
"minY": 421
},
"label": "soyaSauce",
"probability": 0.94173825
},
{
"boundingBox": {
"maxX": 679,
"maxY": 647,
"minX": 578,
"minY": 432
},
"label": "soyaSauce",
"probability": 0.9386765
},
{
"boundingBox": {
"maxX": 874,
"maxY": 656,
"minX": 810,
"minY": 401
},
"label": "soyaSauce",
"probability": 0.93829554
},
{
"boundingBox": {
"maxX": 743,
"maxY": 661,
"minX": 642,
"minY": 430
},
"label": "soyaSauce",
"probability": 0.9326571
},
{
"boundingBox": {
"maxX": 393,
"maxY": 664,
"minX": 292,
"minY": 427
},
"label": "soyaSauce",
"probability": 0.9322365
},
{
"boundingBox": {
"maxX": 805,
"maxY": 662,
"minX": 743,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.9173948
},
{
"boundingBox": {
"maxX": 332,
"maxY": 661,
"minX": 226,
"minY": 425
},
"label": "soyaSauce",
"probability": 0.9157952
},
{
"boundingBox": {
"maxX": 703,
"maxY": 298,
"minX": 603,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.90590423
},
{
"boundingBox": {
"maxX": 737,
"maxY": 306,
"minX": 669,
"minY": 44
},
"label": "soyaSauce",
"probability": 0.9048507
},
{
"boundingBox": {
"maxX": 251,
"maxY": 319,
"minX": 182,
"minY": 28
},
"label": "soyaSauce",
"probability": 0.90457577
},
{
"boundingBox": {
"maxX": 822,
"maxY": 299,
"minX": 719,
"minY": 35
},
"label": "soyaSauce",
"probability": 0.903023
},
{
"boundingBox": {
"maxX": 609,
"maxY": 665,
"minX": 539,
"minY": 411
},
"label": "soyaSauce",
"probability": 0.8928388
},
{
"boundingBox": {
"maxX": 943,
"maxY": 298,
"minX": 875,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.88029706
},
{
"boundingBox": {
"maxX": 636,
"maxY": 301,
"minX": 534,
"minY": 51
},
"label": "soyaSauce",
"probability": 0.8651045
},
{
"boundingBox": {
"maxX": 351,
"maxY": 296,
"minX": 252,
"minY": 24
},
"label": "soyaSauce",
"probability": 0.8629094
},
{
"boundingBox": {
"maxX": 874,
"maxY": 290,
"minX": 777,
"minY": 18
},
"label": "soyaSauce",
"probability": 0.8621941
},
{
"boundingBox": {
"maxX": 443,
"maxY": 291,
"minX": 343,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.8542643
},
{
"boundingBox": {
"maxX": 562,
"maxY": 302,
"minX": 463,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.85307074
},
{
"boundingBox": {
"maxX": 948,
"maxY": 653,
"minX": 852,
"minY": 391
},
"label": "soyaSauce",
"probability": 0.8488111
},
{
"boundingBox": {
"maxX": 772,
"maxY": 663,
"minX": 702,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.82573885
},
{
"boundingBox": {
"maxX": 270,
"maxY": 646,
"minX": 176,
"minY": 416
},
"label": "soyaSauce",
"probability": 0.77004087
},
{
"boundingBox": {
"maxX": 504,
"maxY": 288,
"minX": 409,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.76433444
},
{
"boundingBox": {
"maxX": 903,
"maxY": 274,
"minX": 847,
"minY": 30
},
"label": "soyaSauce",
"probability": 0.51238227
}
];
var data_filter = response.filter( element => element.label = 'soyaSauce')
console.log(data_filter);
当前,当我运行此代码时,我能够获得包含labels
的所有soyaSauce
(我只是为了测试而写的),但是我的实际用例是找到< strong> minX 并返回该节点作为响应。
例如,如果minX的最小值是326。我要打印
{
"boundingBox": {
"maxX": 407,
"maxY": 969,
"minX": 326,
"minY": 723
},
"label": "other",
"probability": 0.6874929
}
在输出中。我可以使用以下代码获得最小值。
let values = response.map(function (v) {
return v.boundingBox.minX;
});
var min = Math.min.apply(null, values);
console.log(min);
但是我想在输出中获取相应的json对象。
我试图将这两种逻辑结合起来并编写如下逻辑。
var data_filter = response.filter(element => element.boundingBox.minX = min)
console.log(data_filter);
不幸的是,我得到的输出如下。而不是返回单个json对象:-(
176
[ { boundingBox: { maxX: 660, maxY: 971, minX: 176, minY: 735 },
label: 'other',
probability: 0.698079 },
{ boundingBox: { maxX: 407, maxY: 969, minX: 176, minY: 723 },
label: 'other',
probability: 0.6874929 },
{ boundingBox: { maxX: 569, maxY: 963, minX: 176, minY: 728 },
label: 'other',
probability: 0.6783268 },
{ boundingBox: { maxX: 476, maxY: 960, minX: 176, minY: 725 },
label: 'other',
probability: 0.6762803 },
{ boundingBox: { maxX: 507, maxY: 643, minX: 176, minY: 405 },
label: 'soyaSauce',
probability: 0.9600686 },
{ boundingBox: { maxX: 574, maxY: 647, minX: 176, minY: 428 },
label: 'soyaSauce',
probability: 0.9461142 },
{ boundingBox: { maxX: 466, maxY: 662, minX: 176, minY: 421 },
label: 'soyaSauce',
probability: 0.94173825 },
{ boundingBox: { maxX: 679, maxY: 647, minX: 176, minY: 432 },
label: 'soyaSauce',
probability: 0.9386765 },
{ boundingBox: { maxX: 874, maxY: 656, minX: 176, minY: 401 },
label: 'soyaSauce',
probability: 0.93829554 },
{ boundingBox: { maxX: 743, maxY: 661, minX: 176, minY: 430 },
label: 'soyaSauce',
probability: 0.9326571 },
{ boundingBox: { maxX: 393, maxY: 664, minX: 176, minY: 427 },
label: 'soyaSauce',
probability: 0.9322365 },
{ boundingBox: { maxX: 805, maxY: 662, minX: 176, minY: 410 },
label: 'soyaSauce',
probability: 0.9173948 },
{ boundingBox: { maxX: 332, maxY: 661, minX: 176, minY: 425 },
label: 'soyaSauce',
probability: 0.9157952 },
{ boundingBox: { maxX: 703, maxY: 298, minX: 176, minY: 46 },
label: 'soyaSauce',
probability: 0.90590423 },
{ boundingBox: { maxX: 737, maxY: 306, minX: 176, minY: 44 },
label: 'soyaSauce',
probability: 0.9048507 },
{ boundingBox: { maxX: 251, maxY: 319, minX: 176, minY: 28 },
label: 'soyaSauce',
probability: 0.90457577 },
{ boundingBox: { maxX: 822, maxY: 299, minX: 176, minY: 35 },
label: 'soyaSauce',
probability: 0.903023 },
{ boundingBox: { maxX: 609, maxY: 665, minX: 176, minY: 411 },
label: 'soyaSauce',
probability: 0.8928388 },
{ boundingBox: { maxX: 943, maxY: 298, minX: 176, minY: 29 },
label: 'soyaSauce',
probability: 0.88029706 },
{ boundingBox: { maxX: 636, maxY: 301, minX: 176, minY: 51 },
label: 'soyaSauce',
probability: 0.8651045 },
{ boundingBox: { maxX: 351, maxY: 296, minX: 176, minY: 24 },
label: 'soyaSauce',
probability: 0.8629094 },
{ boundingBox: { maxX: 874, maxY: 290, minX: 176, minY: 18 },
label: 'soyaSauce',
probability: 0.8621941 },
{ boundingBox: { maxX: 443, maxY: 291, minX: 176, minY: 29 },
label: 'soyaSauce',
probability: 0.8542643 },
{ boundingBox: { maxX: 562, maxY: 302, minX: 176, minY: 46 },
label: 'soyaSauce',
probability: 0.85307074 },
{ boundingBox: { maxX: 948, maxY: 653, minX: 176, minY: 391 },
label: 'soyaSauce',
probability: 0.8488111 },
{ boundingBox: { maxX: 772, maxY: 663, minX: 176, minY: 410 },
label: 'soyaSauce',
probability: 0.82573885 },
{ boundingBox: { maxX: 270, maxY: 646, minX: 176, minY: 416 },
label: 'soyaSauce',
probability: 0.77004087 },
{ boundingBox: { maxX: 504, maxY: 288, minX: 176, minY: 29 },
label: 'soyaSauce',
probability: 0.76433444 },
{ boundingBox: { maxX: 903, maxY: 274, minX: 176, minY: 30 },
label: 'soyaSauce',
probability: 0.51238227 } ]
请让我知道我要去哪里哪里以及如何解决这个问题。
谢谢
答案 0 :(得分:1)
您可能想比较filter
回调中的值,而不是分配它
response.filter(element => element.boundingBox.minX === min)
在您的情况下,将是这样。注意过滤器回调
let response = [
{
"boundingBox": {
"maxX": 660,
"maxY": 971,
"minX": 582,
"minY": 735
},
"label": "other",
"probability": 0.698079
},
{
"boundingBox": {
"maxX": 407,
"maxY": 969,
"minX": 326,
"minY": 723
},
"label": "other",
"probability": 0.6874929
},
{
"boundingBox": {
"maxX": 569,
"maxY": 963,
"minX": 489,
"minY": 728
},
"label": "other",
"probability": 0.6783268
},
{
"boundingBox": {
"maxX": 476,
"maxY": 960,
"minX": 395,
"minY": 725
},
"label": "other",
"probability": 0.6762803
},
{
"boundingBox": {
"maxX": 507,
"maxY": 643,
"minX": 442,
"minY": 405
},
"label": "soyaSauce",
"probability": 0.9600686
},
{
"boundingBox": {
"maxX": 574,
"maxY": 647,
"minX": 479,
"minY": 428
},
"label": "soyaSauce",
"probability": 0.9461142
},
{
"boundingBox": {
"maxX": 466,
"maxY": 662,
"minX": 363,
"minY": 421
},
"label": "soyaSauce",
"probability": 0.94173825
},
{
"boundingBox": {
"maxX": 679,
"maxY": 647,
"minX": 578,
"minY": 432
},
"label": "soyaSauce",
"probability": 0.9386765
},
{
"boundingBox": {
"maxX": 874,
"maxY": 656,
"minX": 810,
"minY": 401
},
"label": "soyaSauce",
"probability": 0.93829554
},
{
"boundingBox": {
"maxX": 743,
"maxY": 661,
"minX": 642,
"minY": 430
},
"label": "soyaSauce",
"probability": 0.9326571
},
{
"boundingBox": {
"maxX": 393,
"maxY": 664,
"minX": 292,
"minY": 427
},
"label": "soyaSauce",
"probability": 0.9322365
},
{
"boundingBox": {
"maxX": 805,
"maxY": 662,
"minX": 743,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.9173948
},
{
"boundingBox": {
"maxX": 332,
"maxY": 661,
"minX": 226,
"minY": 425
},
"label": "soyaSauce",
"probability": 0.9157952
},
{
"boundingBox": {
"maxX": 703,
"maxY": 298,
"minX": 603,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.90590423
},
{
"boundingBox": {
"maxX": 737,
"maxY": 306,
"minX": 669,
"minY": 44
},
"label": "soyaSauce",
"probability": 0.9048507
},
{
"boundingBox": {
"maxX": 251,
"maxY": 319,
"minX": 182,
"minY": 28
},
"label": "soyaSauce",
"probability": 0.90457577
},
{
"boundingBox": {
"maxX": 822,
"maxY": 299,
"minX": 719,
"minY": 35
},
"label": "soyaSauce",
"probability": 0.903023
},
{
"boundingBox": {
"maxX": 609,
"maxY": 665,
"minX": 539,
"minY": 411
},
"label": "soyaSauce",
"probability": 0.8928388
},
{
"boundingBox": {
"maxX": 943,
"maxY": 298,
"minX": 875,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.88029706
},
{
"boundingBox": {
"maxX": 636,
"maxY": 301,
"minX": 534,
"minY": 51
},
"label": "soyaSauce",
"probability": 0.8651045
},
{
"boundingBox": {
"maxX": 351,
"maxY": 296,
"minX": 252,
"minY": 24
},
"label": "soyaSauce",
"probability": 0.8629094
},
{
"boundingBox": {
"maxX": 874,
"maxY": 290,
"minX": 777,
"minY": 18
},
"label": "soyaSauce",
"probability": 0.8621941
},
{
"boundingBox": {
"maxX": 443,
"maxY": 291,
"minX": 343,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.8542643
},
{
"boundingBox": {
"maxX": 562,
"maxY": 302,
"minX": 463,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.85307074
},
{
"boundingBox": {
"maxX": 948,
"maxY": 653,
"minX": 852,
"minY": 391
},
"label": "soyaSauce",
"probability": 0.8488111
},
{
"boundingBox": {
"maxX": 772,
"maxY": 663,
"minX": 702,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.82573885
},
{
"boundingBox": {
"maxX": 270,
"maxY": 646,
"minX": 176,
"minY": 416
},
"label": "soyaSauce",
"probability": 0.77004087
},
{
"boundingBox": {
"maxX": 504,
"maxY": 288,
"minX": 409,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.76433444
},
{
"boundingBox": {
"maxX": 903,
"maxY": 274,
"minX": 847,
"minY": 30
},
"label": "soyaSauce",
"probability": 0.51238227
}
];
var data_filter = response.filter( element => element.label === 'soyaSauce')
console.log(data_filter);