【问题标题】:How to create for loop cv.write in image processing tensorflow and jupyter notebook?如何在图像处理 tensorflow 和 jupyter notebook 中创建 for loop cv.write?
【发布时间】:2021-12-11 19:58:22
【问题描述】:

我目前正在python中进行图像处理,tensorflow想为指定文件夹创建for循环python代码 这是特定文件夹的代码

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

我想在我的图像处理的这一部分创建一个for循环,以便它可以输出多张图像而无需右键单击另存为在jupyter笔记本中

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))

【问题讨论】:

  • 据我了解,您想保存当前显示的图像,并为 TEST_IMAGE_PATHS 中的每个图像都这样做,是吗?你试过什么,你卡在哪里了?
  • 是的。我想 cv2.imwrite() TEST_IMAGE_PATHS 目录中带有标签的每个图像。我想用 cv2.write() 制作一个 for 循环 python 代码。 jupyter notebook 中的显示图像是正确的。我只是想用python代码自动把所有带标签的图片保存在指定的文件夹里。

标签: python image tensorflow image-processing jupyter-notebook


【解决方案1】:

在这里很难说出您的要求,但这是一种可以显示从目录中采样的n 图像的方法。随机抽样是通过numpy.random 的choice 函数完成的。

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))

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2023-03-05
    • 2020-05-21
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多