【问题标题】:Python Code Not Selecting Correct DictionaryPython代码没有选择正确的字典
【发布时间】:2021-05-18 16:49:58
【问题描述】:

我正在尝试循环并选择我想要的两个 class_id 值,然后将它们的 center_xcenter_y 值一起比较。如果它们在某个范围内,我现在设置为 0.10,它将在范围内打印。但是,当我现在运行我的代码并打印出它们之间的x_absolute_dify_absolute_dif 时,它只会输出0.0,这意味着它没有正确选择它们。任何帮助将不胜感激。

Python 代码:

    desired_id1 = 14
    for thing in results:
        for object1 in thing["objects"]:
            if object1["class_id"] == desired_id1:
                specific_class = object1
                print("Correct Class")
                break

    for _class in results:
        for object1 in _class["objects"]:
            relative_coordinates = object1["relative_coordinates"]
            center_x = relative_coordinates["center_x"]
            center_y = relative_coordinates["center_y"]
            # Do something with these values

    desired_id2 = 15
    for thing in results:
        for object2 in thing["objects"]:
            if object2["class_id"] == desired_id2:
                specific_class = object2
                print("Correct Class")
                break

    for _class in results:
        for object2 in _class["objects"]:
            relative_coordinates = object2["relative_coordinates"]
            center_x = relative_coordinates["center_x"]
            center_y = relative_coordinates["center_y"]
            # Do something with these values

    x_dif = object1["relative_coordinates"]["center_x"] - object2["relative_coordinates"]["center_x"]
    x_absolute_dif = abs(x_dif)
    print(x_absolute_dif)

    if (x_absolute_dif <= 0.10):
        print("X-Cords Within Range") 
        x_within_range = True
    else:
        print("X-Cords Not Within Range")

    y_dif = object1["relative_coordinates"]["center_y"] - object2["relative_coordinates"]["center_y"]
    y_absolute_dif = abs(y_dif)
    print(y_absolute_dif)

    if (y_absolute_dif <= 0.10):
        print("Y-Cords Within Range")
        y_within_range = True
    else:
        print("Y-Cords Not Within Range")

Json 文件:

[
    {
        "frame_id": 1,
        "filename": "C:\\Yolo_v4\\darknet\\build\\darknet\\x64\\f047.png",
        "objects": [
               {
                "class_id": 14,
                "name": "d",
                "relative_coordinates": {
                    "center_x": 0.049905,
                    "center_y": 0.635935,
                    "width": 0.101077,
                    "height": 0.044067
                },
                "confidence": 0.966701
            },
            {
                "class_id": 15,
                "name": "e",
                "relative_coordinates": {
                    "center_x": 0.045943,
                    "center_y": 0.685398,
                    "width": 0.109195,
                    "height": 0.041489
                },
                "confidence": 0.923188
            },
            ]
    }
]

【问题讨论】:

    标签: python json function loops dictionary


    【解决方案1】:

    我认为代码的问题在于使用object1object2,它们在循环中和循环外初始化,它们将具有循环结束前最后一个元素的值。在你的情况下循环

    for _class in results:
        for object1 in _class["objects"]:
    

    for _class in results:
        for object2 in _class["objects"]:
    

    将使object1object2 等于_class["objects"] 中的最后一个元素,而不是您想要的状态(中断后的状态)。

    编辑:

    desired_id1 = 14
    obj1 = None
    for thing in results:
        for object1 in thing["objects"]:
            if object1["class_id"] == desired_id1:
                specific_class = object1
                obj1 = object1
                print("Correct Class")
                break
    
    for _class in results:
        for object1 in _class["objects"]:
            relative_coordinates = object1["relative_coordinates"]
            center_x = relative_coordinates["center_x"]
            center_y = relative_coordinates["center_y"]
            # Do something with these values
    
    desired_id2 = 15
    obj2 = None
    for thing in results:
        for object2 in thing["objects"]:
            if object2["class_id"] == desired_id2:
                specific_class = object2
                obj2 = object2
                print("Correct Class")
                break
    
    for _class in results:
        for object2 in _class["objects"]:
            relative_coordinates = object2["relative_coordinates"]
            center_x = relative_coordinates["center_x"]
            center_y = relative_coordinates["center_y"]
            # Do something with these values
    
    x_dif = obj1["relative_coordinates"]["center_x"] - obj2["relative_coordinates"]["center_x"]
    x_absolute_dif = abs(x_dif)
    print(x_absolute_dif)
    
    if (x_absolute_dif <= 0.10):
        print("X-Cords Within Range") 
        x_within_range = True
    else:
        print("X-Cords Not Within Range")
    
    y_dif = obj1["relative_coordinates"]["center_y"] - obj2["relative_coordinates"]["center_y"]
    y_absolute_dif = abs(y_dif)
    print(y_absolute_dif)
    
    if (y_absolute_dif <= 0.10):
        print("Y-Cords Within Range")
        y_within_range = True
    else:
        print("Y-Cords Not Within Range")
    

    【讨论】:

    • 那我应该把代码改成什么?刚回object
    • specific_class = object1 之后添加obj1 = object1obj2 = object2specific_class = object2 之后。然后当你计算 x_dify_dif 使用 obj1obj2
    • 你能把它放在你的实际答案中以便正确格式化吗?
    【解决方案2】:

    重复代码表明您应该编写一个函数并对其进行测试以返回正确的结果。你的循环一直在继续,不会停下来找到坐标。

    import json
    
    json_string = r'''[
        {
            "frame_id": 1,
            "filename": "C:\\Yolo_v4\\darknet\\build\\darknet\\x64\\f047.png",
            "objects": [
                   {
                    "class_id": 14,
                    "name": "d",
                    "relative_coordinates": {
                        "center_x": 0.049905,
                        "center_y": 0.635935,
                        "width": 0.101077,
                        "height": 0.044067
                    },
                    "confidence": 0.966701
                },
                {
                    "class_id": 15,
                    "name": "e",
                    "relative_coordinates": {
                        "center_x": 0.045943,
                        "center_y": 0.685398,
                        "width": 0.109195,
                        "height": 0.041489
                    },
                    "confidence": 0.923188
                }
                ]
        }
    ]'''
    
    results = json.loads(json_string)
    
    class ValueNotFoundError(Exception): ...
    
    def get_xy(results,desired_id):
        for r in results:
            for o in r['objects']:
                if o['class_id'] == desired_id:
                    rc = o['relative_coordinates']
                    return rc['center_x'],rc['center_y'] # return results immediately
        # if not found...
        raise ValueNotFoundError
    
    def check(label,a,b,tolerance):
        if (d := abs(a - b)) <= tolerance:
            print(f'{label}-Coords Within Range: {d:.2}') 
            return True
        else:
            print(f'{label}-Coords Not Within Range: {d:.2}')
            return False
    
    x1,y1 = get_xy(results,14)
    x2,y2 = get_xy(results,15)
    tol = .1
    check('X',x1,x2,tol)
    check('Y',y1,y2,tol)
    
    X-Coords Within Range: 0.004
    Y-Coords Within Range: 0.049
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2017-04-30
      • 1970-01-01
      相关资源
      最近更新 更多