如何在google视觉api中获取image_properties的颜色百分比?

How to get the percentage of color from image_properties in google vision api?

我正在尝试使用 Google Vision API 进行颜色检测。为此,我正在使用 python 库。当我获取 image_properties 时,我得到如下结果

colors {
  color {
    red: 52.0
    green: 94.0
    blue: 96.0
  }
  score: 0.11754503101110458
  pixel_fraction: 0.04190981388092041
}

但是如何计算在这种情况下得出的百分比 "18%"

Actual Output of the Google Vision Api

我假设你已经得到了每个颜色记录的 result using Vision demo. This percentage (18%) is calculated by taking the score 并将其除以所有分数的总和:

% "percent" = score / (score1 + score2 + score3 + ... + scoreN)

将此输出转换为ProtoBuf,然后再转换为json,这将是简单的提取。

def json_to_hash_dump(vision_response):
    """
    a function defined to take a convert the 
    response from vision api to json object  transformation via protoBuff 

    Args: 
            vision_response 
    Returns: 
            json_object
    """
    from google.protobuf.json_format import MessageToJson
    json_obj = MessageToJson((vision_response._pb))
    # to dict items 
    r = json.loads(json_obj)
    return r