【发布时间】:2020-09-21 11:16:39
【问题描述】:
我正在构建一个对象检测应用程序。它检测给定图像中的平面并将坐标写入字典:
planes =
{'Plane 1': {'Plane Coordinates': [20.0, 20.0, 551.0, 52.0], 'Plane Type': 1, 'Points': [2]},
'Plane 2': {'Plane Coordinates': [502.0, 425.0, 741.0, 713.0], 'Plane Type': 7, 'Points': [2]},
'Plane 3': {'Plane Coordinates': [55.0, 669.0, 310.0, 736.0], 'Plane Type': 2, 'Points': [2]},
'Plane 4': {'Plane Coordinates': [503.0, 1194.0, 805.0, 1222.0], 'Plane Type': 5, 'Points': []},
'Plane 5': {'Plane Coordinates': [56.0, 1240.0, 391.0, 1268.0], 'Plane Type': 8, 'Points': []}}
但这里是棘手的部分:
我希望用户向 Points 键添加值。因此,当检测完成时,我想创建一个表单,人们可以在其中向检测到的平面添加点。
我决定用Flask WTForms(不过我可以根据建议改一下,也许我可以用JS?)我只是不知道如何根据检测到的数字复制相同的字段飞机。
所以我想出了这样的解决方案:
我将字典保存为 JSON 文件。然后在forms.py 中使用以下代码打开它:
json_file_path = "planes.json"
with open(json_file_path, 'r') as j:
contents = json.loads(j.read())
试试这个:
class PlanesValue(Form):
for key in contents:
plane = FieldList(StringField(key), min_entries=1)
submit = SubmitField('Submit')
但问题是它只是将最后一个Plane 即Plane 5 呈现为唯一的字段。
我可以理解,因为它循环遍历内容中的键,所以它只是想出字典中的最后一项,我不知道如何解决这个问题。
另外我不知道如何将这些值一一添加到表单验证上面的planes 字典中:/
如果有任何帮助,我将不胜感激。提前致谢。
编辑:
我想要达到的目标:
我希望 Flask WTForms 查看planes 字典,而不是此处的“静态字段”,获取标签并生成:“平面 1、平面 2、平面 3、平面 4、平面 5”
【问题讨论】:
-
你能更好地解释你想要达到的目标吗?试着一步一步地给出一个简单的例子。 “根据检测到的平面 nb 复制相同的字段”是什么意思?此外,您可以通过
planes['Plane 1']['Points'].append(3)访问点列表并添加到其中 -
谢谢,我已经编辑了原帖并添加了我想要实现的目标。
标签: python json flask flask-wtforms