如何从这个请求中得到想要的值?
How to get the desired value from this request?
我正在使用休息 api 模型来提出以下请求:
def predict(path):
with open(path) as img:
res = vr.classify(images_file=img, threshold=0, classifier_ids=['food'])
print res
当我 运行 我的脚本时,我得到:
{u'images': [{u'image': u'/tacos.jpg', u'classifiers': [{u'classes': [{u'score': 0.0495783, u'class': u'pizza'}, {u'score': 0.553117, u'class': u'tacos'}], u'classifier_id': u'food', u'name': u'food-test'}]}], u'custom_classes': 2, u'images_processed': 1}
但是我只想得到 class 具有更高价值的如下:
this is the corresponding class: tacos
所以我想感谢支持修改我的函数以获得所需的输出
这是一个字典,因此您可以遍历 'classes' 并找到最高分。
免责声明:我不是 python2 或 watson 用户。
访问 类
res['images'][0]['classifiers'][0]['classes']
因此要遍历 类...
highest_class = ['', 0]
for class in res['images'][0]['classifiers'][0]['classes']:
if class['score'] > highest_class[1]:
highest_class = [class['class'], [class['score']
print "this is the corresponding class: " + highest_class[0]
当然,如果您有 1 个以上的分类器,则必须有另一个外部 for 循环来遍历分类器(如果您需要该功能)
如何使用 python-native sorted
函数,为每个分类器的 id 获取更高的值?使用(太大)大的对象名称以便清楚,并避免打代码,您可能需要执行以下操作
def predict(path):
with open(path) as img:
res = vr.classify(images_file=img, threshold=0, classifier_ids=['food'])
dict_of_higher_value_per_ = {} # in order to record and reuse values sooner or later.
for image in res['images']:
for classifier in image['classifiers']:
classes = classifier['classes']
classifier_id = classifier['classifier_id']
sorted_scores = sorted(classes,
key=lambda class_:class_['score'],
reverse=True)
best_match = sorted_scores[0] # which corresponds to the best score since elements are sorted.
dict_of_higher_value_per_[classifier_id] = best_match
print "Classifier '{cid}' says this is the corresponding class: {class}".format(cid=classifier_id,
**best_match)
打印
Classifier 'food' says this is the corresponding class: tacos
我正在使用休息 api 模型来提出以下请求:
def predict(path):
with open(path) as img:
res = vr.classify(images_file=img, threshold=0, classifier_ids=['food'])
print res
当我 运行 我的脚本时,我得到:
{u'images': [{u'image': u'/tacos.jpg', u'classifiers': [{u'classes': [{u'score': 0.0495783, u'class': u'pizza'}, {u'score': 0.553117, u'class': u'tacos'}], u'classifier_id': u'food', u'name': u'food-test'}]}], u'custom_classes': 2, u'images_processed': 1}
但是我只想得到 class 具有更高价值的如下:
this is the corresponding class: tacos
所以我想感谢支持修改我的函数以获得所需的输出
这是一个字典,因此您可以遍历 'classes' 并找到最高分。
免责声明:我不是 python2 或 watson 用户。
访问 类
res['images'][0]['classifiers'][0]['classes']
因此要遍历 类...
highest_class = ['', 0]
for class in res['images'][0]['classifiers'][0]['classes']:
if class['score'] > highest_class[1]:
highest_class = [class['class'], [class['score']
print "this is the corresponding class: " + highest_class[0]
当然,如果您有 1 个以上的分类器,则必须有另一个外部 for 循环来遍历分类器(如果您需要该功能)
如何使用 python-native sorted
函数,为每个分类器的 id 获取更高的值?使用(太大)大的对象名称以便清楚,并避免打代码,您可能需要执行以下操作
def predict(path):
with open(path) as img:
res = vr.classify(images_file=img, threshold=0, classifier_ids=['food'])
dict_of_higher_value_per_ = {} # in order to record and reuse values sooner or later.
for image in res['images']:
for classifier in image['classifiers']:
classes = classifier['classes']
classifier_id = classifier['classifier_id']
sorted_scores = sorted(classes,
key=lambda class_:class_['score'],
reverse=True)
best_match = sorted_scores[0] # which corresponds to the best score since elements are sorted.
dict_of_higher_value_per_[classifier_id] = best_match
print "Classifier '{cid}' says this is the corresponding class: {class}".format(cid=classifier_id,
**best_match)
打印
Classifier 'food' says this is the corresponding class: tacos