【问题标题】:How Can I Compare Detected Objects with Each Other如何将检测到的对象相互比较
【发布时间】:2021-01-27 20:34:14
【问题描述】:

我想获取我的 .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}, 
 ] 
}
]

【问题讨论】:

  • 您是否尝试过嵌套的for 循环来比较所有对?如果您正在寻找更复杂的东西,您可能想查看k-means 或其他聚类算法之类的东西。 sklearn library 有一个实现。
  • @naicolas 如果可能,我不想计算所有可能的组合,我想指定“如果 x 的中心在 z 中心的 y 像素内”
  • 一开始你会如何选择“x”和“z”?我猜这些是您要比较的对象
  • X 和 Z 将是两个独立的对象

标签: python arrays json comparison grouping


【解决方案1】:

只需使用 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])

【讨论】:

  • 我想说“如果 x 的中心在 z 中心的 y 像素内”,但是比较方法不会只是吐出一个真值或假值吗?
  • 另外,i/j 是什么,什么值?
  • ij 是索引,从 0 开始到您的 data['objects'] 的长度返回 True/False 基本上是在说“如果 x 的中心在 z 中心的 y 像素内”。您说要将它们组合在一起,我实际上为此写了a small lib - 使用自定义比较将多个对象分组到多个组中,它可能会对您有所帮助。
  • 将检查该库,但是我收到来自 for i in range(len(data["objects"]): 的错误,说 invalid syntax
  • 缺少括号,我已经修复了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 2015-08-08
  • 1970-01-01
相关资源
最近更新 更多