代码之家  ›  专栏  ›  技术社区  ›  Eoghan Casey

无法将google的vision API结果打印到应用程序屏幕

  •  1
  • Eoghan Casey  · 技术社区  · 6 年前

    我正在使用Google的Vision API识别图像中的某些特征。当徽标出现在我的终端上时,我的徽标检测工作正常,但我无法将其显示在我的应用程序屏幕上。它不断打印“找不到徽标”-下面是我的代码:

        //Get logo annotations
                let logoAnnotations: JSON = logoResponses["logoAnnotations"]
                let numLogos: Int = logoAnnotations.count
                var logos: Array<String> = []
                if numLogos > 0 {
                    var allResultsText:String = "Logos: "
                    for index in 0..<numLogos {
                        let logo = logoAnnotations[index]["logo"].stringValue
                        logos.append(logo)
                    }
                    for logo in logos {
                        if logos[logos.count - 1] != logo {
                            allResultsText += "\(logo), "
                        } else {
                            allResultsText += "\(logo)."
                        }
                    }
                    self.allResults.text = allResultsText
                } else {
                    self.allResults.text = "No logos found"
                }
            }
    

    这是我得到的JSON响应:

    [
      {
        "boundingPoly": {
          "vertices": [
            {
              "x": 210,
              "y": 139
            },
            {
              "x": 229,
              "y": 139
            },
            {
              "x": 229,
              "y": 179
            },
            {
              "x": 210,
              "y": 179
            }
          ]
        },
        "mid": "/m/04lg33",
        "score": 0.18314756,
        "description": "Ralph Lauren Corporation"
      }
    ]
    

    我如何访问徽标描述返回的值,本例为Ralph Lauren Corporation?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Eoghan Casey    6 年前

    我更正了代码中的问题。以下是供任何需要它的人使用的功能代码:

     let logoAnnotations: JSON = responses["logoAnnotations"]
                let numLogos: Int = logoAnnotations.count
                var logos: Array<String> = []
                if numLogos > 0 {
                    var logoResultsText:String = " ,"
                    for index in 0..<numLogos {
                        let logo = logoAnnotations[index]["description"].stringValue
                        logos.append(logo)
                    }
                    for logo in logos {
                        if logos[logos.count - 1] != logo {
                            logoResultsText += ", \(logo), "
                        } else {
                            logoResultsText += "\(logo)."
                        }
                    }
                    self.logoResults.text = logoResultsText
                } else {
                    self.logoResults.text = ""
                }
    

    我从logoAnnotations数组调用了错误的索引。