Search code examples
pythontensorflowobject-detection

Hide the accuracy percentage from the bounding box generated


Display only the predicted class name and hide the accuracy/confidence percentage from the bounding box made on the detected object

I have trained a custom object detection model and get the bounding boxes with the predicted class name and also the confidence percentage on my object as of now. Below is my code

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)

suppose my model detectes a tiger from an image. So it makes a bounding box around the detected tiger showing the predicted class name with confidence percentage like (TIGER 80%). I want to display only the predicted class name on my bounding box and not the percentage when the bounding box is made like (TIGER) only


Solution

  • Here is a simple solution, just add skip_scores=True to function visualize_boxes_and_labels_on_image_array. So the function calls is:

    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)
    

    I've tested on an image from the Kitti dataset. No scores are displayed! enter image description here