如何对检测到的对象进行比较



我想用我的对象检测模型检测到的所有对象的.json文件,然后看看它们中的任何一个是否在彼此的x像素内,然后将它们分组在一起做出决定。我试着在检测到的对象上循环,但我似乎无法将它们单独进行比较。如有任何帮助,我将不胜感激。

Python代码:

input_file_path = 'C:\Yolo_v4\darknet\build\darknet\x64\result.json'
input_file = open(input_file_path, 'r')
results = json.load(input_file)
for data in results:                        # Loops over processed images
file_path = data['filename']
print("Processing image: " + file_path)
objects = data['objects']
for o in objects:                         # Loops over detected objects in image
class_id = o['class_id']
class_name = o['name']
coords = o['relative_coordinates']
xmid = coords['center_x']
ymid = coords['center_y']
width = coords['width']
height = coords['height']
confidence = o['confidence']

。Json文件(部分)

[
{
"frame_id":1, 
"filename":"C:\Yolo_v4\darknet\build\darknet\x64\f003.png", 
"objects": [ 
{"class_id":41, "name":"z", "relative_coordinates":{"center_x":0.082398, "center_y":0.647144, "width":0.135836, "height":0.048884}, "confidence":0.971427}, 
{"class_id":4, "name":"3", "relative_coordinates":{"center_x":0.225985, "center_y":0.466635, "width":0.097890, "height":0.050788}, "confidence":0.925511}, 
{"class_id":5, "name":"4", "relative_coordinates":{"center_x":0.226959, "center_y":0.422191, "width":0.093029, "height":0.043420}, "confidence":0.617335}, 
{"class_id":30, "name":"q", "relative_coordinates":{"center_x":0.173545, "center_y":0.115036, "width":0.115037, "height":0.058313}, "confidence":0.992148}, 
{"class_id":9, "name":"8", "relative_coordinates":{"center_x":0.252416, "center_y":0.226164, "width":0.092615, "height":0.042916}, "confidence":0.804773}, 
{"class_id":31, "name":"r", "relative_coordinates":{"center_x":0.160054, "center_y":0.168695, "width":0.116981, "height":0.035077}, "confidence":0.536827}, 
{"class_id":8, "name":"7", "relative_coordinates":{"center_x":0.232647, "center_y":0.272744, "width":0.142288, "height":0.049401}, "confidence":0.595207}, 
{"class_id":33, "name":"s", "relative_coordinates":{"center_x":0.152255, "center_y":0.222577, "width":0.085914, "height":0.043117}, "confidence":0.595850}, 
{"class_id":7, "name":"6", "relative_coordinates":{"center_x":0.235020, "center_y":0.323665, "width":0.117890, "height":0.042035}, "confidence":0.877499}, 
{"class_id":34, "name":"t", "relative_coordinates":{"center_x":0.140156, "center_y":0.264790, "width":0.080172, "height":0.040785}, "confidence":0.813210}, 
{"class_id":6, "name":"5", "relative_coordinates":{"center_x":0.226594, "center_y":0.370981, "width":0.146503, "height":0.047302}, "confidence":0.985635}, 
{"class_id":35, "name":"u", "relative_coordinates":{"center_x":0.129700, "center_y":0.317942, "width":0.098264, "height":0.050219}, "confidence":0.714703}, 
{"class_id":5, "name":"4", "relative_coordinates":{"center_x":0.225035, "center_y":0.465708, "width":0.122361, "height":0.039988}, "confidence":0.682546}, 
{"class_id":3, "name":"2", "relative_coordinates":{"center_x":0.224760, "center_y":0.517733, "width":0.136648, "height":0.049830}, "confidence":0.993929}, 
{"class_id":37, "name":"w", "relative_coordinates":{"center_x":0.102568, "center_y":0.442717, "width":0.129250, "height":0.070414}, "confidence":0.968482}, 
{"class_id":2, "name":"1", "relative_coordinates":{"center_x":0.229039, "center_y":0.612601, "width":0.126908, "height":0.026894}, "confidence":0.967374}, 
] 
}
]

只需使用2 for循环并按您的喜好实现compare(o1, o2):

for i in range(len(data["objects"])):
for j in range(len(data["objects"])):
if j<=i:
# Avoid comparing same elements again.
# Eg. no need to compare 3 and 1 since you already did 1 and 3
pass
compare(data["objects"][i], data["objects"][j])

相关内容

  • 没有找到相关文章

最新更新