firebase 云函数-模糊不适当的图像

qnzebej0  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(123)

我想在上传时分析图像,如果内容被认为是不合适的,则对其进行模糊处理。
日志this image is inappropriate工作正常,所以检测工作正常。但是我没有看到之后的任何日志,所以我猜blurImage函数不能正常工作。
我很难调试这个函数中的错误,以及为什么我看不到任何进一步的日志。

exports.controlImage = functions.storage.object().onFinalize(
  async (object) => {
    const bucket = storage.bucket(object.bucket);
    const file = bucket.file(object.name);

    const fileName = object.name;
    const destBucket = admin.storage().bucket(bucket);
    const file = destBucket.file(filePath); */
    

    
  
    // Check the image content using the Cloud Vision API.
    return client.safeSearchDetection(`gs://${bucket.name}/${file.name}`)
    .then((data) => {
      const safeSearch = data[0];
      functions.logger.log('SafeSearch results on image', safeSearch);
  
      if (safeSearch.safeSearchAnnotation.violence === 'VERY_LIKELY' ||
          safeSearch.safeSearchAnnotation.violence === 'POSSIBLE' ||
          safeSearch.safeSearchAnnotation.adult === 'VERY_LIKELY' ||
          safeSearch.safeSearchAnnotation.adult === 'POSSIBLE') {
            functions.logger.log("this image is innapropriate")
            blurImage(object.name, object.bucket, object.metadata);
            return null
      }
      else functions.logger.log("this image is ok")
      return null;
    });
}
)
/**
 * Blurs the given image located in the given bucket using ImageMagick.
 */
 function blurImage(filePath, bucketName, metadata) {
  const tempLocalFile = path.join(os.tmpdir(), filePath);
  const tempLocalDir = path.dirname(tempLocalFile);
  const bucket = storage.bucket(bucketName);

  // Create the temp directory where the storage file will be downloaded.
  return mkdirp(tempLocalDir).then(() => {
    functions.logger.log('Temporary directory has been created', tempLocalDir);
    // Download file from bucket.
    return bucket.file(filePath).download({destination: tempLocalFile});
  }).then(() => {
    functions.logger.log('The file has been downloaded to', tempLocalFile);
    // Blur the image using ImageMagick.
    return spawn('convert', [tempLocalFile, '-channel', 'RGBA', '-blur', '0x8', tempLocalFile]);
  }).then(() => {
    functions.logger.log('Blurred image created at', tempLocalFile);
    // Uploading the Blurred image.
    return bucket.upload(tempLocalFile, {
      destination: filePath,
      metadata: {metadata: metadata}, // Keeping custom metadata.
    });
  }).then(() => {
    functions.logger.log('Blurred image uploaded to Storage at', filePath);
    fs.unlinkSync(tempLocalFile);
    return functions.logger.log('Deleted local file', filePath);
  });
}

yacmzcpb

yacmzcpb1#

可能是因为

blurImage(object.name, object.bucket, object.metadata);
return null;

实际上并不需要等到asynchronousblurImage()函数完成后,再向云函数平台提示可以清理云函数(通过return null;)。
换句话说,在返回null之前,您需要等待此blurImage()函数完成。
由于您使用的是async,因此这里是您的CF的改编版本(未将then()async/await一起使用)(未经测试):

exports.controlImage = functions.storage.object().onFinalize(
    async (object) => {
        const bucket = storage.bucket(object.bucket);
        const file = bucket.file(object.name);

        const fileName = object.name;
        const destBucket = admin.storage().bucket(bucket);
        const file = destBucket.file(filePath); * /

        // Check the image content using the Cloud Vision API.
        const data = await client.safeSearchDetection(`gs://${bucket.name}/${file.name}`);

        const safeSearch = data[0];
        functions.logger.log('SafeSearch results on image', safeSearch);

        if (safeSearch.safeSearchAnnotation.violence === 'VERY_LIKELY' ||
            safeSearch.safeSearchAnnotation.violence === 'POSSIBLE' ||
            safeSearch.safeSearchAnnotation.adult === 'VERY_LIKELY' ||
            safeSearch.safeSearchAnnotation.adult === 'POSSIBLE') {
            functions.logger.log("this image is innapropriate")
            await blurImage(object.name, object.bucket, object.metadata);  // See the await here!!
        } else {
            functions.logger.log("this image is ok")
        }
        return null;

    });

相关问题