多猫检测器
动物管理团队在使用猫咪检测器一段时间后发现,一次只追踪一只猫效率不高。如果能找到成群的猫会更好。
他们希望 Sam 在收到的告警消息中加入检测到的猫的数量。他们还要求她降低置信度阈值,以允许系统出现更多的误报。

Sam 已经完成:
- 创建 Rekognition 客户端。
- 使用 S3 上图像的 Bucket 和 Key 调用了
.detect_labels()。 - 将结果存入变量
response。
请帮助 Sam 拯救更多猫咪!请帮助她统计每张图像中的猫数量,并把该数量包含在发送给动物管理部门的告警中!
本练习是课程的一部分
Python 中的 AWS Boto 入门
练习说明
- 遍历
response中'Labels'键的每个元素。 - 当遇到名称为
'Cat'的标签时,遍历该标签的实例。 - 如果某个实例的置信度高于 85,则将
cat_counts增加 1。 - 打印最终的猫的数量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create an empty counter variable
cats_count = 0
# Iterate over the labels in the response
for label in response['____']:
# Find the cat label, look over the detected instances
if label['____'] == 'Cat':
for instance in label['____']:
# Only count instances with confidence > 85
if (instance['_____'] > 85):
cats_count += 1
# Print count of cats
print(____)