我是Python的新手,我正在寻找如何从下面的JSON文件中提取数据的建议。
我想要一个Python脚本执行以下操作
查找标签"汽车"如果置信度为90,则计数boundingbox;和输出"否则输出"there ' s no Cars"
查找标签"人物"如果置信度为85,则计数boundingbox;和输出"The are x people";否则输出&;there are no people &;
你会使用"import json">
进口json
我非常感谢你在这件事上提供的任何帮助
{
"Labels": [{
"Name": "Car",
"Confidence": 97.12930297851562,
"Instances": [{
"BoundingBox": {
"Width": 0.3040655553340912
},
"Confidence": 97.12930297851562
},
{
"BoundingBox": {
"Width": 0.21719124913215637
},
"Confidence": 87.27943420410156
},
{
"BoundingBox": {
"Width": 0.1468130648136139
},
"Confidence": 63.32168960571289
}
],
"Parents": [{
"Name": "Vehicle"
},
{
"Name": "Transportation"
}
]
},
{
"Name": "Automobile",
"Confidence": 97.12930297851562,
"Instances": [],
"Parents": [{
"Name": "Vehicle"
},
{
"Name": "Transportation"
}
]
},
{
"Name": "Person",
"Confidence": 85.63351440429688,
"Instances": [{
"BoundingBox": {
"Width": 0.024131836369633675
},
"Confidence": 85.63351440429688
}],
"Parents": []
},
{
"Name": "Freeway",
"Confidence": 55.29652786254883,
"Instances": [],
"Parents": [{
"Name": "Road"
}]
}
],
"LabelModelVersion": "2.0"
}
谢谢-我使用了你的建议,并提出了这个python脚本。我仍然需要弄清楚如何剥离外部JSON标签{"Labels"(]}],"LabelModelVersion"2.0"}
该脚本将用于从Amazon AWS rekrecognition和人员和车辆计数中获取响应。
import json
#### Import Data from Analysis service ####
print ('### Raw JSON file from analysis ###')
z = {"Labels":[{"Name":"Car","Confidence":97.12930297851562,"Instances":
[{"BoundingBox":
{"Width":0.3040655553340912},"Confidence":97.12930297851562},
{"BoundingBox":
{"Width":0.21719124913215637},"Confidence":87.27943420410156},
{"BoundingBox":
{"Width":0.1468130648136139},"Confidence":63.32168960571289}],"Parents":
[{"Name":"Vehicle"},{"Name":"Transportation"}]},
{"Name":"Automobile","Confidence":97.12930297851562,"Instances":
[],"Parents":[{"Name":"Vehicle"},{"Name":"Transportation"}]},
{"Name":"Person","Confidence":85.63351440429688,"Instances":
[{"BoundingBox":
{"Width":0.024131836369633675},"Confidence":85.63351440429688}],"Parents":
[]},{"Name":"Freeway","Confidence":55.29652786254883,"Instances":
[],"Parents":[{"Name":"Road"}]}],"LabelModelVersion":"2.0"}
# convert into JSON:
w = json.dumps(z)
#### Need to find a procedure to remove the outer {"Labels":
[],"LabelModelVersion":"2.0"}
print ('### JSON file with outer label removed ###')
x = {"Name":"Car","Confidence":97.12930297851562,"Instances":
[{"BoundingBox":
{"Width":0.3040655553340912},"Confidence":97.12930297851562},
{"BoundingBox":
{"Width":0.21719124913215637},"Confidence":87.27943420410156},
{"BoundingBox":
{"Width":0.1468130648136139},"Confidence":63.32168960571289}],"Parents":
[{"Name":"Vehicle"},{"Name":"Transportation"}]},
{"Name":"Automobile","Confidence":97.12930297851562,"Instances":
[],"Parents":[{"Name":"Vehicle"},{"Name":"Transportation"}]},
{"Name":"Person","Confidence":85.63351440429688,"Instances":
[{"BoundingBox":
{"Width":0.024131836369633675},"Confidence":85.63351440429688}],"Parents":
[]},{"Name":"Freeway","Confidence":55.29652786254883,"Instances":
[],"Parents":[{"Name":"Road"}]}
# convert into JSON:
y = json.dumps(x)
print ('### Searching for people ###')
# Search for People
for p in x:
if p['Name'] == 'Person':
people_conf = (p['Confidence'])
people_inst = (p['Instances'])
people_count= len(people_inst)
print ('### Searching for cars ###')
# Search for Cars
for c in x:
if c['Name'] == 'Car':
car_conf = (c['Confidence'])
car_inst = (c['Instances'])
car_count=len(car_inst)
#### Data from searches ####
print ('### Data from searches ###')
#### Grade People Count ####
print ('### Start Grading ###')
if (people_conf > 84 and people_count >= 100):
peopleScore = 'crowded'
elif (people_conf > 84 and people_count >= 50):
peopleScore = 'manageable'
elif (people_conf > 84 and people_count >= 1):
peopleScore = 'empty'
else:
peopleScore = 'unknown'
#### Grade Car Count ####
if (car_conf >= 97 and car_count >= 100):
carScore = 'crowded'
elif (car_conf >= 97 and car_count >= 3):
carScore = 'manageable'
elif (car_conf >= 97 and car_count >= 1):
carScore = 'empty'
else:
carScore = 'unknown'
#### Print Grading ####
print ("People =:",peopleScore)
print ("Cars =:",carScore)
下面是脚本
的输出C:Usersmcronin02Desktop>python project-python-final1.py
### Raw JSON file from analysis ###
### JSON file with outer label removed ###
### Searching for people ###
### Searching for cars ###
### Data from searches ###
85.63351440429688
[{'BoundingBox': {'Width': 0.024131836369633675}, 'Confidence':
85.63351440429688}]
1
97.12930297851562
[{'BoundingBox': {'Width': 0.3040655553340912}, 'Confidence':
97.12930297851562}, {'BoundingBox': {'Width': 0.21719124913215637},
'Confidence': 87.27943420410156}, {'BoundingBox': {'Width':
0.1468130648136139}, 'Confidence': 63.32168960571289}]
3
### Start Grading ###
People =: empty
Cars =: manageable
这是我的最终版本
# Project to take JSON response from AWS Rekognition and then People and Car
count v1.0 7/11/2022
import json
# Create Variables
people_confidence = (0)
people_instance = (0)
people_count = (0)
car_confidence = (0)
car_instance = (0)
car_count = (0)
# Import Data from Analysis service
raw_data = open('json1.json')
data = json.load(raw_data)
# Strip off outer Label
filtered_data = (data["Labels"])
# Search for People
for people in filtered_data:
if people['Name'] == 'Person':
people_confidence = (people['Confidence'])
people_instance = (people['Instances'])
people_count= len(people_instance)
# Search for Cars
for car in filtered_data:
if car['Name'] == 'Car':
car_confidence = (car['Confidence'])
car_instance = (car['Instances'])
car_count=len(car_instance)
# Grade People Count
if (people_confidence > 95 and people_count >= 100):
people_score = 'crowded'
elif (people_confidence > 95 and people_count >= 10):
people_score = 'manageable'
elif (people_confidence > 95 and people_count >= 1):
people_score = 'empty'
else:
people_score = 'unknown'
# Grade Car Count
if (car_confidence >= 95 and car_count >= 100):
car_score = 'crowded'
elif (car_confidence >= 95 and car_count >= 3):
car_score = 'manageable'
elif (car_confidence >= 95 and car_count >= 1):
car_score = 'empty'
else:
car_score = 'unknown'
# Print Grading
print ("People =:",people_score)
print ("Cars =:",car_score)