Nat*_*bot 6 firebase swift firebase-authentication google-vision google-cloud-functions
我正在 Xcode 12.4 中使用 Firebase Cloud Functions API 和 Swift 编写 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)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在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);
}
});
Run Code Online (Sandbox Code Playgroud)
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.FIRFun ctionsErrorCode),详细信息零结果无
即使我更改为任何其他字典作为我的 requestData,JSON 仍然不会通过。有谁知道如何从 iOS 正确调用 Firebase 云函数?
"features": ["type": "TEXT_DETECTION"] 需要是一个功能数组,这是 Swift 不喜欢的:
let requestData = [
"image": ["content": userImage],
"features": [["type": "TEXT_DETECTION"]],
"imageContext": ["languageHints": ["sa"]]
]
Run Code Online (Sandbox Code Playgroud)
有效的新代码(未完成重构):
import Firebase
import UIKit
import Foundation
class ImageRecognizer: Codable {
let imageName: String
lazy var functions = Functions.functions()
init(imageName: String) {
self.imageName = imageName
}
func recognize() {
struct data: Encodable {
let image: [String: Data]
let features = [["type": "TEXT_DETECTION"]]
let imageContext = ["languageHints": ["sa"]]
init() {
let userImage = UIImage(named: "onlytext.jpg")!
let imageData = userImage.jpegData(compressionQuality: 1.0)!
image = ["content": imageData]
}
}
let encoder = JSONEncoder()
let encodedData = try! encoder.encode(data())
let string = String(data: encodedData, encoding: .utf8)!
functions.httpsCallable("annotateImage").call(string) { (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("SUCCESS")
print("RESULT \(result?.data)")
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)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |