productAI

Color Analysis

Color analysis of image content

Basic Usage

Python

PHP

Java

C#

cURL

from productai import Client

cli = Client(ACCESS_KEY_ID, SECRET_KEY)

# Get color analysis service API
api = cli.get_color_analysis_api(sub_type='everything')

# Color analysis with url
resp = api.query(image='http://xxxx', granularity='major', return_type='w3c')

# Color analysis with local file
with open("zzzz.jpg", mode='rb') as search:
resp = api.query(image=search, granularity='major', return_type='w3c')
use ProductAI;
$product_ai = new ProductAI\API($access_key_id=ACCESS_KEY_ID, $secret_key=SECRET_KEY);

// Get color analysis service API, and analyze image color with url
$result = $product_ai->imageColorAnalysis($image='http://xxxx', $type='everything', $granularity='major', $return_type='w3c');

// Or analyze image color for local file
$result = $product_ai->imageColorAnalysis($image='@zzzz.jpg', $type='everything', $granularity='major', $return_type='w3c');
import java.io.File;
import cn.productai.api.core.*;
import cn.productai.api.core.enums.*;
import cn.productai.api.pai.entity.classify.*;

// Set intialize parameters for API
IProfile profile = new DefaultProfile();
profile.setAccessKeyId(ACCESS_KEY_ID);
profile.setSecretKey(SECRET_KEY);
profile.setVersion("1");

IWebClient client = new DefaultProductAIClient(profile);

// Color analysis with url
ClassifyByImageUrlRequest request = new ClassifyByImageUrlRequest("color", "_0000072");
request.setUrl("https://xxxx");

// Color analysis with local file
ClassifyByImageFileRequest request = new ClassifyByImageFileRequest("color", "_0000072");
request.setImageFile(new File("zzzz.jpg"));

request.getOptions().put("granularity", "major");
request.getOptions().put("return_type", "w3c");

ClassifyResponse response = client.getResponse(request);
String json = response.getResponseJsonString();
using System;
using MalongTech.ProductAI.Core;
using MalongTech.ProductAI.API.Entity.Color;

// Color analysis with url
var request = new ColorClassifyByImageUrlRequest()
{
Url="http://xxxx",
SubType=SubType.EveryThing,
Granularity=Granularity.Major,
ReturnType=ColorReturnType.Basic
};

// Color analysis with local file
var request = new ColorClassifyByImageFileRequest()
{
ImageFile=new System.IO.FileInfo(@".\zzzz.jpg"),
SubType=SubType.EveryThing,
Granularity=Granularity.Major,
ReturnType=ColorReturnType.CNCS
};

// Call service
var response = client.GetResponse(request);
# Call the API with url
curl -X POST \
-H 'x-ca-version: 1.0' \
-H 'x-ca-accesskeyid: ACCESS_KEY_ID' \
-d 'url=http://xxxx.jpg&granularity=major&return_type=w3c' \
https://api.productai.com/color/_0000072

# Or call the API with local file
curl -X POST \
-H 'x-ca-version: 1.0' \
-H 'x-ca-accesskeyid: ACCESS_KEY_ID' \
-F 'search=@zzzz.jpg' \
-F 'granularity=major' \
-F 'return_type=w3c' \
https://api.productai.com/color/_0000072

Note: for sub_type, please refer to Full Image Color Analysis, Outfit Color Analysis, Object Color Analysis

Request

ArgumentTypeDescriptionRequiredLimitation
urlStringURL of query image, the first argument of query methodYesMutually exclusive with search argument
searchFileContent of query image, the first argument of query methodYesMutually exclusive with url argument
granularityStringAnalysis granularityYesOne out of major, detailed, dominant. Please refer to granularity
return_typeStringType of return valueYesOne out of basic, w3c, ncs, cncs. Please refer to return_type

granularity

Take full image color analysis as an example. For the following image:
Image Sample1

OptionResultDescription
major             Return only the two main colors of the entire image
detailed                                  Return more colors in the image
dominant      Return only one main color

return_type

