I am programing an ALPR system and I am using the OpenALPR API to extract a license plate number from a picture. The default Python code to access the service looks like this:
import base64
import json
# Sample image file is available at http://plates.openalpr.com/ea7the.jpg
IMAGE_PATH = '/tmp/sample.jpg'
SECRET_KEY = 'sk_DEMODEMODEMODEMODEMODEMO'
with open(IMAGE_PATH, 'rb') as image_file:
img_base64 = base64.b64encode(image_file.read())
url = 'https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s' % (SECRET_KEY)
r = requests.post(url, data = img_base64)
print(json.dumps(r.json(), indent=2))
I am new to JSON so now I am not sure how to extract a certain value. A possible output of the print command could look like this:
{
"data_type" : "alpr_results",
"epoch_time" : 1592849838217,
"img_width" : 600,
"img_height" : 600,
"error" : false,
"version" : 3,
"credit_cost" : 1,
"uuid" : "",
"processing_time" : {
"plates" : 85.0386276245117,
"vehicles" : 76.9439999994574,
"total" : 830.30300000064
},
"regions_of_interest" : [
{
"y" : 0,
"height" : 600,
"x" : 0,
"width" : 600
}
],
"credits_monthly_used" : 5001,
"credits_monthly_total" : 100000,
"results" : [
{
"requested_topn" : 10,
"matches_template" : 1,
"region_confidence" : 99,
"plate_index" : 0,
"plate" : "627WWI",
"region" : "us-wa",
"confidence" : 94.0910949707031,
"coordinates" : [
{
"x" : 238,
"y" : 358
},
{
"x" : 357,
"y" : 358
},
{
"x" : 357,
"y" : 408
},
{
"x" : 239,
"y" : 409
}
],
"candidates" : [
{
"matches_template" : 1,
"plate" : "627WWI",
"confidence" : 94.0910949707031
},
{
"matches_template" : 0,
"plate" : "627WW1",
"confidence" : 74.0910949707031
}
],
"vehicle_region" : {
"y" : 96,
"x" : 3,
"width" : 589,
"height" : 483
},
"processing_time_ms" : 8.83281803131104,
"vehicle_detected" : true,
"vehicle" : {
"make" : [
{
"name" : "daihatsu",
"confidence" : 67.1475448608398
},
{
"name" : "mini",
"confidence" : 18.6626739501953
},
{
"name" : "fiat",
"confidence" : 8.87434959411621
},
{
"confidence" : 1.25781571865082,
"name" : "scion"
},
{
"confidence" : 1.04451608657837,
"name" : "toyota"
}
],
"year" : [
{
"name" : "2005-2009",
"confidence" : 54.0047645568848
},
{
"confidence" : 39.7950668334961,
"name" : "2000-2004"
},
{
"name" : "2010-2014",
"confidence" : 5.38613367080688
},
{
"name" : "1995-1999",
"confidence" : 0.628315031528473
},
{
"confidence" : 0.16707843542099,
"name" : "1990-1994"
}
],
"color" : [
{
"confidence" : 77.7851638793945,
"name" : "silver-gray"
},
{
"confidence" : 9.67624187469482,
"name" : "blue"
},
{
"name" : "black",
"confidence" : 7.02981376647949
},
{
"name" : "green",
"confidence" : 2.66030049324036
},
{
"name" : "gold-beige",
"confidence" : 1.42329037189484
}
],
"orientation" : [
{
"name" : "180",
"confidence" : 99.9426956176758
},
{
"name" : "225",
"confidence" : 0.0323438383638859
},
{
"name" : "135",
"confidence" : 0.0129895266145468
},
{
"name" : "45",
"confidence" : 0.00725177442654967
},
{
"confidence" : 0.00194440386258066,
"name" : "90"
}
],
"body_type" : [
{
"confidence" : 99.8512573242188,
"name" : "sedan-compact"
},
{
"name" : "tractor-trailer",
"confidence" : 0.0433907173573971
},
{
"name" : "sedan-sports",
"confidence" : 0.0432351566851139
},
{
"confidence" : 0.0218438617885113,
"name" : "suv-crossover"
},
{
"confidence" : 0.0212091449648142,
"name" : "sedan-wagon"
}
],
"make_model" : [
{
"name" : "mini_cooper-s",
"confidence" : 35.001880645752
},
{
"confidence" : 23.0991153717041,
"name" : "mini_cooper"
},
{
"confidence" : 5.29244709014893,
"name" : "daihatsu_sirion"
},
{
"name" : "mini_hatch",
"confidence" : 4.69152307510376
},
{
"confidence" : 4.35747337341309,
"name" : "daihatsu_tanto"
}
]
}
}
],
"vehicles" : [
{
"x" : 3,
"y" : 96,
"width" : 589,
"height" : 483,
"details" : {
"orientation" : [
{
"name" : "180",
"confidence" : 99.9426956176758
},
{
"name" : "225",
"confidence" : 0.0323438383638859
},
{
"name" : "135",
"confidence" : 0.0129895266145468
},
{
"confidence" : 0.00725177442654967,
"name" : "45"
},
{
"name" : "90",
"confidence" : 0.00194440386258066
}
],
"make" : [
{
"name" : "daihatsu",
"confidence" : 67.1475448608398
},
{
"name" : "mini",
"confidence" : 18.6626739501953
},
{
"confidence" : 8.87434959411621,
"name" : "fiat"
},
{
"confidence" : 1.25781571865082,
"name" : "scion"
},
{
"confidence" : 1.04451608657837,
"name" : "toyota"
}
],
"color" : [
{
"name" : "silver-gray",
"confidence" : 77.7851638793945
},
{
"name" : "blue",
"confidence" : 9.67624187469482
},
{
"name" : "black",
"confidence" : 7.02981376647949
},
{
"confidence" : 2.66030049324036,
"name" : "green"
},
{
"confidence" : 1.42329037189484,
"name" : "gold-beige"
}
],
"year" : [
{
"name" : "2005-2009",
"confidence" : 54.0047645568848
},
{
"name" : "2000-2004",
"confidence" : 39.7950668334961
},
{
"name" : "2010-2014",
"confidence" : 5.38613367080688
},
{
"name" : "1995-1999",
"confidence" : 0.628315031528473
},
{
"confidence" : 0.16707843542099,
"name" : "1990-1994"
}
],
"make_model" : [
{
"confidence" : 35.001880645752,
"name" : "mini_cooper-s"
},
{
"confidence" : 23.0991153717041,
"name" : "mini_cooper"
},
{
"name" : "daihatsu_sirion",
"confidence" : 5.29244709014893
},
{
"confidence" : 4.69152307510376,
"name" : "mini_hatch"
},
{
"name" : "daihatsu_tanto",
"confidence" : 4.35747337341309
}
],
"body_type" : [
{
"confidence" : 99.8512573242188,
"name" : "sedan-compact"
},
{
"confidence" : 0.0433907173573971,
"name" : "tractor-trailer"
},
{
"confidence" : 0.0432351566851139,
"name" : "sedan-sports"
},
{
"confidence" : 0.0218438617885113,
"name" : "suv-crossover"
},
{
"confidence" : 0.0212091449648142,
"name" : "sedan-wagon"
}
]
}
}
]
}
I aim at passing the candidate with the highest probability (regarding the "plate" number) of being accurate to a new String which I can use later on in my program.
How would I be able to do that? Any help is highly appreciated!
CodePudding user response:
You can simply do:
jsn = r.json()
resultsList = jsn['results']
candidateList = []
for r in resultsList:
candidateList.append(r['candidates'])
You can iterate through candidateList
as follows:
for c in candidateList:
# you can do comparison here to find probability
print(c['plate'])
EDIT 1: I updated the code after realizing the results
is list
, not a dict
CodePudding user response:
Idk what are you aiming for here but from what i think maybe max?
Assuming candidates and results are definied (so no Errors are raised) you can do something like this
data = r.json() # the response json (a dict object)
results = data["results"]
all_best_candidates = []
for result in results:
candidate_list = result["candidates"]
best_candidate = max(candidate_list, key=lambda x: x["confidence"])
# You can do whatever you want here
# Just dont forget that results can be more than 1
# this will be used later to get the best candidate of ALL results
all_best_candidates.append(best_candidate)
absolute_best_candidate = max(all_best_candidates, key=lambda x: x["confidence"])
but i dont even know what you want here to down vote this if its wrong
Simple Explaination:
The max function compares the elements in the list to find the maximal value, we use the key
argument so that we can tell the max
function what value to use, in this case candidates
.
data['results']
is a list with bunch of dicts so we use the max as stated above to get the Highest of each result and store them in best_candidates
then we can use this list to get the best of them all with the same method.
EDIT: after testing this I realised that results is a list of dict objects, I will fix this but for the time being DONT USE THIS CODE
EDIT 2: Fixed it you can use this :)
EDIT 3: You should really make sure that the request was successfull or you will run into some errors