I am currently doing image processing in python and tensorflow want to create a for loop python code for a specified folder This is the code for the specific folder
import pathlib
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('C:/Object_detection/models-master/research/object_detection/test_images')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS
I want to create a for loop in this part of my image processing so that it can output multiple images without right click save as in the jupyter notebook
def show_inference(model, image_path):
image_np = np.array(Image.open(image_path))
output_dict = run_inference_for_single_image(model, image_np)
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_reframed', None),
use_normalized_coordinates=True,
line_thickness=8)
display(Image.fromarray(image_np))
CodePudding user response:
Kind of difficult to tell what you're asking for here, but this is a method that would display n
images sampled from the directory. The random sampling is done with numpy.random
's choice
function.
def show_n_inferences(model, image_paths=TEST_IMAGE_PATHS, n=10):
sampled_paths = np.random.choice(image_paths, size=n)
for image_path in sampled_paths:
image_np = np.array(Image.open(image_path))
output_dict = run_inference_for_single_image(model, image_np)
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_reframed', None),
use_normalized_coordinates=True,
line_thickness=8)
display(Image.fromarray(image_np))