下面是答案。我希望你知道在python中使用for循环。
通过计算机视觉程序和对象识别,我得到了这样的数组,它描述了识别对象周围的矩形:
[{
min: { x: 0.04963243, y: 0.48638576 },
max: { x: 0.31779325, y: 0.99719137 },
object_class: {
id: '1',
class_number: '1',
name: 'aaaa',
confidence: 0.97399896
}
},
{
min: { x: 0.048526138, y: 0 },
max: { x: 0.3132363, y: 0.4141748 },
object_class: {
id: '2',
class_number: '2',
name: 'bbbb'
confidence: 0.7052338
}
},
{
min: { x: 0.05446595, y: 0 },
max: { x: 0.31821355, y: 0.41463163 },
object_class: {
id: '1',
class_number: '1',
name: 'aaaa',
confidence: 0.702788
}
},
{
min: { x: 0.043725803, y: 0.4907713 },
max: { x: 0.31781, y: 0.9913551 },
object_class: {
id: '2',
class_number: '2',
name: 'bbbb',
confidence: 0.6097382
}
}]
我正在尝试获取min和max值以裁剪所拍摄照片的区域。所以我是python的新手,真的不知道该怎么做。我研究了列表理解,但没有找到实现它的方法。
当我尝试使用数组时,我也会得到错误NameError: name 'x' is not defined
解决这个问题的最佳方法是什么?
arr = [{
'min': {'x': 0.04963243, 'y': 0.48638576},
'max': {'x': 0.31779325, 'y': 0.99719137},
'object_class': {
'id': '1',
'class_number': '1',
'name': 'aaaa',
'confidence': 0.97399896
}
},
{
'min': {'x': 0.048526138, 'y': 0},
'max': {'x': 0.3132363, 'y': 0.4141748},
'object_class': {
'id': '2',
'class_number': '2',
'name': 'bbbb', #mistake here comma missing
'confidence': 0.7052338
}
},
{
'min': {'x': 0.05446595, 'y': 0},
'max': {'x': 0.31821355, 'y': 0.41463163},
'object_class': {
'id': '1',
'class_number': '1',
'name': 'aaaa',
'confidence': 0.702788
}
},
{
'min': {'x': 0.043725803, 'y': 0.4907713},
'max': {'x': 0.31781, 'y': 0.9913551},
'object_class': {
'id': '2',
'class_number': '2',
'name': 'bbbb',
'confidence': 0.6097382
}
}]
# loop through your array
for i in arr:
# min amount is obtained by i['min']
min_amount = i['min']
# max amount is obtained by i['max']
max_amount = i['max']
# check them out
print(min_amount)
print(max_amount)
编码快乐!