0
[
{
"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}
 ]

How can I parse the file and count the number of IDs present in the JSON file using Python? The code i used displayed all the data, but id like something of narrowing down eg

"person": 7, 
"boat": 1

Here's my code

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))
1
  • 1
    The values you want are in the name element, why are you using k.startswith('class_id')? Commented Nov 14, 2020 at 14:58

2 Answers 2

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

)

output

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

data.json

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

This assumes you may have more than one frame in the JSON file and handles all frames at once. Of course you can convert counter to dict

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, there are multiple frame into this JSON. Will converting to dict would display me the { "boat": 1, "person": 7 }
Yes, it will. But if you going to write to JSON file you don't need to. And Counter object has dict-like interface. Do it's really just a presentation issue
Thanks for the reply, how its possible to convert this and getting a HEX string instead of a python list using this solution?
I would suggest you ask separate question with the sample input and expected output.
While a modified solution of the above code worked as expected i cant represent while i try to print values above 255 that later generated in my JSON. i get a byte must be in range(0, 256) error. Bear in mind i converted the above tuple representation to a bytes string needed for my application. "print(bytes(str))"
0

Assuming you have data file a.json, you can use pandas and json to manipulate the data.

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)

output

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

3 Comments

Code is executing, but i dont get the print.
I was working in jupyter notebook. The edited code should work now.
I get the printout thank you, im having a module were i want to sent Lora packages and in my code i sent messages in the form of lora.sent("Hello"). When im pasting the lora.sent(json.dump(output,fo,indent=4)) i get the error of "Encoding without a string argument. Probably that python list needs to be converted to a string or a hex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.