Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow object detection api get labels in array

I want to get labels from Tensorflow object detection api and put them into array instead of showing them on the video

this is detect_object function

def detect_objects(image_np, sess, detection_graph):
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
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')

# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
    [boxes, scores, classes, num_detections],
    feed_dict={image_tensor: image_np_expanded})

# Visualization of the results of a detection.
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=8)


return image_np
like image 456
user31562 Avatar asked Sep 14 '26 03:09

user31562


2 Answers

After some research this is what i came up with

final_score = np.squeeze(scores)    
    count = 0
    for i in range(100):
        if scores is None or final_score[i] > 0.5:
                count = count + 1
    print('cpunt',count)
    printcount =0;
    for i in classes[0]:
          printcount = printcount +1
          print(category_index[i]['name'])

          if(printcount == count):
                break

this will print all the detected objects , if you want to return it u can add it to some variable and return.

if you only want to print the detected objects add print(class_name) in the visualization_utils.py file inside the util folder

if not agnostic_mode:
          if classes[i] in category_index.keys():
            class_name = category_index[classes[i]]['name']

            **print(class_name)** --> this line 
          else:
like image 62
sajanthomas01 Avatar answered Sep 16 '26 17:09

sajanthomas01


    classes=output_dict['detection_classes']
    boxes =output_dict['detection_boxes']
    scores = output_dict['detection_scores']

    for i in range(min(max_boxes_to_draw, boxes.shape[0])):
          if scores is None or scores[i] > min_score_thresh :
              if classes[i] in category_index.keys():
                  class_name = category_index[classes[i]]['name']
                  print(class_name)   

When i gave max_boxes_to_draw=20 min_score_thresh=0.5 (these are default), it worked.

You can find this piece of code in visualization_utils.py file.

like image 42
Can Aras Avatar answered Sep 16 '26 17:09

Can Aras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!