Search code examples
opencvtensorflowobject-detectionobject-detection-api

Object Detection API using OpenCV but got wrong color output


I tried to detect a image in tensorflow object detection api.

But matplotlib can not show image successfully so I changed to use opencv to display the image result.

I got my result but the image colour is incorrect: https://i.sstatic.net/OYQlA.jpg

Am i missing any color handle script?

Original Source: https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

I changed last 2 scripts:

for image_path in TEST_IMAGE_PATHS:
    image = Image.open(image_path)
    image_np = load_image_into_numpy_array(image)
    image_np_expanded = np.expand_dims(image_np, axis=0)
    output_dict = run_inference_for_single_image(image_np, detection_graph)
    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)
    # plt.figure(figsize=IMAGE_SIZE)
    # plt.imshow(image_np)

    cv2.imshow('image', image_np)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Solution

  • Opencv uses BGR and Matplotlib RGB. You have to: cv2.cvtColor(image, cv2.COLOR_BGR2RGB)