ios AWSRekognition从图像中检测颜色总是返回nil

hfwmuf9z  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(121)

我正在使用AWSRekognition进行图像标记,一切都很好,我想获取图像颜色,我试图使用下面的代码来获取主颜色,但它总是返回nil

print("Image CSS Color : \(String(describing: label.instances?.first?.dominantColors?.first?.cssColor))")

print("Image Hex Color : \(String(describing: label.instances?.first?.dominantColors?.first?.hexCode))")

 print("Image Simplified Color : \(String(describing: label.instances?.first?.dominantColors?.first?.simplifiedColor))")

  print("Image Background Color : \(String(describing: response.imageProperties?.background))")
  print("Image Forground Color : \(String(describing: response.imageProperties?.foreground))")

这里simplifiedColor,Background,Foreground颜色属性总是返回nil,有人能告诉我有没有什么方法可以通过使用AWSRekognition来检测平均颜色。

xuo3flqw

xuo3flqw1#

最后我得到了这个问题的解决方案,我们需要在请求中设置feature属性。代码就像下面的请求。

let rekognition = AWSRekognition.default()
    let request = AWSRekognitionDetectLabelsRequest()
    request?.image = AWSRekognitionImage()
    let data = image.jpegData(compressionQuality: 0.2)
    let imageSizeInMB = Double(data!.count) / 1_000_000.0
    print("Image size: \(imageSizeInMB) MB")
    request?.image?.bytes = data
    request?.minConfidence = 25
    
    **request?.features = ["GENERAL_LABELS", "IMAGE_PROPERTIES"]**
    rekognition.detectLabels(request!) { (response, error) in
        if let error = error {
            print("Error: \(error.localizedDescription)")
        } else if let response = response {
            print("Detected Labels:")
            for label in response.labels! {
                print("Image CSS Color : \(String(describing: label.instances?.first?.dominantColors?.first?.cssColor))")
                print("Image Hex Color : \(String(describing: label.instances?.first?.dominantColors?.first?.hexCode))")
                print("Image Simplified Color : \(String(describing: label.instances?.first?.dominantColors?.first?.simplifiedColor))")
                print("\(label.name!) : \(label.confidence!.floatValue)")
            }
            
            DispatchQueue.main.async {
                //  print(response.labels!.map({"\($0.name!) : \($0.confidence!.floatValue)"}).joined(separator: "\n"))
                let alert = UIAlertController(title: "Response Data", message: "\(response.labels?.description ?? "")", preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
            print("Image Background Color : \(String(describing: response.imageProperties?.background))")
            print("Image Forground Color : \(String(describing: response.imageProperties?.foreground))")
        }
}

相关问题