OptionResultDescription
basic      Blue
w3c      Deep sky blueColor module released by W3C CSS team
ncs      1050-R90BNCS color card is a color design tool from Sweden, which describes colors in the same way with eyes. The color of surface is defined in the NCS color card as a color number.
cncs       { “chroma”: “Medium”, “cncs”: “1196022”, “color”: “Violet”, “lightness”: “Medium”, “rgb”: [“107”, “160”, “206”]}CNCS is a color system developed by China Textile Information Center in collaboration with international and domestic experts and agencies, after years of careful research. The color system strives to provide fashion designers and related organizations with the most fashionable color information and color management solutions available today.

Request

Return Http Status Code 200, When Success

The service successfully processed, and returned the results.

{
"is_err": 0,
"request_id": "d54dab1a-e098-11e7-bb18-eea500941bf4",
"results": [
{
"cncs": {
"chroma": "Medium",
"cncs": "1196022",
"color": "Violet",
"lightness": "Medium",
"rgb": [
"107",
"160",
"206"
]
},
"freq": 1,
"hex": "#5BA3D3",
"rgb": [
91,
163,
211
]
}
],
"time": 0.149
}
FieldTypeDescription
is_errInteger1 means error, and 0 means success
request_idStringUnique request ID, which can be used to debug jointly with ProductAI team
resultsArrayColor analysis results
cncsDictionaryRelated result information for cncs
ncsStringRelated result information for ncs
basicStringResult inforamtion for basic color
w3cStringResult information for w3c standard
freqIntegerColor ratio
hexStringColor in hex
rgbArrayColor RGB
timeFloatTime cost in service side

Return Http Status Code Non-200, When Failed

The server encountered an error

{
"data": {
"is_err": 1,
"message": "Download image failed.",
"request_id": "224b2a28-e08f-11e7-bb18-eea500941bf4",
"time": 0.013
},
"status": 408
}
FieldTypeDescription
dataDictionaryAll information are wrapped inside the data field
statusIntegerHTTP status code
is_errInteger1 means error, and 0 means success
messageStringError details
request_idStringUnique request ID, which can be used to debug jointly with ProductAI team
timeFloatTime cost in service side in seconds

Error Code

SDK/API uses HTTP status code to indicate success or failure of an API call.

HTTP Status Codeerror_codeDescription
200N/aSuccess
400N/aArgument granularity is invalid, or argument return_type is invalid, or argument loc is invalid, or query image is empty
408N/aQuery image download failed, or query image format parsing failed, or image color analysis failed, or upstream computing service failed

Services

Currently there are three types of color analysis: full image, foreground objects and outfit. The differences among them are exlained with the following image.(granularity argument is major)
Image sample1

Analysis TypeResultDescription
Full image color analysis             Gray background, and the dark red (main color) of foreground characters are found
Outfit color analysis                    The returned color are mainly from outfits.
Object color analysis                    After removing the background color, the foreground objects are characters. So the returned colors are similar with Outfit color analysis

Full Image Color Analysis v1.0

Analyze color for the entire image, and return the color of the corresponding color system based on the input argument. The corresponding _sub_type_ argument is everything

Python

PHP

Java

C#

cURL

from productai import Client

cli = Client(ACCESS_KEY_ID, SECRET_KEY)

# Get full image color analysis service API
api = cli.get_color_analysis_api(sub_type='everything')
use ProductAI;
$product_ai = new ProductAI\API($access_key_id=ACCESS_KEY_ID, $secret_key=SECRET_KEY);

// Get full image color analysis service API
$result = $product_ai->imageColorAnalysis($image='http://xxxx', $type='everything', $granularity='xxx', $return_type='xxx');
IProfile profile = new DefaultProfile();
profile.setAccessKeyId(ACCESS_KEY_ID);
profile.setSecretKey(SECRET_KEY);
profile.setVersion("1");

IWebClient client = new DefaultProductAIClient(profile);

// Get full image color analysis service API
ClassifyByImageUrlRequest request = new ClassifyByImageUrlRequest("color", "_0000072");
request.getOptions().put("granularity", "xxx");
request.getOptions().put("return_type", "xxx");
// Get full image color analysis service API
var request = new ColorClassifyByImageUrlRequest()
{
Url="http://xxxx",
SubType=SubType.EveryThing,
Granularity=Granularity.Major,
ReturnType=ColorReturnType.Basic
};

