如何从同一文件夹中加载多个图像?

2024-03-29 01:38:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从同一个文件夹加载几个图像。但是,下面的代码产生了一个错误:

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

根据要求,我正在粘贴以下对象检测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()

Tags: pathtextrectimageapiimgsizeobject
1条回答
网友
1楼 · 发布于 2024-03-29 01:38:07

差不多

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的帮助

相关问题 更多 >