从生成的边界框中隐藏准确性百分比

时间:2019-04-10 17:12:00

标签: python tensorflow object-detection

仅显示预测的类名,并从检测到的对象上的边界框中隐藏准确性/可信度百分比

我已经训练了一个自定义的对象检测模型,并获得了带有预测类名的边界框以及到目前为止我对对象的置信度百分比。下面是我的代码

def recognize_object(model_name,ckpt_path,label_path,test_img_path):

    count=0
    sys.path.append("..")

    MODEL_NAME = model_name

    PATH_TO_CKPT = ckpt_path


    PATH_TO_LABELS = label_path

    PATH_TO_IMAGE = list(glob(test_img_path))

    NUM_CLASSES = 3

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    detection_graph = tf.Graph()

    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

        sess = tf.Session(graph=detection_graph)

    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')


    for paths in range(len(PATH_TO_IMAGE)):
        image = cv2.imread(PATH_TO_IMAGE[paths])
        image_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_expanded})


        vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=4,
        min_score_thresh=0.80)


        coordinates=vis_util.return_coordinates(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=4,
        min_score_thresh=0.80)

        threshold=0.80


cv2.imwrite("C:\\new_multi_cat\\models\\research\\object_detection\\my_imgs\\frame%d.jpg"%count,image)
        count += 1
        cv2.waitKey(0)
        cv2.destroyAllWindows()


model_name='inference_graph'
ckpt_path=("C:\\new_multi_cat\\models\\research\\object_detection\\inference_graph\\frozen_inference_graph.pb")
label_path=("C:\\new_multi_cat\\models\\research\\object_detection\\training\\labelmap.pbtxt")
test_img_path=("C:\\Python35\\target_non_target\\Target_images_new\\*.jpg")

recognize = recognize_object(model_name,ckpt_path,label_path,test_img_path)

假设我的模型从图像中检测到老虎。因此,它在检测到的老虎周围制作了一个边界框,显示了具有预测百分比的类名(可信度为(TIGER 80%))。我只想在边界框上显示预测的类名,而不要在边界框仅显示为(TIGER)时显示百分比

1 个答案:

答案 0 :(得分:1)

这是一个简单的解决方案,只需将skip_scores=True添加到功能visualize_boxes_and_labels_on_image_array。因此,函数调用为:

vis_util.visualize_boxes_and_labels_on_image_array(
    image,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index = category_index,
    use_normalized_coordinates=True,
    line_thickness=4,
    min_score_thresh=0.80,
    skip_scores=True)

我已经对Kitti数据集中的图像进行了测试。没有分数显示! enter image description here