productAI

Start With SDK

It is recommended to use ProductAI via SDK because it offers higher developer productivity. Currently four programming language SDKs are officially supported: Python, PHP, Java and C#. The code for each SDK is available as open source on Github. We accept issues and pull requests from the community. If an SDK of your preferred programming language is not available yet, please feel free to propose it to our team at client-service@productai.com.

Python SDK

Please install our SDK via pypi

pip install productai

Source Code

Python 2.7/3.5/3.6 Supported

PHP SDK

Please install our SDK via composer

composer require malong/productai

Source Code

PHP 5.6+ Supported

Java SDK

Please add the JAR package into your project

Source Code

JDK 1.6, 1.8 Supported

.Net SDK

Please install our SDK via NuGet

Install-Package MalongTech.ProductAI.API

Source Code

.Net 4.0+ Supported

Create First Search Service

ProductAI requires an access_key_id and secret_key for service authentication. To help illustrate usage, the following sample code creates a service client with this information. To obtain your keys, please read how to get your access_key_id

Python

PHP

Java

C#

from productai import Client

# create a client instance with access_key_id and secret_key
cli = Client(ACCESS_KEY_ID, SECRET_KEY)
use ProductAI;

$product_ai = new ProductAI\API(ACCESS_KEY_ID, SECRET_KEY, 'zh-Hans');
import cn.productai.api.core.*;
import cn.productai.api.core.enums.*;
import cn.productai.api.pai.entity.service.*;
import cn.productai.api.pai.entity.dataset.*;

// initialize a client
IProfile profile = new DefaultProfile();
profile.setAccessKeyId(ACCESS_KEY_ID);
profile.setSecretKey(SECRET_KEY);
profile.setVersion("1");


IWebClient client = new DefaultProductAIClient(profile);
using MalongTech.ProductAI.Core;

IProfile profile = new DefaultProfile
{
Version = "1",
AccessKeyId = ACCESS_KEY_ID,
SecretKey = SECRET_KEY
};
var client = new DefaultProductAIClient(profile);

One of the important concepts of ProductAI is the image set; conceptually, it is like a data table. The image set contains an image, which is conceptually like a record in the data table.

Python

PHP

Java

C#

# create a new image set
api = cli.get_image_set_creating_api()
resp = api.create_image_set(name=SET_NAME, description=SET_DESC)

# parse image set id
from json import loads
image_set = loads(resp.content)
image_set_id = image_set['id']
$result = $product_ai->createImageSet($name=SET_NAME, $description=SET_DESC);
$image_set_id = $result['id']
// set data set name and description
CreateDataSetRequest request = new CreateDataSetRequest(SET_NAME, SET_DESC);

// get response via api
CreateDataSetResponse response = client.getResponse(request);
// parse image set id
String image_set_id = response.getDataSetId();
var request = new CreateDataSetRequest
{
Name = SET_NAME,
Description = SET_DESC
};

var response = client.GetResponse(request);
var dataSetId = response.DataSetId;

In the following sample, we add an image, which is the indexed object used in search. Add as many images to the image set as you’d like to fulfill your custom image search engine requirements.

Python

PHP

Java

C#

# create an image set api
api = cli.get_image_set_api(IMAGE_SET_ID)
api.add_image('https://url/of/your/query.jpg')
$result = $product_ai->addImageToSet($image_set_id=IMAGE_SET_ID, 'https://url/of/your/query.jpg');
DataSetSingleAddByImageUrlRequest request = new DataSetSingleAddByImageUrlRequest(IMAGE_SET_ID);
request.setImageUrl("https://url/of/your/query.jpg");
DataSetModifyResponse response = client.getResponse(request);
var request = new DataSetSingleAddByImageUrlRequest(IMAGE_SET_ID)
{
ImageUrl = "https://url/of/your/query.jpg"
};
var response = client.GetResponse(request);

Create a new image search service on the image set, tuned to a scenario, such as fashion.

Python

PHP

Java

C#

# create an image set api
api = cli.get_image_set_api(IMAGE_SET_ID)

# create a search service based on the provided image set
# to set the param, see 'supported scenarios' in search docs
resp = api.create_service(name='NAME', scenario='fashion_5_6')

# parse the service id, representing your custom search engine,
# the response also contains indexing status and other info.
from json import loads
service = loads(resp.content)
service_id = service['id']
$result = $product_ai->createService($image_set_id=IMAGE_SET_ID, $name='NAME', $scenario='fashion_5_6');
$service_id = $result['id']
// setup a search request object
CreateSearchServiceRequest request = new CreateSearchServiceRequest(IMAGE_SET_ID, "NAME", SearchScenario.fashion_5_6);
// get response via api
CreateSearchServiceResponse response = client.getResponse(request);
// parse service_id
String service_id = response.getServiceId();
var request = new CreateSearchServiceRequest
{
Name = "NAME",
Scenario = SearchScenario.fashion_5_6,
DataSetId = IMAGE_SET_ID
};

var response = client.GetResponse(request);
var serviceId = response.ServiceId;

Send a query to your custom image search service.

Python

PHP

Java

C#

# create a search api via service_id
api = cli.get_image_search_api(SERVICE_ID)

# based on an image query, find the similar results in your index
resp = api.query(image_url='http://url/of/your/query.jpg')

# enumerate the results, obtaining each result image url,
# additionally, can obtain similarity score and other info
from json import loads
results = loads(resp.content)

for result in results['results']:
url = result['url']
score = results['score']
$response = $product_ai->searchImage('search', SERVICE_ID, 'http://url/of/your/query.jpg');

foreach ($response['results'] as $value) {
url = $value['url']
score = $value['score']
}
ImageSearchByImageUrlRequest request = new ImageSearchByImageUrlRequest(SERVICE_ID);
request.setUrl("http://url/of/your/query.jpg");

ImageSearchResponse response = client.getResponse(request);
for (SearchResult r : response.getResults()) {
url = r.getUrl();
score = r.getScore();
}
var request = new ImageSearchByImageUrlRequest(SERVICE_ID)
{
Url = "http://url/of/your/query.jpg"
};

var response = client.GetResponse(request);
foreach (var r in response.Results) {
url = r.Url
score = r.Score
}