Module: JPSClient::API::ImageSearch

Included in:
Client
Defined in:
lib/jpsclient/api/image_search.rb

Overview

ImageSearch 相关 API 处理图片搜索相关接口

Instance Method Summary collapse

Instance Method Details

#search_image_by_image(image_url: nil, image_data: nil, limit: 10, threshold: 0.7) ⇒ Hash

通过图片搜索相似图片(以图搜图)

Parameters:

  • image_url (String) (defaults to: nil)

    图片URL(与image_data二选一)

  • image_data (String) (defaults to: nil)

    Base64编码的图片数据(与image_url二选一)

  • limit (Integer) (defaults to: 10)

    返回结果数量(可选,默认10)

  • threshold (Float) (defaults to: 0.7)

    相似度阈值,0-1之间(可选,默认0.7)

Returns:

  • (Hash)

    API响应

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jpsclient/api/image_search.rb', line 34

def search_image_by_image(image_url: nil, image_data: nil, limit: 10, threshold: 0.7)
  config = @request_config && @request_config["image_search_by_image"]
  raise JPSClient::ExceptionError, "Missing config for image_search_by_image" unless config && config["url"]

  # 验证参数
  unless image_url || image_data
    raise JPSClient::ExceptionError, "Either image_url or image_data must be provided"
  end

  path = config["url"]
  params = {
    limit: limit,
    threshold: threshold
  }
  params[:image_url] = image_url if image_url
  params[:image_data] = image_data if image_data

  return request_with_auth(:get, path, params: params)
end

#search_image_by_text(query:, limit: 10, offset: 0) ⇒ Hash

通过文本搜索相似图片

Parameters:

  • query (String)

    搜索关键词(必需)

  • limit (Integer) (defaults to: 10)

    返回结果数量(可选,默认10)

  • offset (Integer) (defaults to: 0)

    偏移量,用于分页(可选)

Returns:

  • (Hash)

    API响应

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jpsclient/api/image_search.rb', line 13

def search_image_by_text(query:, limit: 10, offset: 0)
  config = @request_config && @request_config["image_search_by_text"]
  raise JPSClient::ExceptionError, "Missing config for image_search_by_text" unless config && config["url"]

  path = config["url"]
  params = {
    query: query,
    limit: limit,
    offset: offset
  }

  return request_with_auth(:get, path, params: params)
end