deepstack 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/deepstack/custom.rb +3 -3
- data/lib/deepstack/detection.rb +2 -2
- data/lib/deepstack/face.rb +5 -5
- data/lib/deepstack/scene.rb +3 -3
- data/lib/deepstack/version.rb +3 -3
- data/lib/deepstack.rb +84 -86
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc0409e409422c065962aa065244f20658c764c904462935936b05c1950e771a
|
4
|
+
data.tar.gz: 074e0f2e9f309f72aaa402ef5e1b3bd469da172bbb60679a2a59b218f4a2a7ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0923fd828175f78a7bc90a40623c6ed7ec8ce8681876ab340697309c9557bdbf2551f10567b4df4dd861c2121e2cd37c62b47f06803e08f8cc0b1a5739e68d89'
|
7
|
+
data.tar.gz: b9be9f1439d69f939f2dbf66842264a750e47d8cba455ff528b7879f115f3510e45690f8ff7a804e1b23428a5313c44c147049b3bcc269db31b4887f7c03b34e
|
data/lib/deepstack/custom.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module DeepStackModule
|
4
4
|
# Custom Model
|
5
5
|
module Custom
|
6
6
|
#
|
@@ -28,9 +28,9 @@ module Deepstack
|
|
28
28
|
#
|
29
29
|
# @param [String] model custom model name
|
30
30
|
# @param [Object] image binary data or a File object
|
31
|
-
# @param [Hash] options additional fields for
|
31
|
+
# @param [Hash] options additional fields for DeepStack, e.g. min_confidence: 0.5
|
32
32
|
#
|
33
|
-
# @return [Array] if successful, an array of
|
33
|
+
# @return [Array] if successful, an array of DeepStack predictions
|
34
34
|
#
|
35
35
|
# @return [nil] if error
|
36
36
|
#
|
data/lib/deepstack/detection.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module DeepStackModule
|
4
4
|
# APIs related to object detection
|
5
5
|
module Detection
|
6
6
|
#
|
7
7
|
# Perform object detection
|
8
8
|
#
|
9
9
|
# @param [Object] image raw image data or a File object of an image file
|
10
|
-
# @param [Hash] options additional fields for
|
10
|
+
# @param [Hash] options additional fields for DeepStack, e.g. min_confidence: 0.5
|
11
11
|
#
|
12
12
|
# @return [Array] a list of predictions, or nil on error
|
13
13
|
#
|
data/lib/deepstack/face.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module DeepStackModule
|
4
4
|
# APIs related to face recognition
|
5
5
|
module Face
|
6
6
|
#
|
7
7
|
# Perform face recognition
|
8
8
|
#
|
9
9
|
# @param [Object] image binary data or a File object
|
10
|
-
# @param [Hash] options additional fields for
|
10
|
+
# @param [Hash] options additional fields for DeepStack, e.g. min_confidence: 0.5
|
11
11
|
#
|
12
|
-
# @return [Array] if successful, an array of
|
12
|
+
# @return [Array] if successful, an array of DeepStack predictions
|
13
13
|
#
|
14
14
|
# @return [nil] if error
|
15
15
|
#
|
@@ -23,9 +23,9 @@ module Deepstack
|
|
23
23
|
# Detect faces in an image
|
24
24
|
#
|
25
25
|
# @param [Object] image binary data or a File object
|
26
|
-
# @param [Hash] options additional fields for
|
26
|
+
# @param [Hash] options additional fields for DeepStack, e.g. min_confidence: 0.5
|
27
27
|
#
|
28
|
-
# @return [Array] if successful, an array of
|
28
|
+
# @return [Array] if successful, an array of DeepStack predictions
|
29
29
|
#
|
30
30
|
def detect_faces(image, **options)
|
31
31
|
target = 'vision/face/' # the URL ends with a slash
|
data/lib/deepstack/scene.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module DeepStackModule
|
4
4
|
# Scene Recognition
|
5
5
|
module Scene
|
6
6
|
#
|
7
7
|
# Return
|
8
8
|
#
|
9
9
|
# @param [Object] image binary data or a File object
|
10
|
-
# @param [Hash] options additional fields for
|
10
|
+
# @param [Hash] options additional fields for DeepStack, e.g. min_confidence: 0.5
|
11
11
|
#
|
12
|
-
# @return [Hash] if successful,
|
12
|
+
# @return [Hash] if successful, DeepStack result hash {'label' => 'scene', 'confidence' => 2.2}
|
13
13
|
#
|
14
14
|
# @return [nil] if error
|
15
15
|
def identify_scene(image, **options)
|
data/lib/deepstack/version.rb
CHANGED
data/lib/deepstack.rb
CHANGED
@@ -9,103 +9,101 @@ require_relative 'deepstack/scene'
|
|
9
9
|
require_relative 'deepstack/custom'
|
10
10
|
require_relative 'deepstack/version'
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
12
|
+
# DeepStack API
|
13
|
+
class DeepStack
|
14
|
+
include DeepStackModule::Face
|
15
|
+
include DeepStackModule::Detection
|
16
|
+
include DeepStackModule::Scene
|
17
|
+
include DeepStackModule::Custom
|
18
|
+
|
19
|
+
attr_reader :duration, :predictions, :success
|
20
|
+
|
21
|
+
# Create a deepstack object connected to the given URL
|
22
|
+
def initialize(base_url)
|
23
|
+
@base_url = base_url
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
#
|
27
|
+
# The result of the last call
|
28
|
+
#
|
29
|
+
# @return [Boolean] true if the last call was successful
|
30
|
+
#
|
31
|
+
def success?
|
32
|
+
@success == true
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
55
|
-
raise Net::HTTPClientException, 'Too many redirections' if result.is_a?(Net::HTTPRedirection)
|
56
|
-
|
57
|
-
process_result(result)
|
35
|
+
#
|
36
|
+
# Make a POST request to DeepStack path target
|
37
|
+
#
|
38
|
+
# @param [String] path to the DeepStack API URL
|
39
|
+
# @param [Array] images zero or more images to post
|
40
|
+
# @param [Hash] args additional named fields to post
|
41
|
+
#
|
42
|
+
# @return [Hash] if successful, the json data returned by DeepStack, nil otherwise
|
43
|
+
#
|
44
|
+
def api_post(path, *images, **args)
|
45
|
+
uri = build_uri(path)
|
46
|
+
|
47
|
+
result = nil
|
48
|
+
10.times do
|
49
|
+
result = images ? post_files(uri, images.flatten, **args) : post(uri, args)
|
50
|
+
break unless result.is_a?(Net::HTTPRedirection)
|
51
|
+
|
52
|
+
uri.path = result['location']
|
58
53
|
end
|
54
|
+
raise Net::HTTPClientException, 'Too many redirections' if result.is_a?(Net::HTTPRedirection)
|
59
55
|
|
60
|
-
|
56
|
+
process_result(result)
|
57
|
+
end
|
61
58
|
|
62
|
-
|
63
|
-
URI.join(@base_url, '/v1/', path)
|
64
|
-
end
|
59
|
+
private
|
65
60
|
|
66
|
-
|
67
|
-
|
68
|
-
|
61
|
+
def build_uri(path)
|
62
|
+
URI.join(@base_url, '/v1/', path)
|
63
|
+
end
|
69
64
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
req.set_form(form_data, 'multipart/form-data')
|
74
|
-
Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
|
75
|
-
end
|
65
|
+
def post(uri, **args)
|
66
|
+
Net::HTTP.post_form(uri, args)
|
67
|
+
end
|
76
68
|
|
77
|
-
|
78
|
-
|
79
|
-
|
69
|
+
def post_files(uri, *images, **args)
|
70
|
+
form_data = combine_images_and_args(images.flatten, **args)
|
71
|
+
req = Net::HTTP::Post.new(uri)
|
72
|
+
req.set_form(form_data, 'multipart/form-data')
|
73
|
+
Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
|
74
|
+
end
|
80
75
|
|
81
|
-
|
82
|
-
|
83
|
-
|
76
|
+
def combine_images_and_args(*images, **args)
|
77
|
+
stringify_keys(args).concat(image_form_data(images.flatten))
|
78
|
+
end
|
84
79
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
# For multiple images, the field names will be 'image1', 'image2', ...
|
89
|
-
#
|
90
|
-
# @param [Array<Object>] images an array of raw image data or a File object
|
91
|
-
#
|
92
|
-
# @return [Array] the image entries for set_form
|
93
|
-
#
|
94
|
-
def image_form_data(*images)
|
95
|
-
images = images.flatten
|
96
|
-
return [image_entry('image', images.first)] if images.length == 1
|
97
|
-
|
98
|
-
images.map.with_index(1) { |image, i| image_entry("image#{i}", image) }
|
99
|
-
end
|
80
|
+
def stringify_keys(hash)
|
81
|
+
hash.map { |k, v| [k.to_s, v] }
|
82
|
+
end
|
100
83
|
|
101
|
-
|
102
|
-
|
103
|
-
|
84
|
+
#
|
85
|
+
# Return an array of image entries for form data.
|
86
|
+
# The field name is 'image' for a single image
|
87
|
+
# For multiple images, the field names will be 'image1', 'image2', ...
|
88
|
+
#
|
89
|
+
# @param [Array<Object>] images an array of raw image data or a File object
|
90
|
+
#
|
91
|
+
# @return [Array] the image entries for set_form
|
92
|
+
#
|
93
|
+
def image_form_data(*images)
|
94
|
+
images = images.flatten
|
95
|
+
return [image_entry('image', images.first)] if images.length == 1
|
96
|
+
|
97
|
+
images.map.with_index(1) { |image, i| image_entry("image#{i}", image) }
|
98
|
+
end
|
104
99
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
100
|
+
def image_entry(name, image)
|
101
|
+
[name, image].tap { |result| result << { filename: "#{name}.jpg" } unless image.instance_of? File }
|
102
|
+
end
|
103
|
+
|
104
|
+
def process_result(result)
|
105
|
+
@result = result.is_a?(Net::HTTPSuccess) ? JSON.parse(result.body) : nil
|
106
|
+
%w[success duration predictions].each { |attrib| instance_variable_set("@#{attrib}", @result&.dig(attrib)) }
|
107
|
+
@result
|
110
108
|
end
|
111
109
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deepstack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Tanagra
|
@@ -48,5 +48,5 @@ requirements: []
|
|
48
48
|
rubygems_version: 3.3.7
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
|
-
summary: A Ruby wrapper for
|
51
|
+
summary: A Ruby wrapper for DeepStack API
|
52
52
|
test_files: []
|