Class: Oslovision::Client
- Inherits:
-
Object
- Object
- Oslovision::Client
- Includes:
- HTTParty
- Defined in:
- lib/oslovision/client.rb
Overview
Main client class for interacting with the Oslo API
Instance Method Summary collapse
-
#add_image(project_identifier, image, split: "train", status: "pending") ⇒ Hash
Add an image to a project.
-
#create_annotation(project_identifier, image_identifier, label, x0:, y0:, width_px:, height_px:) ⇒ Hash
Create a new annotation for an image.
-
#download_export(project_identifier, version, output_dir: ".") ⇒ String
Download a dataset export and extract from the zip file.
-
#initialize(token, base_url: "https://app.oslo.vision/api/v1") ⇒ Client
constructor
Initialize the Oslo API client.
-
#test_api ⇒ Hash
Test if the API is up and running and the token is valid.
Constructor Details
#initialize(token, base_url: "https://app.oslo.vision/api/v1") ⇒ Client
Initialize the Oslo API client
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
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
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
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_api ⇒ Hash
Test if the API is up and running and the token is valid
30 31 32 33 |
# File 'lib/oslovision/client.rb', line 30 def test_api response = self.class.get("/test") handle_response(response) end |