Searching by text description
You can use Yandex Search API to search through the Yandex Images
Getting started
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Get your cloud ready
To use the examples:
- Create a service account and assign the
search-api.webSearch.userrole to it. -
Get and save the service account's API key with
yc.search-api.executefor its scope.The following examples use API key authentication. Yandex Cloud ML SDK also supports IAM token and OAuth token authentication. For more information, see Authentication in Yandex Cloud ML SDK.
Note
If you are using Windows
, we recommend installing the WSL shell first and using it to proceed. -
Install Python 3.10
or higher. -
Install Python venv
to create isolated virtual environments in Python. -
Create a new Python virtual environment and activate it:
python3 -m venv new-env source new-env/bin/activate -
Use the pip
package manager to install the ML SDK library:pip install yandex-cloud-ml-sdk
-
Create a service account you will use to send requests. You can also use a Yandex account or a federated account, but a service account is a better choice for automation purposes.
-
Assign the
search-api.webSearch.userrole to the account you will use to send requests. -
Get an IAM token, which is required for authentication.
The following examples use IAM token authentication. To use a service account's API key for authentication, edit the
Authorizationheader in the query examples. For more information, see API authentication.
To use the examples, you should additionally install cURL
-
Create a service account you will use to send requests. You can also use a Yandex account or a federated account, but a service account is a better choice for automation purposes.
-
Assign the
search-api.webSearch.userrole to the account you will use to send requests. -
Get an IAM token, which is required for authentication.
The following examples use IAM token authentication. To use a service account's API key for authentication, edit the
Authorizationheader in the query examples. For more information, see API authentication.
To use the examples, you should additionally install gRPCurl
Send a search query
To run a search query:
-
Create a file named
pic-search-by-text.pyand paste the following code into it:#!/usr/bin/env python3 from __future__ import annotations from yandex_cloud_ml_sdk import YCloudML from yandex_cloud_ml_sdk.search_api import ( FamilyMode, FixTypoMode, ImageColor, ImageFormat, ImageOrientation, ImageSize, SearchType, ) import pathlib USER_AGENT = "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.112 Mobile Safari/537.36" def main() -> None: sdk = YCloudML( folder_id="<folder_ID>", auth="<API_key>", ) sdk.setup_default_logging() # you could pass any settings when creating the Search object search = sdk.search_api.image( "RU", family_mode=FamilyMode.MODERATE, # By default object configuration property values are set to None, # which corresponds to the "default" value which is # defined at the service's backend. # e.g. docs_in_group=None, ) # but you can also reconfigure the Search object at any time: search = search.configure( # These are enum-type settings, # they could be passed as strings as shown below. search_type="kk", family_mode="strict", fix_typo_mode="off", format="jpeg", size="LARGE", orientation="vertical", color="GRAYSCALE", docs_on_page=3, site="yandex.ru", user_agent=USER_AGENT, ) search = search.configure( # any enum-like option may also be passed as an explicit enum option; # this might be helpful to control and understand which values there can be search_type=SearchType.RU, family_mode=FamilyMode.STRICT, fix_typo_mode=FixTypoMode.OFF, format=ImageFormat.JPEG, size=ImageSize.LARGE, orientation=ImageOrientation.VERTICAL, color=ImageColor.GRAYSCALE, docs_on_page=5, ) search_query = input("Enter the search query: ") if not search_query.strip(): search_query = "Yandex Cloud" for i in range(5): search_result = search.run(search_query, format="xml", page=i) output_filename = ( str(pathlib.Path(__file__).parent) + "/" + "page_" + str(i) + ".xml" ) file = open(output_filename, "a") file.write(search_result.decode("utf-8")) print(f"Page {i} saved to file {output_filename}") file.close() if __name__ == "__main__": main()Where:
-
<folder_ID>: ID of the folder in which the service account was created. -
<API_key>: Service account API key you got earlier required for authentication in the API.The following examples use API key authentication. Yandex Cloud ML SDK also supports IAM token and OAuth token authentication. For more information, see Authentication in Yandex Cloud ML SDK.
You can set the search parameters in the relevant
search_api.imageclass object properties or the.configuremethod properties:Description of object properties
-
search_type: Search type. The possible values are:ru: For theRussiansearch type.tr: For theTurkishsearch type.com: For theInternationalsearch type.kk: For theKazakhsearch type.be: For theBelarusiansearch type.uz: For theUzbeksearch type.
-
family_mode: Results filtering. This is an optional parameter. The possible values are:moderate: Moderate filter (default). Adult category documents are excluded from search results unless the query explicitly targets resources of this category.none: Filtering is off. Search results include any documents regardless of their contents.strict: Family filter. Regardless of the search query, Adult category documents and documents containing profanity are excluded from search results.
-
fix_typo_mode: Search query typo correction setting. This is an optional parameter. The possible values are:on: Typo correction enabled (default). Search query typos are corrected automatically.off: Typo correction disabled. Search query typos are not corrected. The search is performed strictly as per the query.
-
format: Searching for images of specified format. This is an optional parameter. If not set, the search includes images of all formats. The possible values are: -
size: Searching for images of specified size. This is an optional parameter. If not set, the search includes images of all sizes. The possible values are:enormous: Very large images (over1600 × 1200in pixels).large: Large images (from800 × 600to1600 × 1200in pixels).medium: Medium images (from150 × 150to800 × 600in pixels).small: Small images (from32 × 32to150 × 150in pixels).tiny: Icons (up to32 × 32in pixels).wallpaper: Wallpaper images.
-
orientation: Searching for images of specified orientation. This is an optional parameter. If not set, the search includes images of any orientation. The possible values are:vertical: Vertical images.horizontal: Horizontal images.square: Square aspect ratio images.
-
color: Searching for images with specified color parameters. This is an optional parameter. If not set, the search includes images with any color parameters. The possible values are:color: Colored images.grayscale: Grayscale images.red: Images with red as the main color.orange: Images with orange as the main color.yellow: Images with yellow as the main color.green: Images with green as the main color.cyan: Images with cyan as the main color.blue: Images with blue as the main color.violet: Images with violet as the main color.white: Images with white as the main color.black: Images with black as the main color.
-
docs_on_page: Number of result groups displayed per search result page. You can specify values between1and60. This is an optional parameter. The default value is20. -
site: Searching for images only on specified website, e.g.,yandex.cloud. This is an optional parameter. If not set, the search includes all websites in the search base. -
user_agent: String containing the User-Agent header . Use this parameter to have your search results optimized for a specific device and browser, including mobile search results. This is an optional parameter. If not specified, you will get the default output.
-
-
Run the file you created:
python3 pic-search-by-text.pyDuring the execution, the code will prompt you to enter the search query text and, as a result, will save the first five pages of search results for the specified query in XML format in the current directory:
Page 0 saved to file /Users/MyUser/Desktop/page_0.xml ... Page 4 saved to file /Users/MyUser/Desktop/page_4.xml
-
Send a query and get a Base64
-encoded result:-
Create a file with the request body, e.g.,
body.json:body.json
{ "query": { "searchType": "<search_type>", "queryText": "<search_query_text>", "familyMode": "<result_filter_setting_value>", "page": "<page_number>", "fixTypoMode": "<typo_correction_mode_setting_value>" }, "imageSpec": { "format": "<image_format>", "size": "<image_size>", "orientation": "<image_orientation>", "color": "<image_color>" }, "site": "<website_domain_name>", "docsOnPage": "<results_per_page>", "folderId": "<folder_ID>", "userAgent": "<User-Agent_header>" }Description of fields
-
searchType: Search type. The possible values are:SEARCH_TYPE_RU: For theRussiansearch type.SEARCH_TYPE_TR: For theTurkishsearch type.SEARCH_TYPE_COM: For theInternationalsearch type.SEARCH_TYPE_KK: For theKazakhsearch type.SEARCH_TYPE_BE: For theBelarusiansearch type.SEARCH_TYPE_UZ: For theUzbeksearch type.
-
queryText: Search query text. The maximum length is 400 characters. -
familyMode: Results filtering. This is an optional parameter. The possible values are:FAMILY_MODE_MODERATE: Moderate filter (default). Adult category documents are excluded from search results unless the query explicitly targets resources of this category.FAMILY_MODE_NONE: Filtering is off. Search results include any documents regardless of their contents.FAMILY_MODE_STRICT: Family filter. Regardless of the search query, Adult category documents and documents containing profanity are excluded from search results.
-
page: Requested page number. This is an optional parameter. By default, the first page with search results is returned. Page numbering starts from zero (0stands for page one). -
fixTypoMode: Search query typo correction setting. This is an optional parameter. The possible values are:FIX_TYPO_MODE_ON: Typo correction enabled (default). Search query typos are corrected automatically.FIX_TYPO_MODE_OFF: Typo correction disabled. Search query typos are not corrected. The search is performed strictly as per the query.
-
format: Searching for images of specified format. This is an optional parameter. If not set, the search includes images of all formats. The possible values are: -
size: Searching for images of specified size. This is an optional parameter. If not set, the search includes images of all sizes. The possible values are:IMAGE_SIZE_ENORMOUS: Very large images (over1600 × 1200in pixels).IMAGE_SIZE_LARGE: Large images (from800 × 600to1600 × 1200in pixels).IMAGE_SIZE_MEDIUM: Medium images (from150 × 150to800 × 600in pixels).IMAGE_SIZE_SMALL: Small images (from32 × 32to150 × 150in pixels).IMAGE_SIZE_TINY: Icons (up to32 × 32in pixels).IMAGE_SIZE_WALLPAPER: Wallpaper images.
-
orientation: Searching for images of specified orientation. This is an optional parameter. If not set, the search includes images of any orientation. The possible values are:IMAGE_ORIENTATION_VERTICAL: Vertical images.IMAGE_ORIENTATION_HORIZONTAL: Horizontal images.IMAGE_ORIENTATION_SQUARE: Square aspect ratio images.
-
color: Searching for images with specified color parameters. This is an optional parameter. If not set, the search includes images with any color parameters. The possible values are:IMAGE_COLOR_COLOR: Colored images.IMAGE_COLOR_GRAYSCALE: Grayscale images.IMAGE_COLOR_RED: Images with red as the main color.IMAGE_COLOR_ORANGE: Images with orange as the main color.IMAGE_COLOR_YELLOW: Images with yellow as the main color.IMAGE_COLOR_GREEN: Images with green as the main color.IMAGE_COLOR_CYAN: Images with cyan as the main color.IMAGE_COLOR_BLUE: Images with blue as the main color.IMAGE_COLOR_VIOLET: Images with violet as the main color.IMAGE_COLOR_WHITE: Images with white as the main color.IMAGE_COLOR_BLACK: Images with black as the main color.
-
site: Searching for images only on specified website, e.g.,yandex.cloud. This is an optional parameter. If not set, the search includes all websites in the search base. -
docsOnPage: Number of result groups displayed per search result page. You can specify values between1and60. This is an optional parameter. The default value is20. -
folderId: Folder ID of the user or service account you will use for queries. -
userAgent: String containing the User-Agent header . Use this parameter to have your search results optimized for a specific device and browser, including mobile search results. This is an optional parameter. If not specified, you will get the default output.
Request body example
body.json
{ "query": { "searchType": "SEARCH_TYPE_RU", "queryText": "cats" }, "folderId": "b1gt6g8ht345********" } -
-
Send an HTTP request specifying the IAM token you got earlier and a path to the request body file:
curl \ --request POST \ --header "Authorization: Bearer <IAM_token>" \ --data "@body.json" \ "https://searchapi.api.cloud.yandex.net/v2/image/search" \ > result.jsonThe search query result will be saved to a file named
result.jsonwith a Base64-encoded XML response in therawDatafield.
-
-
Decode the result from
Base64:echo "$(< result.json)" | \ jq -r .rawData | \ base64 --decode > result.xmlThe XML response to the query will be saved to a file named
result.xml.
-
Send a query and get a Base64
-encoded result:-
Create a file with the request body, e.g.,
body.json:body.json
{ "query": { "search_type": "<search_type>", "query_text": "<search_query_text>", "family_mode": "<result_filter_setting_value>", "page": "<page_number>", "fix_typo_mode": "<typo_correction_mode_setting_value>" }, "image_spec": { "format": "<image_format>", "size": "<image_size>", "orientation": "<image_orientation>", "color": "<image_color>" }, "site": "<website_domain_name>", "docs_on_page": "<results_per_page>", "folder_id": "<folder_ID>", "user_agent": "<User-Agent_header>" }Description of fields
-
search_type: Search type. The possible values are:SEARCH_TYPE_RU: For theRussiansearch type.SEARCH_TYPE_TR: For theTurkishsearch type.SEARCH_TYPE_COM: For theInternationalsearch type.SEARCH_TYPE_KK: For theKazakhsearch type.SEARCH_TYPE_BE: For theBelarusiansearch type.SEARCH_TYPE_UZ: For theUzbeksearch type.
-
query_text: Search query text. The maximum length is 400 characters. -
family_mode: Results filtering. This is an optional parameter. The possible values are:FAMILY_MODE_MODERATE: Moderate filter (default). Adult category documents are excluded from search results unless the query explicitly targets resources of this category.FAMILY_MODE_NONE: Filtering is off. Search results include any documents regardless of their contents.FAMILY_MODE_STRICT: Family filter. Regardless of the search query, Adult category documents and documents containing profanity are excluded from search results.
-
page: Requested page number. This is an optional parameter. By default, the first page with search results is returned. Page numbering starts from zero (0stands for page one). -
fix_typo_mode: Search query typo correction setting. This is an optional parameter. The possible values are:FIX_TYPO_MODE_ON: Typo correction enabled (default). Search query typos are corrected automatically.FIX_TYPO_MODE_OFF: Typo correction disabled. Search query typos are not corrected. The search is performed strictly as per the query.
-
format: Searching for images of specified format. This is an optional parameter. If not set, the search includes images of all formats. The possible values are: -
size: Searching for images of specified size. This is an optional parameter. If not set, the search includes images of all sizes. The possible values are:IMAGE_SIZE_ENORMOUS: Very large images (over1600 × 1200in pixels).IMAGE_SIZE_LARGE: Large images (from800 × 600to1600 × 1200in pixels).IMAGE_SIZE_MEDIUM: Medium images (from150 × 150to800 × 600in pixels).IMAGE_SIZE_SMALL: Small images (from32 × 32to150 × 150in pixels).IMAGE_SIZE_TINY: Icons (up to32 × 32in pixels).IMAGE_SIZE_WALLPAPER: Wallpaper images.
-
orientation: Searching for images of specified orientation. This is an optional parameter. If not set, the search includes images of any orientation. The possible values are:IMAGE_ORIENTATION_VERTICAL: Vertical images.IMAGE_ORIENTATION_HORIZONTAL: Horizontal images.IMAGE_ORIENTATION_SQUARE: Square aspect ratio images.
-
color: Searching for images with specified color parameters. This is an optional parameter. If not set, the search includes images with any color parameters. The possible values are:IMAGE_COLOR_COLOR: Colored images.IMAGE_COLOR_GRAYSCALE: Grayscale images.IMAGE_COLOR_RED: Images with red as the main color.IMAGE_COLOR_ORANGE: Images with orange as the main color.IMAGE_COLOR_YELLOW: Images with yellow as the main color.IMAGE_COLOR_GREEN: Images with green as the main color.IMAGE_COLOR_CYAN: Images with cyan as the main color.IMAGE_COLOR_BLUE: Images with blue as the main color.IMAGE_COLOR_VIOLET: Images with violet as the main color.IMAGE_COLOR_WHITE: Images with white as the main color.IMAGE_COLOR_BLACK: Images with black as the main color.
-
site: Searching for images only on specified website, e.g.,yandex.cloud. This is an optional parameter. If not set, the search includes all websites in the search base. -
docs_on_page: Number of result groups displayed per search result page. You can specify values between1and60. This is an optional parameter. The default value is20. -
folder_id: Folder ID of the user or service account you will use for queries. -
user_agent: String containing the User-Agent header . Use this parameter to have your search results optimized for a specific device and browser, including mobile search results. This is an optional parameter. If not specified, you will get the default output.
Request body example
body.json
{ "query": { "search_type": "SEARCH_TYPE_RU", "query_text": "cats" }, "folder_id": "b1gt6g8ht345********" } -
-
Run a gRPC call specifying the IAM token you got earlier and a path to the request body file:
grpcurl \ -rpc-header "Authorization: Bearer <IAM_token>" \ -d @ < body.json \ searchapi.api.cloud.yandex.net:443 yandex.cloud.searchapi.v2.ImageSearchService/Search \ > result.jsonThe search query result will be saved to a file named
result.jsonwith a Base64-encoded XML response in therawDatafield.
-
-
Decode the result from
Base64:echo "$(< result.json)" | \ jq -r .rawData | \ base64 --decode > result.xmlThe XML response to the query will be saved to a file named
result.xml.