从同一文件夹加载几张图像

从同一文件夹加载几张图像

我想从同一文件夹加载几张图像。但是,以下代码会产生错误:

(TypeError Traceback (most recent call last)
<ipython-input-22-a30c12347c11> in <module>
2 import glob
3
----> 4 image_list = map(Image.open, glob('/Users/name/images/*.jpg'))
5
6 object_detection_api(image_list, rect_th=2, text_th=1, text_size=1)

TypeError: 'module' object is not callable)


我已经检查了一些故障排除页面,但是给定的通知不断出现。我必须在代码中修改什么?

from PIL import Image
import glob

image_list = map(Image.open, glob('/Users/name/images/*.jpg'))

object_detection_api(image_list, rect_th=2, text_th=1, text_size=1)


根据要求,我也在下面粘贴了object_detection_api的代码:

def object_detection_api(img_path, threshold=0.7, rect_th=3, text_size=3, text_th=3):
  """
  object_detection_api
    parameters:
      - img_path - path of the input image
      - threshold - threshold value for prediction score
      - rect_th - thickness of bounding box
      - text_size - size of the class label text
      - text_th - thichness of the text
    method:
      - prediction is obtained from get_prediction method
      - for each prediction, bounding box is drawn and text is written
        with opencv
      - the final image is displayed
  """
  boxes, pred_cls = get_prediction(img_path, threshold)
  img = cv2.imread(img_path)
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  for i in range(len(boxes)):
    cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th)
    cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th)
  plt.figure(figsize=(20,30))
  plt.imshow(img)
  plt.xticks([])
  plt.yticks([])
  plt.show()

最佳答案

就像是

import os
ls = [x for x in os.listdir('/Users/name/images/') if x.endswith('.jpg')]
im_list = ['/Users/name/images/'+x for x in ls]
for img_path in im_list:
   object_detection_api(img_path)


可能为您工作。

请记住,map()filter是生成器,如果需要热切评估它们,可以对它们调用list()以将它们放在列表中。

感谢@ Gwang-JinKim提供的所有帮助!

08-25 00:41