Class: DeepStack

Inherits:
Object
  • Object
show all
Includes:
CustomModel, Detection, Face, Image, Scene
Defined in:
lib/deep_stack/version.rb,
lib/deep_stack/face.rb,
lib/deep_stack/image.rb,
lib/deep_stack/scene.rb,
lib/deep_stack/detection.rb,
lib/deep_stack/deep_stack.rb,
lib/deep_stack/custom_model.rb

Overview

DeepStack API wrapper

All the methods that communicate with the server can raise an Errno::ECONNREFUSED exception if the server is down or the connection details are incorrect.

Defined Under Namespace

Modules: CustomModel, Detection, Face, Image, Scene

Constant Summary collapse

VERSION =

Returns Version of DeepStack helper libraries.

Returns:

  • (String)

    Version of DeepStack helper libraries

'1.6.1'

Instance Method Summary collapse

Methods included from CustomModel

#custom_model

Methods included from Image

#enhance_image

Methods included from Scene

#identify_scene

Methods included from Detection

#detect_objects

Methods included from Face

#delete_face, #delete_faces, #detect_faces, #face_list, #face_match, #recognize_faces, #register_face

Constructor Details

#initialize(base_url, api_key: nil, admin_key: nil, verify_mode: nil) ⇒ DeepStack

Create a deepstack object for the given server URL. HTTP connections are not made within the constructor so the deepstack object can be created against a server that’s currently down without raising an exception.

A persistent HTTP connection will be initiated on the first method request.

Examples:

DeepStack.new('http://127.0.0.1:5000')

# Using an API KEY
DeepStack.new('http://127.0.0.1:5000', api_key: 'secret', admin_key: 'supersecret')

# SSL connection with a self-signed certificate
DeepStack.new('https://localhost:443', verify_mode: OpenSSL::SSL::VERIFY_NONE)

Parameters:

  • base_url (String)

    the url to DeepStack’s server:port

  • api_key (String) (defaults to: nil)

    an optional API-KEY to use when connecting to DeepStack

  • admin_key (String) (defaults to: nil)

    an optional ADMIN-KEY to use when connecting to DeepStack

  • verify_mode (Integer) (defaults to: nil)

    sets the flags for server the certification verification at beginning of an SSL/TLS session. OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER are acceptable. The default is OpenSSL::SSL::VERIFY_PEER.



49
50
51
52
53
54
55
56
# File 'lib/deep_stack/deep_stack.rb', line 49

def initialize(base_url, api_key: nil, admin_key: nil, verify_mode: nil)
  @base_url = base_url
  @auth = { api_key: api_key, admin_key: admin_key }.select { |_k, v| v } # remove nil values
  uri = URI(base_url)
  @http = Net::HTTP.new(uri.hostname, uri.port)
  @http.use_ssl = uri.instance_of?(URI::HTTPS)
  @http.verify_mode = verify_mode if verify_mode
end

Instance Method Details

#api_post(path, *images, **args) ⇒ Hash

Make a POST request to DeepStack path target

Parameters:

  • path (String)

    to the DeepStack API URL

  • images (Array)

    zero or more images to post

  • args (Hash)

    additional named fields to post

Returns:

  • (Hash)

    if successful, the json data returned by DeepStack, nil otherwise

Raises:

  • (Net::HTTPClientException)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/deep_stack/deep_stack.rb', line 67

def api_post(path, *images, **args)
  uri = build_uri(path)
  args = @auth.merge(args)

  result = nil
  10.times do
    result = images ? post_files(uri, images.flatten, **args) : post(uri, args)
    break unless result.is_a?(Net::HTTPRedirection)

    uri.path = result['location']
  end
  raise Net::HTTPClientException, 'Too many redirections' if result.is_a?(Net::HTTPRedirection)

  process_result(result)
end

#closeObject

Close the persistent HTTP connection to DeepStack server. This should be called after a period of inactivity to close the TCP connection. Subsequent API calls will re-open the connection automatically.



88
89
90
# File 'lib/deep_stack/deep_stack.rb', line 88

def close
  @http.finish if @http&.started?
end