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
353 views
in Technique[技术] by (71.8m points)

python - Can't Subtract Two Individual Dictionary Values

I am trying to subtract two float values from a .json file, but I keeping getting the an error. All of the other threads/documentation are more focused on subtracting from two separate dictionaries so I haven't found anything that works. Any help would be appreaciated.

traceback (most recent call last):
  File "c:/Yolo_v4/darknet/build/darknet/x64/vision.py", line 46, in <module>
    if(data["objects"][4] - data["objects"][12]) < 40:
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'

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


    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
            if(data["objects"][4] - data["objects"][12]) < 40:
                print("Within Range")

.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}, 
] 
}
]

Edit: Added full error file and more of the python code

question from:https://stackoverflow.com/questions/65929457/cant-subtract-two-individual-dictionary-values

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

1 Reply

0 votes
by (71.8m points)

Sample json:

data=[
{
 "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}, 
] 
}
]

You are having a list of dict and some nested dicts

data[0]['objects'][0]['class_id'] - data[0]['objects'][1]['class_id']

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

...