python - Tensorflow对象检测api获取数组中的标签

标签 python tensorflow

我想从 Tensorflow 对象检测 api 获取标签并将它们放入数组中,而不是在视频上显示它们

这是 detector_object 函数

def detect_objects(image_np, sess, detection_graph):
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
    [boxes, scores, classes, num_detections],
    feed_dict={image_tensor: image_np_expanded})

# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
    image_np,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8)


return image_np

最佳答案

经过一番研究,这就是我的想法

final_score = np.squeeze(scores)    
    count = 0
    for i in range(100):
        if scores is None or final_score[i] > 0.5:
                count = count + 1
    print('cpunt',count)
    printcount =0;
    for i in classes[0]:
          printcount = printcount +1
          print(category_index[i]['name'])

          if(printcount == count):
                break

这将打印所有检测到的对象,如果你想返回它,你可以将它添加到某个变量并返回。

如果您只想打印检测到的对象,请在 util 文件夹内的 Visualization_utils.py 文件中添加 print(class_name)

if not agnostic_mode:
          if classes[i] in category_index.keys():
            class_name = category_index[classes[i]]['name']

            **print(class_name)** --> this line 
          else:

关于python - Tensorflow对象检测api获取数组中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50077353/

相关文章:

python - 十六进制十进制浮点Python

python - 在 pycharm 上加载经过训练的 Tensorflow 保存模型时出错。值错误 : invalid literal for int() with base 10: 'class_name'

python - TensorFlow matmul : Blas xGEMMBatched launch failed

python - tf.keras.layers.RNN 与 tf.keras.layers.StackedRNNCells : Tensorflow 2

python - TensorFlow 2.0 如何从 tf.keras.layers 层获取可训练变量,如 Conv2D 或 Dense

python - Tensorflow feed_dict 值错误 : setting an array element with a sequence

python - 如何从 Python 中的管道命令获取交互式输出?

python - 绘图破折号链接到本地​​html文件

python - 将 timedelta 添加到日期时间不会移动到下一天

python - 在 tkinter GUI 中使用 for 循环在新行中列出项目