在Tensorflow对象检测API中,获取预测值的百分比可以通过以下步骤实现:
model.detect()
或类似的方法。score
属性来获取置信度的百分比值。以下是一个示例代码:
import tensorflow as tf
from object_detection.utils import label_map_util
# 加载标签映射文件
label_map_path = 'path/to/label_map.pbtxt'
label_map = label_map_util.load_labelmap(label_map_path)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
# 加载模型
model_path = 'path/to/model'
detection_model = tf.saved_model.load(model_path)
# 加载待检测的图像
image_path = 'path/to/image.jpg'
image_np = tf.image.decode_image(open(image_path, 'rb').read(), channels=3)
# 图像预处理
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]
# 目标检测
detections = detection_model(input_tensor)
# 获取预测值的百分比
for i in range(detections['detection_scores'][0].numpy().shape[0]):
class_id = int(detections['detection_classes'][0][i].numpy())
class_name = category_index[class_id]['name']
score = detections['detection_scores'][0][i].numpy()
percentage = round(score * 100, 2)
print(f"Detected {class_name} with {percentage}% confidence.")
在上述代码中,首先加载了标签映射文件,然后加载了预训练的模型。接着,加载待检测的图像并进行预处理。最后,通过遍历检测结果列表,获取每个检测结果的置信度,并将其转换为百分比形式进行展示。
对于腾讯云相关产品和产品介绍链接地址,由于要求不能提及具体品牌商,建议在腾讯云官方网站上查找与Tensorflow对象检测API相关的产品和服务。
领取专属 10元无门槛券
手把手带您无忧上云