简体   繁体   English

如何将检测到的对象相互比较

[英]How Can I Compare Detected Objects with Each Other

I want to take my.json file with all of the objects that my object detection model detected and then see if any of them are within x pixels of each other, and then group them together to make decisions.我想将 my.json 文件与我的 object 检测到的所有对象 model 检测到,然后查看它们中的任何一个是否在 x 像素范围内,然后将它们组合在一起以做出决定。 I have tried just looping over the detected objects but I can't seem to compare them with each other individually.我试过循环检测到的对象,但我似乎无法单独将它们相互比较。 Any help would be appreaciated.任何帮助将不胜感激。

Python Code: 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 File(Some of it) .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}, 
 ] 
}
]

Just use 2 for loops and implement compare(o1, o2) as you like:只需使用 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 Python 中的对象相互通信? - How can I make Objects communicate with each other in Python? 如何创建两个互相监视的对象? - How can I create two objects that monitor each other? 如何声明 3 个相互作为属性的对象 - How can I declare 3 objects that have each other as an attribute 如何比较每个对象? - How can I compare each object with each? 如何比较两个熊猫数据框并返回将它们相互映射的索引? - How can I compare two pandas dataframes and return an index mapping them to each other? 我如何在两个不同的熊猫数据框中相互比较值 - How can I compare values to each other in two different pandas dataframes 使用python矩阵M(使用numpy),如何将该矩阵的每一列与所有其他列进行比较? - With a python matrix M (using numpy), how can I compare each column of that matrix with all other columns? 如何将元组列表的字典相互比较? - How can you compare a dictionary of list of tuples to each other? 如何检查 Django 中来自不同模型的两个对象是否彼此相等? - How can I check if two objects from differente models equal each other in Django? 如何在不将它们相互合并的情况下检测此图像中具有白色背景的所有对象? - How can I detect all objects in this image with a white background without merging them with each other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM