Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

python - 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. 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:

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)

[
{
 "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}, 
 ] 
}
]
question from:https://stackoverflow.com/questions/65926964/how-can-i-compare-detected-objects-with-each-other

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use 2 for loops and implement compare(o1, o2) as you like:

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])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...