Class: Oslovision::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/oslovision/client.rb

Overview

Main client class for interacting with the Oslo API

Instance Method Summary collapse

Constructor Details

#initialize(token, base_url: "https://app.oslo.vision/api/v1") ⇒ Client

Initialize the Oslo API client

Parameters:

  • token (String)

    Your Oslo API authentication token

  • base_url (String) (defaults to: "https://app.oslo.vision/api/v1")

    The base URL of the Oslo API



18
19
20
21
22
23
24
# File 'lib/oslovision/client.rb', line 18

def initialize(token, base_url: "https://app.oslo.vision/api/v1")
  @token = token
  @base_url = base_url
  self.class.base_uri base_url
  self.class.headers "Authorization" => "Bearer #{token}"
  self.class.headers "User-Agent" => "oslovision-ruby/#{Oslovision::VERSION}"
end

Instance Method Details

#add_image(project_identifier, image, split: "train", status: "pending") ⇒ Hash

Add an image to a project

Parameters:

  • project_identifier (String)

    The ID of the project to add the image to

  • image (String, File, IO)

    Either a file path, file object, or URL string of the image

  • split (String) (defaults to: "train")

    The dataset split for the image (default: “train”)

  • status (String) (defaults to: "pending")

    The status of the image (default: “pending”)

Returns:

  • (Hash)

    Response containing the added image’s data

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/oslovision/client.rb', line 43

def add_image(project_identifier, image, split: "train", status: "pending")
  if image.is_a?(String) && (image.start_with?("http://") || image.start_with?("https://"))
    # Handle URL
    body = {
      url: image,
      split: split,
      status: status,
      project_identifier: project_identifier
    }
    response = self.class.post("/images", body: body)
  else
    # Handle file upload
    file_data = prepare_file_data(image)
    body = {
      image: file_data,
      split: split,
      status: status,
      project_identifier: project_identifier
    }
    response = self.class.post("/images", body: body)
  end

  handle_response(response)
end

#create_annotation(project_identifier, image_identifier, label, x0:, y0:, width_px:, height_px:) ⇒ Hash

Create a new annotation for an image

Parameters:

  • project_identifier (String)

    The ID of the project

  • image_identifier (String)

    The ID of the image to annotate

  • label (String)

    The label for the annotation

  • x0 (Float)

    The x-coordinate of the top-left corner of the bounding box

  • y0 (Float)

    The y-coordinate of the top-left corner of the bounding box

  • width_px (Float)

    The width of the bounding box in pixels

  • height_px (Float)

    The height of the bounding box in pixels

Returns:

  • (Hash)

    Response containing the created annotation’s data

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/oslovision/client.rb', line 79

def create_annotation(project_identifier, image_identifier, label, x0:, y0:, width_px:, height_px:)
  body = {
    project_identifier: project_identifier,
    image_identifier: image_identifier,
    label: label,
    x0: x0,
    y0: y0,
    width_px: width_px,
    height_px: height_px
  }

  response = self.class.post("/annotations",
                             body: body.to_json,
                             headers: { "Content-Type" => "application/json" })
  handle_response(response)
end

#download_export(project_identifier, version, output_dir: ".") ⇒ String

Download a dataset export and extract from the zip file

Parameters:

  • project_identifier (String)

    The ID of the project

  • version (Integer)

    The version number of the export

  • output_dir (String) (defaults to: ".")

    The directory to save the downloaded files (default: current directory)

Returns:

  • (String)

    The path to the downloaded export directory

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/oslovision/client.rb', line 103

def download_export(project_identifier, version, output_dir: ".")
  response = self.class.get("/exports/#{version}?project_identifier=#{project_identifier}")
  
  if response.success?
    zip_path = File.join(output_dir, "#{project_identifier}_v#{version}.zip")
    extract_path = File.join(output_dir, "#{project_identifier}_v#{version}")

    # Write the zip file
    File.open(zip_path, "wb") do |file|
      file.write(response.body)
    end

    # Extract the zip file
    extract_zip(zip_path, extract_path)
    
    # Clean up the zip file
    File.delete(zip_path)
    
    extract_path
  else
    handle_response(response)
  end
end

#test_apiHash

Test if the API is up and running and the token is valid

Returns:

  • (Hash)

    Response from the API test endpoint

Raises:



30
31
32
33
# File 'lib/oslovision/client.rb', line 30

def test_api
  response = self.class.get("/test")
  handle_response(response)
end