【发布时间】:2021-06-11 01:04:30
【问题描述】:
我正在使用带有 Swift 的 Xcode 12.4 中的 Firebase Cloud Functions API 编写 ImageRecognizer,如下所示:
import Firebase
import UIKit
import Foundation
class ImageRecognizer {
let imageName: String
lazy var functions = Functions.functions()
init(imageName: String) {
self.imageName = imageName
}
func recognize() {
print("RECOGNIZING")
if let userImage = UIImage(named: imageName) {
print("IMAGE VALID")
guard let imageData = userImage.jpegData(compressionQuality: 1.0) else { return }
print("IMAGE DATA VALID")
let base64encodedImage = imageData.base64EncodedString()
let requestData = [
"image": ["content": base64encodedImage],
"features": ["type": "TEXT_DETECTION"],
"imageContext": ["languageHints": ["sa"]]
]
functions.httpsCallable("annotateImage").call(requestData) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
print("ERROR \(message), CODE \(code), DETAILS \(details)")
}
print("RESULT \(result)")
}
guard let annotation = (result?.data as? [String: Any])?["fullTextAnnotation"] as? [String: Any] else { return }
print("%nComplete annotation:")
let text = annotation["text"] as? String ?? ""
print("%n\(text)")
}
}
}
}
我在index.js中的云函数如下:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.annotateImage = void 0;
const functions = require("firebase-functions");
const vision_1 = require("@google-cloud/vision");
const client = new vision_1.default.ImageAnnotatorClient();
// This will allow only requests with an auth token to access the Vision
// API, including anonymous ones.
// It is highly recommended to limit access only to signed-in users. This may
// be done by adding the following condition to the if statement:
// || context.auth.token?.firebase?.sign_in_provider === 'anonymous'
//
// For more fine-grained control, you may add additional failure checks, ie:
// || context.auth.token?.firebase?.email_verified === false
// Also see: https://firebase.google.com/docs/auth/admin/custom-claims
exports.annotateImage = functions.https.onCall(async (data, context) => {
console.log("DATA: " + data);
if (!context.auth) {
throw new functions.https.HttpsError("unauthenticated", "annotateImage must be called while authenticated.");
}
try {
return await client.annotateImage(JSON.parse(data));
}
catch (e) {
throw new functions.https.HttpsError("internal", e.message, e.details);
}
});
JSON.parse(data) 部分不起作用 - 它返回错误:
认识到 图片有效 图像数据有效 2021-03-13 07:57:37.915895+0530 ImageReader[10575:10270760] [] nw_protocol_get_quic_image_block_invoke dlopen libquic 失败 错误 JSON 中位置 1 的意外标记 o,代码可选(__C.FIRFunctionsErrorCode),详细信息无 结果无
即使我将任何其他字典更改为我的 requestData,JSON 仍然无法通过。有谁知道如何从 iOS 正确调用 Firebase 云功能?
【问题讨论】:
-
尝试使用可编码和可解码?
-
谢谢 - 试过了,它给了我更多的想法来解决这个问题。会保留它!
标签: swift firebase firebase-authentication google-cloud-functions google-vision