gpt4 book ai didi

swift - Firebase iOS annotateImage 函数返回 'Unexpected token o in JSON at position 1'

转载 作者:行者123 更新时间:2023-12-04 12:23:00 28 4
gpt4 key购买 nike

我正在使用 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,CODE Optional(__C.FIRFunctionsErrorCode),DETAILS nil
结果为零
即使我更改为任何其他字典作为我的 requestData,JSON 仍然没有通过。有谁知道如何从 iOS 正确调用 Firebase 云功能?

最佳答案

"features": ["type": "TEXT_DETECTION"] 需要是一个 Swift 不喜欢的特性数组:

            let requestData = [
"image": ["content": userImage],
"features": [["type": "TEXT_DETECTION"]],
"imageContext": ["languageHints": ["sa"]]
]
有效的新代码(未完成重构):
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)")
}

}

}
}

关于swift - Firebase iOS annotateImage 函数返回 'Unexpected token o in JSON at position 1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66609620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com