I have an object detection model that I use with opencv to detect my custom class.
I want to output the boxes only when the model is 95% or more confident.
Is there a way I can configure that?
(Bonus question: Can I set it so that only the object with the highest confidence is shown? Example: The cam detects two objects with 98% and 91% confidence respectively. I want it to only output the box for the 98% one.)
In case you need it, here is my inference code that uses opencv.
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while True:
ret, image_np = cap.read()
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
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')
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
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=6)
cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
Okay, answering my own question. It only took me 1 minute to find out on my own.
This is the function that visualizes the boxes. Its input parameters are well explained in the source code.
object_detection/utils/visualization_utils.visualize_boxes_and_labels_on_image_array(...)
For my case, I just had to set
min_score_thresh=.95
and max_boxes_to_draw=1
when calling this function.