tensorflow / models

Models and examples built with TensorFlow
Other
76.99k stars 45.79k forks source link

Bounding box is not showing #11222

Closed Prerana-V closed 3 months ago

Prerana-V commented 3 months ago

I am doing object detection using tensorflow. The problem is whole code is running but it was not showing bounding box on image. I am running the code in colab. I tried each and every possible way to solve but the problem is not solved yet. If anyone can help me in this pls help me out.

My code is this-

Loading the saved_model

import tensorflow as tf import time import numpy as np import warnings warnings.filterwarnings('ignore') from PIL import Image from google.colab.patches import cv2_imshow from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as viz_utils import matplotlib.pyplot as plt

print('Loading model...', end='')

*Load saved model and build the detection function detect_fn=tf.saved_model.load(PATH_TO_SAVED_MODEL) print('Done!')

*Loading the label_map category_index=label_map_util.create_category_index_from_labelmap("/mydrive/customTF2/data/label_map.pbtxt",use_display_name=True)

def load_image_into_numpy_array(path): img = Image.open(path) img = img.resize((800, 600)) # Resize the image to a smaller size return np.array(img)

image_path = "/mydrive/mask_test_images/image2.jpg"

image_np = load_image_into_numpy_array(image_path)

input_tensor = tf.convert_to_tensor(image_np) input_tensor = input_tensor[tf.newaxis,...]

detections = detect_fn(input_tensor)

num_detections = int(detections.pop('num_detections')) detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()} detections['num_detections'] = num_detections

detections['detection_classes'] = detections['detection_classes'].astype(np.uint8)

image_np_with_detections_uint8 = image_np_with_detections.astype(np.uint8) viz_utils.visualize_boxes_and_labels_on_image_array( image_np_with_detections, detections['detection_boxes'], detections['detection_classes'], detections['detection_scores'], category_index, use_normalized_coordinates=False, max_boxes_to_draw=100, min_score_thresh=0.2, # Adjust this value to set the minimum probability boxes to be classified as True agnostic_mode=True)

print("Detection scores:", detections['detection_scores']) # Print detection scores

%matplotlib inline

plt.figure(figsize=IMAGE_SIZE, dpi=200)

plt.axis("off")

plt.imshow(image_np_with_detections)

plt.show() # Add plt.show() to display the image

plt.figure(figsize=(24,32)) plt.imshow(image_np_with_detections) plt.show().

laxmareddyp commented 3 months ago

Hi @Prerana-V ,

May be the issue here was image_np_with_detections is not defined in your code and it doesn't hold any values if i am not wrong and please check the following params in your code use_normalized_coordinates=True and min_score_thresh = 0.2, this value,bounding boxes with a confidence score above 0.2 will be visualized and need to adjusted accordingly.

Thanks.

github-actions[bot] commented 3 months ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 3 months ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 3 months ago

Are you satisfied with the resolution of your issue? Yes No