我正在尝试循环并选择我想要的两个class_id
值,然后将它们的center_x
和center_y
值一起比较。如果它们在一定范围内,我现在将其设置为0.10,它将在范围内打印。然而,当我现在运行我的代码并打印出它们之间的x_absolute_dif
和y_absolute_dif
时,它只是输出0.0
,这意味着它没有正确选择它们。如有任何帮助,我将不胜感激。
Python代码:
desired_id1 = 14
for thing in results:
for object1 in thing["objects"]:
if object1["class_id"] == desired_id1:
specific_class = object1
print("Correct Class")
break
for _class in results:
for object1 in _class["objects"]:
relative_coordinates = object1["relative_coordinates"]
center_x = relative_coordinates["center_x"]
center_y = relative_coordinates["center_y"]
# Do something with these values
desired_id2 = 15
for thing in results:
for object2 in thing["objects"]:
if object2["class_id"] == desired_id2:
specific_class = object2
print("Correct Class")
break
for _class in results:
for object2 in _class["objects"]:
relative_coordinates = object2["relative_coordinates"]
center_x = relative_coordinates["center_x"]
center_y = relative_coordinates["center_y"]
# Do something with these values
x_dif = object1["relative_coordinates"]["center_x"] - object2["relative_coordinates"]["center_x"]
x_absolute_dif = abs(x_dif)
print(x_absolute_dif)
if (x_absolute_dif <= 0.10):
print("X-Cords Within Range")
x_within_range = True
else:
print("X-Cords Not Within Range")
y_dif = object1["relative_coordinates"]["center_y"] - object2["relative_coordinates"]["center_y"]
y_absolute_dif = abs(y_dif)
print(y_absolute_dif)
if (y_absolute_dif <= 0.10):
print("Y-Cords Within Range")
y_within_range = True
else:
print("Y-Cords Not Within Range")
Json文件:
[
{
"frame_id": 1,
"filename": "C:\Yolo_v4\darknet\build\darknet\x64\f047.png",
"objects": [
{
"class_id": 14,
"name": "d",
"relative_coordinates": {
"center_x": 0.049905,
"center_y": 0.635935,
"width": 0.101077,
"height": 0.044067
},
"confidence": 0.966701
},
{
"class_id": 15,
"name": "e",
"relative_coordinates": {
"center_x": 0.045943,
"center_y": 0.685398,
"width": 0.109195,
"height": 0.041489
},
"confidence": 0.923188
},
]
}
]
我认为代码的问题是使用object1
和object2
,它们在循环中和循环外初始化,它们将在循环结束前具有最后一个元素的值。在你的例子中,循环
for _class in results:
for object1 in _class["objects"]:
和
for _class in results:
for object2 in _class["objects"]:
将使object1
和object2
等于_class["objects"]
中的最后一个元素,而不是您想要的状态(break后的那个)。
编辑:
desired_id1 = 14
obj1 = None
for thing in results:
for object1 in thing["objects"]:
if object1["class_id"] == desired_id1:
specific_class = object1
obj1 = object1
print("Correct Class")
break
for _class in results:
for object1 in _class["objects"]:
relative_coordinates = object1["relative_coordinates"]
center_x = relative_coordinates["center_x"]
center_y = relative_coordinates["center_y"]
# Do something with these values
desired_id2 = 15
obj2 = None
for thing in results:
for object2 in thing["objects"]:
if object2["class_id"] == desired_id2:
specific_class = object2
obj2 = object2
print("Correct Class")
break
for _class in results:
for object2 in _class["objects"]:
relative_coordinates = object2["relative_coordinates"]
center_x = relative_coordinates["center_x"]
center_y = relative_coordinates["center_y"]
# Do something with these values
x_dif = obj1["relative_coordinates"]["center_x"] - obj2["relative_coordinates"]["center_x"]
x_absolute_dif = abs(x_dif)
print(x_absolute_dif)
if (x_absolute_dif <= 0.10):
print("X-Cords Within Range")
x_within_range = True
else:
print("X-Cords Not Within Range")
y_dif = obj1["relative_coordinates"]["center_y"] - obj2["relative_coordinates"]["center_y"]
y_absolute_dif = abs(y_dif)
print(y_absolute_dif)
if (y_absolute_dif <= 0.10):
print("Y-Cords Within Range")
y_within_range = True
else:
print("Y-Cords Not Within Range")
重复的代码表明您应该编写一个函数并测试它是否返回正确的结果。你的循环继续运行,不会停止,直到找到坐标。
import json
json_string = r'''[
{
"frame_id": 1,
"filename": "C:\Yolo_v4\darknet\build\darknet\x64\f047.png",
"objects": [
{
"class_id": 14,
"name": "d",
"relative_coordinates": {
"center_x": 0.049905,
"center_y": 0.635935,
"width": 0.101077,
"height": 0.044067
},
"confidence": 0.966701
},
{
"class_id": 15,
"name": "e",
"relative_coordinates": {
"center_x": 0.045943,
"center_y": 0.685398,
"width": 0.109195,
"height": 0.041489
},
"confidence": 0.923188
}
]
}
]'''
results = json.loads(json_string)
class ValueNotFoundError(Exception): ...
def get_xy(results,desired_id):
for r in results:
for o in r['objects']:
if o['class_id'] == desired_id:
rc = o['relative_coordinates']
return rc['center_x'],rc['center_y'] # return results immediately
# if not found...
raise ValueNotFoundError
def check(label,a,b,tolerance):
if (d := abs(a - b)) <= tolerance:
print(f'{label}-Coords Within Range: {d:.2}')
return True
else:
print(f'{label}-Coords Not Within Range: {d:.2}')
return False
x1,y1 = get_xy(results,14)
x2,y2 = get_xy(results,15)
tol = .1
check('X',x1,x2,tol)
check('Y',y1,y2,tol)
X-Coords Within Range: 0.004
Y-Coords Within Range: 0.049