无法读取图像路径。 AttributeError:“列表”对象没有属性“读取”

时间:2019-08-14 06:44:39

标签: python machine-learning python-imaging-library argparse

我正在预测花朵的图像标签。我正在通过argparser将图像的路径作为参数传递。但是我遇到了以下错误。

我该如何解决?

functions-samples

thumbnailGeneratorSharp: async (object) => {
    const fileBucket = object.bucket; // The Storage bucket that contains the file.
    const filePath = object.name; // File path in the bucket.
    const contentType = object.contentType; // File content type.

    // Exit if this is triggered on a file that is not an image.
    if (!contentType.startsWith('image/')) {
      console.log('This is not an image.');
      return null;
    }

    // Get the file name.
    const fileName = path.basename(filePath);
    // Exit if the image is already a thumbnail.
    if (fileName.startsWith('thumb_')) {
      console.log('Already a Thumbnail.');
      return null;
    }

    // Download file from bucket.
    const bucket = gcs.bucket(fileBucket);

    const metadata = {
      contentType: contentType,
    };
    // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
    const thumbFileName = `thumb_${fileName}`;
    const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
    // Create write stream for uploading thumbnail
    const thumbnailUploadStream = bucket.file(thumbFilePath).createWriteStream({ metadata });

    // Create Sharp pipeline for resizing the image and use pipe to read from bucket read stream
    const pipeline = sharp();
    pipeline.resize(THUMB_MAX_WIDTH, THUMB_MAX_HEIGHT).max().pipe(thumbnailUploadStream);

    bucket.file(filePath).createReadStream().pipe(pipeline);

    return new Promise((resolve, reject) =>
      thumbnailUploadStream.on('finish', resolve).on('error', reject));
  }

1 个答案:

答案 0 :(得分:0)

在将参数nargs=1添加到参数解析器时,您可能设置了input_img
像这样:

parser.add_argument('--input_img', type=open, nargs=1)

来自docs

  

请注意,nargs = 1会产生一项的列表。这不同于   默认情况下,商品是由其自身生产的。

只需删除nargs

parser.add_argument('--input_img', type=open)
相关问题