<commit_before>#  (C) Catchoom Technologies S.L.
#  Licensed under the MIT license.
#  https://github.com/catchoom/craftar-python/blob/master/LICENSE
#  All warranties and liabilities are disclaimed.

""" Provides the function for visual recogn. with CraftAR's API.

Before a single query request is sent, the image is loaded from disk, and,
if needed, transformed (i.e. resized, converted to grayscale, and compressed
as jpeg).
"""

from PIL import Image
from io import BytesIO
from craftar import settings
import requests


def search(token, filename, embed_custom=False, embed_tracking=False,
           bbox=False, app_id=None, version=None, color=False,
           min_size=settings.DEFAULT_QUERY_MIN_SIZE, verbose=False):
    """Performs a visual recognition using CraftAR's API.

    Arguments:
      token          - Token for the target collection.
      filename       - Path to the query image file.
      embed_custom   - custom_data will be embedded.
      embed_tracking - tracking_data will be embedded.
      bbox           - Return bounding boxes.
      app_id         - Application id for signing the tracking data.
      color          - Disables grayscale conversion.
                       Often color images do not give better results.
      min_size       - Sets the shorter dimension for query resizing.
                       The default value provides good recognition
                       results and small round-trip times for most cases.
                       Recognition of objects covering <1/4 of the scene
                       may require higher values.
                       Rescaling can be disabled by using using min_size = -1.
      verbose        - Shows all image transformations performed to the query.
    """
    image = _prepare_image(filename, color, min_size, verbose)

    response = requests.post(
        url="%s/%s/search" % (settings.RECOGNITION_HOSTNAME,
                              settings.RECOGNITION_API_VERSION),
        headers={'User-Agent': settings.USER_AGENT},
        data={
            'token': token,
            'embed_custom': embed_custom and 'true' or 'false',
            'embed_tracking': embed_tracking and 'true' or 'false',
            'bbox': bbox and 'true' or 'false',
            'app_id': app_id,
            'version': version,
        },
        files={'image': image},
    )

    return response.json()


def sync(token, app_id, version, bundled=True, tag=None):
    response = requests.post(
        url="%s/%s/sync" % (settings.RECOGNITION_HOSTNAME,
                            settings.RECOGNITION_API_VERSION),
        headers={'User-Agent': settings.USER_AGENT},
        data={
            'token': token,
            'app_id': app_id,
            'version': version,
            'bundled': bundled and 'true' or 'false',
            'tag': tag,
        },
    )
<commit_msg>Add strategy parameter to search<commit_after>#  (C) Catchoom Technologies S.L.
#  Licensed under the MIT license.
#  https://github.com/catchoom/craftar-python/blob/master/LICENSE
#  All warranties and liabilities are disclaimed.

""" Provides the function for visual recogn. with CraftAR's API.

Before a single query request is sent, the image is loaded from disk, and,
if needed, transformed (i.e. resized, converted to grayscale, and compressed
as jpeg).
"""

from PIL import Image
from io import BytesIO
from craftar import settings
import requests


def search(token, filename, embed_custom=False, embed_tracking=False,
           bbox=False, app_id=None, strategy="none", version=None, color=False,
           min_size=settings.DEFAULT_QUERY_MIN_SIZE, verbose=False):
    """Performs a visual recognition using CraftAR's API.

    Arguments:
      token          - Token for the target collection.
      filename       - Path to the query image file.
      embed_custom   - custom_data will be embedded.
      embed_tracking - tracking_data will be embedded.
      bbox           - Return bounding boxes.
      app_id         - Application id for signing the tracking data.
      color          - Disables grayscale conversion.
                       Often color images do not give better results.
      min_size       - Sets the shorter dimension for query resizing.
                       The default value provides good recognition
                       results and small round-trip times for most cases.
                       Recognition of objects covering <1/4 of the scene
                       may require higher values.
                       Rescaling can be disabled by using using min_size = -1.
      verbose        - Shows all image transformations performed to the query.
    """
    image = _prepare_image(filename, color, min_size, verbose)

    response = requests.post(
        url="%s/%s/search" % (settings.RECOGNITION_HOSTNAME,
                              settings.RECOGNITION_API_VERSION),
        headers={'User-Agent': settings.USER_AGENT},
        data={
            'token': token,
            'embed_custom': embed_custom and 'true' or 'false',
            'embed_tracking': embed_tracking and 'true' or 'false',
            'bbox': bbox and 'true' or 'false',
            'app_id': app_id,
            'strategy': strategy,
            'version': version,
        },
        files={'image': image},
    )

    return response.json()


def sync(token, app_id, version, bundled=True, tag=None):
    response = requests.post(
        url="%s/%s/sync" % (settings.RECOGNITION_HOSTNAME,
                            settings.RECOGNITION_API_VERSION),
        headers={'User-Agent': settings.USER_AGENT},
        data={
            'token': token,
            'app_id': app_id,
            'version': version,
            'bundled': bundled and 'true' or 'false',
            'tag': tag,
        },
    )

{'fraction_non_alphanumeric': 0.0864027538726334, 'fraction_numerical': 0.0010327022375215145, 'mean_word_length': 2.1208378088077335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2723116', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1518, 'n_words': 513}