如何将图像从 firebase 存储发送到 Microsoft Emotion API(认知服务)

How to send image from firebase storage to Microsoft Emotion API (Cognitive Service)

我正在将上传到应用程序的图像存储在 firebase 存储中。然后我需要检索图像并将其发送到 Microsoft 的认知服务(情感 API)。我尝试将图像的下载 URL 发送到 API 但它没有用 - 它给我一个 400 错误。如何将图像从 firebase 存储发送到 Emotion API.

下面是将上传的文件存储到 firebase 存储的代码...我需要继续此代码段末​​尾的代码。

 download_photo_btn.addEventListener("click", function(e) {
    var user = firebase.auth().currentUser;
    var uid;

    if (user != null) {
    uid = user.uid;  // The user's ID, unique to the Firebase project. Do NOT use
                     // this value to authenticate with your backend server, if
                     // you have one. Use User.getToken() instead.
    }
    var snap = takeSnapshot();
    var blob = dataURItoBlob(snap);

    // Create a root reference
    var storageRef = firebase.storage().ref();

    // Initial UID for images
    var selfieID = 0;

    // Create a reference to 'mountains.jpg'
    var selfieRef = storageRef.child(uid + '-' + selfieID++ + '.png');

    // Create a reference to 'images/mountains.jpg'
    var selfieImagesRef = storageRef.child('/selfies/' + uid  + '-' + selfieID++ + '.png');


    // While the file names are the same, the references point to different files
    selfieRef.name === selfieImagesRef.name            // true
    selfieRef.fullPath === selfieImagesRef.fullPath    // false

    // send image file to firebase storage
    var file = blob; 
    var uploadTask = selfieImagesRef.put(file);
  });

您有两个选择:

  • 既然你已经有了它,你可以将相同的 blob 上传到 Emotion API。这确实意味着您将两次上传图像数据,一次到 Firebase,一次到 Microsoft。
  • 您可以获得一个 public URL 作为您的存储项目。 This is the relevant documentation. Note that you may need to adjust the permission 的存储空间供认知服务访问图像。

这里有一些额外的信息:firebase storage - getting image URL