// Call the service
var response = client.GetResponse(request);
# Get full image color analysis service API
curl -X POST \
-H 'x-ca-version: 1.0' \
-H 'x-ca-accesskeyid: ACCESS_KEY_ID' \
-d 'url=http://xxx.jpg&granularity=xxx&return_type=xxx' \
https://api.productai.com/color/_0000072

Outfit Color Analysis v1.0

Analyze color for the fashion clothing in the image by removing the background and skin color, and return the color of the corresponding color system based on the input argument. The corresponding _sub_type_ argument is _person_outfit_

Python

PHP

Java

C#

cURL

from productai import Client

cli = Client(ACCESS_KEY_ID, SECRET_KEY)

# Get outfit color analysis service API
api = cli.get_color_analysis_api(sub_type='person_outfit')
use ProductAI;
$product_ai = new ProductAI\API($access_key_id=ACCESS_KEY_ID, $secret_key=SECRET_KEY);

//Get outfit color analysis service API
$result = $product_ai->imageColorAnalysis($image='http://xxxx', $type='person_outfit', $granularity='xxx', $return_type='xxx');
IProfile profile = new DefaultProfile();
profile.setAccessKeyId(ACCESS_KEY_ID);
profile.setSecretKey(SECRET_KEY);
profile.setVersion("1");

IWebClient client = new DefaultProductAIClient(profile);

//Get outfit color analysis service API
ClassifyByImageUrlRequest request = new ClassifyByImageUrlRequest("color", "_0000074");
request.getOptions().put("granularity", "xxx");
request.getOptions().put("return_type", "xxx");
// Get outfit color analysis service API
var request = new ColorClassifyByImageUrlRequest()
{
Url="http://xxxx",
SubType=SubType.PersonOutfit,
Granularity=Granularity.Major,
ReturnType=ColorReturnType.Basic
};

// Call the service
var response = client.GetResponse(request);
# Get outfit color analysis service API
curl -X POST \
-H 'x-ca-version: 1.0' \
-H 'x-ca-accesskeyid: ACCESS_KEY_ID' \
-d 'url=http://xxx.jpg&granularity=xxx&return_type=xxx' \
https://api.productai.com/color/_0000074

Product Color Analysis v1.0

Analyze color for the foreground objects in the image by removing the background, and return the color of the corresponding color system based on the input argument. The corresponding _sub_type_ argument is foreground

Python

PHP

Java

C#

cURL

from productai import Client

cli = Client(ACCESS_KEY_ID, SECRET_KEY)

# Get foreground object color analysis service API
api = cli.get_color_analysis_api(sub_type='foreground')
use ProductAI;
$product_ai = new ProductAI\API($access_key_id=ACCESS_KEY_ID, $secret_key=SECRET_KEY);

// Get foreground object color analysis service API
$result = $product_ai->imageColorAnalysis($image='http://xxxx', $type='foreground', $granularity='xxx', $return_type='xxx');
IProfile profile = new DefaultProfile();
profile.setAccessKeyId(ACCESS_KEY_ID);
profile.setSecretKey(SECRET_KEY);
profile.setVersion("1");

IWebClient client = new DefaultProductAIClient(profile);

// Get foreground object color analysis service API
ClassifyByImageUrlRequest request = new ClassifyByImageUrlRequest("color", "_0000073");
request.getOptions().put("granularity", "xxx");
request.getOptions().put("return_type", "xxx");
// Get foreground object color analysis service API
var request = new ColorClassifyByImageUrlRequest()
{
Url="http://yyyy",
SubType=SubType.Foreground,
Granularity=Granularity.Major,
ReturnType=ColorReturnType.Basic
};

// Call the service
var response = client.GetResponse(request);
# Get foreground object color analysis service API

curl -X POST \
-H 'x-ca-version: 1.0' \
-H 'x-ca-accesskeyid: ACCESS_KEY_ID' \
-d 'url=http://xxx.jpg&granularity=xxx&return_type=xxx' \
https://api.productai.com/color/_0000073