【问题标题】:Firebase iOS annotateImage Function returning 'Unexpected token o in JSON at position 1'Firebase iOS annotateImage 函数返回“位置 1 处 JSON 中的意外令牌 o”
【发布时间】: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


【解决方案1】:

"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)")
            }

        }
        
    }
}

【讨论】:

  • 你好...@NatashaTheRobot....我遇到了同样的问题,现在问题已经解决了,但我遇到了新问题,那就是它总是在使用字符串解析响应时抛出返回:any。 ..关于解决方案的任何想法?
猜你喜欢
  • 2023-03-27
  • 2017-10-08
  • 1970-01-01
  • 2023-04-08
  • 2021-08-28
  • 1970-01-01
相关资源
最近更新 更多