Search code examples
python-3.xtensorflowobject-detection

output_dict['detection_boxes'], KeyError: 'detection_boxes'


I am using this code: https://www.edureka.co/blog/tensorflow-object-detection-tutorial/ . From the line: vis_util.visualize_boxes_and_labels_on_image_array( and down I use Vadim's code: Get the bounding box coordinates in the TensorFlow object detection API tutorial this:

#so detection has happened and you've got output_dict as a
# result of your inference

# then assume you've got this in your inference.py in order to draw rectangles
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'),
    use_normalized_coordinates=True,
    line_thickness=8)

# This is the way I'm getting my coordinates
boxes = output_dict['detection_boxes']
# get all boxes from an array
max_boxes_to_draw = boxes.shape[0]
# get scores to get a threshold
scores = output_dict['detection_scores']
# this is set as a default but feel free to adjust it to your needs
min_score_thresh=.5
# iterate over all objects found
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    # 
    if scores is None or scores[i] > min_score_thresh:
        # boxes[i] is the box which will be drawn
        class_name = category_index[output_dict['detection_classes'][i]]['name']
        print ("This box is gonna get used", boxes[i], output_dict['detection_classes'][i])

I am getting this insane!!! error:

    output_dict['detection_boxes'],
KeyError: 'detection_boxes'

I am searching for many hours on google and on stackoverflow. I haven't found something helpful. Does anyone has any idea what to do?

Update(12/08/2022): I have added prints throughout the def run_inference_for_single_image(image, graph): Here is what I get:

ops =  []
all_tensor_names =  set()
tensor_name =  num_detections:0
tensor_name =  detection_boxes:0
tensor_name =  detection_scores:0
tensor_name =  detection_classes:0
tensor_name =  detection_masks:0
returned output_dict =  {}
output_dict =  {}
Traceback (most recent call last):
  File "/home/someone/Desktop/test/test_code.py", line 132, in <module>
    output_dict['detection_boxes'],
KeyError: 'detection_boxes'

For some reason it does read anything from the files, right? Do you see something I don't? Any ideas?


Solution

  • As I was saying in the comments, from the error it looks like that output_dict does not have a key "detection_boxes". The OP also confirmed that the function responsible for creating the dict, run_inference_for_single_image(image, graph) outputs an empty output_dict.

    So the problem lies within this function most probably or to what is passed as arguments. You should debug the function a bit, maybe printing the variables within to discover the issue. Let me know how it goes!