python - 在对象检测 api 中获取 neam_classes
问题描述
我需要帮助。处理项目对象检测 api 。在对象名称和可能性下在图像上标识的项目但我需要获取对象的名称以在条件下使用或将其打印在屏幕上使用以下程序:
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
model_fn = model.signatures['serving_default']
output_dict = model_fn(input_tensor)
# print(output_dict)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(output_dict.pop('num_detections'))
output_dict = {key:value[0, :num_detections].numpy()
for key,value in output_dict.items()}
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
# print(output_dict['detection_classes'])
# Handle models with masks:
if 'detection_masks' in output_dict:
# Reframe the the bbox mask to the image size.
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
output_dict['detection_masks'], output_dict['detection_boxes'],
image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.8,
tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
return output_dict
def show_inference(model, image_np):
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
# image_np = np.array(Image.open(image_path))
# Actual detection.
output_dict = run_inference_for_single_image(model, image_np)
# print(category_index)
# Visualization of the results of a detection.
final_img =vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks_reframed', None),
use_normalized_coordinates=True,
line_thickness=8)
return(final_img)```
My thanks to you.
解决方案
推荐阅读
- airflow - 全新安装后未运行 Ubuntu 20.04 上的 Apache Airflow
- ubuntu-16.04 - 在 ubuntu 16 中安装 ceres
- python - 下载 Python 音频文件 Jupyter Notebook
- matplotlib - 如何增加两个特定 matplotlib 子图之间的水平空间(hspace)?
- node.js - 在 Mac OS X 中创建反应应用程序时出现 EACCESS 权限错误
- html - 将数据从 angular typescript 推送到 chart.js 库的复杂性
- java - 错误休眠无法连接mysql数据库
- postgresql - Postgres返回错误:执行创建索引DDL语句时没有参数$1
- ruby-on-rails - 无法使用具有角度 9+ 的 Rails PaperClip Gem 上传图像
- javascript - XMLHttpRequest 只允许每页重新加载一个请求