Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run_inference_for_single_image(image, graph) - Tensorflow, object detection

In reference to object_detection_tutorial.ipynb. I am wondering if its possible to run for all the images in a directory.

Rather than writing a for loop and running a "run_inference_for_single_image(image, graph)". Is there a way to run the inference on all the images in a directory or run the inference on multiple images. link

  for f in files:
    if f.lower().endswith(('.png', '.jpg', '.jpeg')):
      image_path = files_dir + '/' + f
       .... // Read image etc.
      output_dict = run_inference_for_single_image(image_np, detection_graph)

This will create tf.session each time and i think its computationally expensive. Please correct me if i am wrong.

like image 202
BhanuKiran Avatar asked Aug 21 '26 07:08

BhanuKiran


2 Answers

As you know, 'run_inference_for_single_image' method create each time. If you wanna inference for multiple images, you should change code like,

  • Method Call

    images = []
    for f in files:
      if f.lower().endswith(('.png', '.jpg', '.jpeg')):
        image_path = files_dir + '/' + f
        image =  .... // Read image etc.
        images.append(image)
        output_dicts = run_inference_for_multiple_images(images, detection_graph)
    
  • run_inference_for_multiple_images

    def run_inference_for_multiple_images(images, grapg):
      with graph.as_default():
        with tf.Session() as sess:
          output_dicts = []
    
          for index, image in enumerate(images):
            ... same as inferencing for single image
    
             output_dicts.append(output_dict)
    
       return output_dicts
    

This code will be performed without creating tf.session each time but once.

like image 161
kihoon han Avatar answered Aug 24 '26 12:08

kihoon han


I found this tutorial from google - creating-object-detection-application-tensorflow. After looking into its github page --> object_detection_app --> app.py we only need to run detect_objects(image_path) function every single time we want to detect an object.

like image 43
BhanuKiran Avatar answered Aug 24 '26 13:08

BhanuKiran



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!