【问题标题】:Count from a JSON file into python从 JSON 文件计数到 python
【发布时间】:2020-11-14 14:42:00
【问题描述】:
[
{
"frame_id":2, 
 "filename":"sample_imgs/2.jpg", 
 "objects": [ 
  {"class_id":8, "name":"boat", "relative_coordinates":{"left_x":1547, "top_y":1126, "width": 298, "height": 149}, "confidence":0.348690}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 128, "top_y":1327, "width":  30, "height":  89}, "confidence":0.972903}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 179, "top_y":1332, "width":  26, "height":  72}, "confidence":0.689888}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x":  79, "top_y":1324, "width":  29, "height":  73}, "confidence":0.663020}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 520, "top_y":1374, "width":  40, "height":  79}, "confidence":0.657240}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 565, "top_y":1367, "width":  36, "height":  84}, "confidence":0.447690}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 521, "top_y":1355, "width":  34, "height":  41}, "confidence":0.412204}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 159, "top_y":1318, "width":  27, "height":  77}, "confidence":0.306808}
 ]

如何使用 Python 解析文件并计算 JSON 文件中存在的 ID 数量? 我使用的代码显示了所有数据,但 id 类似于缩小范围,例如

"person": 7, 
"boat": 1

这是我的代码

import json
from collections import Counter

with open('/home/output/result.json') as json_data:
    data = json.load(json_data)
    c = Counter(k[4:] for d in data for k, v in d.items() if k.startswith('class_id'))
print(data)
print(json.dumps(c, indent=2, sort_keys=True))

【问题讨论】:

  • 你想要的值在name元素中,你为什么用k.startswith('class_id')

标签: python json object count


【解决方案1】:
data = [
{
"frame_id":2, 
 "filename":"sample_imgs/2.jpg", 
 "objects": [ 
  {"class_id":8, "name":"boat", "relative_coordinates":{"left_x":1547, "top_y":1126, "width": 298, "height": 149}, "confidence":0.348690}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 128, "top_y":1327, "width":  30, "height":  89}, "confidence":0.972903}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 179, "top_y":1332, "width":  26, "height":  72}, "confidence":0.689888}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x":  79, "top_y":1324, "width":  29, "height":  73}, "confidence":0.663020}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 520, "top_y":1374, "width":  40, "height":  79}, "confidence":0.657240}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 565, "top_y":1367, "width":  36, "height":  84}, "confidence":0.447690}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 521, "top_y":1355, "width":  34, "height":  41}, "confidence":0.412204}, 
  {"class_id":0, "name":"person", "relative_coordinates":{"left_x": 159, "top_y":1318, "width":  27, "height":  77}, "confidence":0.306808}
 ]
}]

import json
from collections import Counter
counter = Counter(obj.get('name') for item in data for obj in item['objects'])
print(counter.most_common())
with open('data.json', 'w') as f:
    json.dump(counter, f, indent=4

)

输出

[('person', 7), ('boat', 1)]

数据.json

{
    "boat": 1,
    "person": 7
}

这假设您在 JSON 文件中可能有多个帧并一次处理所有帧。当然你可以把counter转换成dict

【讨论】:

  • 是的,这个 JSON 中有多个帧。将转换为 dict 会向我显示 { "boat": 1, "person": 7 }
  • 是的,它会的。但是,如果您要写入 JSON 文件,则不需要。并且Counter 对象具有类似字典的界面。这真的只是一个演示问题吗
  • 感谢您的回复,如何使用此解决方案进行转换并获取 HEX 字符串而不是 python 列表?
  • 我建议您针对示例输入和预期输出提出单独的问题。
  • 虽然上述代码的修改解决方案按预期工作,但当我尝试打印后来在我的 JSON 中生成的高于 255 的值时,我无法表示。我得到一个字节必须在范围内(0, 256)错误。请记住,我将上述元组表示转换为我的应用程序所需的字节字符串。 “打印(字节(str))”
【解决方案2】:

假设你有数据文件a.json,你可以使用pandas和json来操作数据。

import json
import pandas as pd

with open('a.json') as fi:
    jdata = json.load(fi)


df = pd.DataFrame(data=jdata[0]['objects'])
output = df['name'].value_counts().to_dict()
print(output)

with open('output.json','w') as fo:
    json.dump(output,fo,indent=4)

输出

{'boat': 1, 'person': 7}

【讨论】:

  • 代码正在执行,但我没有打印出来。
  • 我在 jupyter notebook 中工作。编辑后的代码现在应该可以工作了。
  • 我得到了打印输出,谢谢,我有一个模块,我想发送 Lora 包,在我的代码中,我以 lora.sent("Hello") 的形式发送消息。当我粘贴 lora.sent(json.dump(output,fo,indent=4)) 时,我收到“没有字符串参数的编码。可能需要将 python 列表转换为字符串或十六进制。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2021-12-21
相关资源
最近更新 更多