embulk-filter-google_vision_api 0.1.0 → 0.2.1
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89b4d7b05b737af39f0a639df19f18eed4980666
|
4
|
+
data.tar.gz: d7f16dc85867f7eab0330587d7b54ca550f113c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e3a3df9c004434fe12d976cd8cf32521643e098598bce620c31d2afa370796744463b0e60032bb12386dcc307fb99156b0e8cd1b2be68f2e6349678d28d54e6
|
7
|
+
data.tar.gz: 3ceffe09715df5d3dd26b0bbcc2ea6e3a069080384b5f86a5e741223769894b90771ba954a262526f653f85d62d53fa4d3b6d744896a445de81653273084d22e
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
Gem::Specification.new do |spec|
|
3
3
|
spec.name = "embulk-filter-google_vision_api"
|
4
|
-
spec.version = "0.1
|
4
|
+
spec.version = "0.2.1"
|
5
5
|
spec.authors = ["toyama0919"]
|
6
6
|
spec.summary = "Google Vision Api filter plugin for Embulk"
|
7
7
|
spec.description = "Google Vision Api filter plugin for Embulk. Very easy image recognition."
|
@@ -1,16 +1,9 @@
|
|
1
|
-
|
2
|
-
require "net/http"
|
3
|
-
require "uri"
|
4
|
-
require "openssl"
|
5
|
-
require "base64"
|
6
|
-
require "pp"
|
1
|
+
require_relative "google_vision_api/vision_client"
|
7
2
|
|
8
3
|
module Embulk
|
9
4
|
module Filter
|
10
|
-
|
11
5
|
class GoogleVisionApi < FilterPlugin
|
12
6
|
Plugin.register_filter("google_vision_api", self)
|
13
|
-
ENDPOINT_PREFIX = "https://vision.googleapis.com/v1/images:annotate"
|
14
7
|
|
15
8
|
def self.transaction(config, in_schema, &control)
|
16
9
|
task = {
|
@@ -32,16 +25,10 @@ module Embulk
|
|
32
25
|
end
|
33
26
|
|
34
27
|
def init
|
35
|
-
@uri = URI.parse("#{ENDPOINT_PREFIX}?key=#{task['google_api_key']}")
|
36
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
37
|
-
@http.use_ssl = true
|
38
|
-
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
39
|
-
@post = Net::HTTP::Post.new(@uri.request_uri, initheader = {'Content-Type' =>'application/json'})
|
40
28
|
@image_path_key_name = task['image_path_key_name']
|
41
|
-
@out_key_name = task['out_key_name']
|
42
29
|
@delay = task['delay']
|
43
30
|
@image_num_per_request = task['image_num_per_request']
|
44
|
-
@
|
31
|
+
@client = VisionClient.new(features: task['features'], google_api_key: task['google_api_key'])
|
45
32
|
end
|
46
33
|
|
47
34
|
def close
|
@@ -54,29 +41,14 @@ module Embulk
|
|
54
41
|
|
55
42
|
record_groups.each do |records|
|
56
43
|
requests = []
|
57
|
-
records.
|
58
|
-
|
59
|
-
image: {},
|
60
|
-
features: @features
|
61
|
-
}
|
62
|
-
image_body = get_image_body(record)
|
63
|
-
request[:image][:content] = Base64.encode64(image_body)
|
64
|
-
requests << request
|
44
|
+
images = records.map do |record|
|
45
|
+
record[@image_path_key_name]
|
65
46
|
end
|
66
|
-
body = {
|
67
|
-
requests: requests
|
68
|
-
}
|
69
|
-
@post.body = body.to_json
|
70
|
-
Embulk.logger.debug "request body => #{@post.body}"
|
71
47
|
|
72
|
-
|
73
|
-
@http.start do |h|
|
74
|
-
response = h.request(@post)
|
75
|
-
response_hash = JSON.parse(response.body)
|
76
|
-
end
|
48
|
+
response = @client.request(images)
|
77
49
|
records.each_with_index do |record, i|
|
78
|
-
recognized =
|
79
|
-
Embulk.logger.warn "Error image => [#{record[@image_path_key_name]}] #{recognized}" if
|
50
|
+
recognized = response.key?("error") ? response : response['responses'][i]
|
51
|
+
Embulk.logger.warn "Error image => [#{record[@image_path_key_name]}] #{recognized}" if response.key?("error")
|
80
52
|
page_builder.add(record.values + [recognized])
|
81
53
|
end
|
82
54
|
|
@@ -87,16 +59,6 @@ module Embulk
|
|
87
59
|
def finish
|
88
60
|
page_builder.finish
|
89
61
|
end
|
90
|
-
|
91
|
-
private
|
92
|
-
def get_image_body(record)
|
93
|
-
image_path = record[@image_path_key_name]
|
94
|
-
if image_path =~ /https?\:\/\//
|
95
|
-
Net::HTTP.get_response(URI.parse(image_path)).body rescue ""
|
96
|
-
else
|
97
|
-
File.read(image_path) rescue ""
|
98
|
-
end
|
99
|
-
end
|
100
62
|
end
|
101
63
|
end
|
102
64
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "json"
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
require "openssl"
|
5
|
+
require "base64"
|
6
|
+
|
7
|
+
module Embulk
|
8
|
+
module Filter
|
9
|
+
class GoogleVisionApi < FilterPlugin
|
10
|
+
class VisionClient
|
11
|
+
ENDPOINT = "https://vision.googleapis.com/v1/images:annotate"
|
12
|
+
def initialize(features:, google_api_key:)
|
13
|
+
uri = URI.parse("#{ENDPOINT}?key=#{google_api_key}")
|
14
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
15
|
+
@http.use_ssl = true
|
16
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
17
|
+
@post = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
|
18
|
+
@features = features
|
19
|
+
end
|
20
|
+
|
21
|
+
def request(images)
|
22
|
+
@post.body = get_body(images).to_json
|
23
|
+
Embulk.logger.debug "request body => #{@post.body}"
|
24
|
+
|
25
|
+
@http.start do |h|
|
26
|
+
response = h.request(@post)
|
27
|
+
JSON.parse(response.body)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def get_body(images)
|
33
|
+
{
|
34
|
+
requests: get_requests(images)
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_requests(images)
|
39
|
+
images.map do |image_path|
|
40
|
+
get_request(image_path)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_request(image_path)
|
45
|
+
request = {
|
46
|
+
image: Hash.new{|h,k| h[k] = {}},
|
47
|
+
features: @features
|
48
|
+
}
|
49
|
+
if image_path =~ /gs\:\/\//
|
50
|
+
request[:image][:source][:gcs_image_uri] = image_path
|
51
|
+
else
|
52
|
+
image_body = get_image_body(image_path)
|
53
|
+
request[:image][:content] = Base64.encode64(image_body)
|
54
|
+
end
|
55
|
+
request
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_image_body(image_path)
|
59
|
+
if image_path =~ /https?\:\/\//
|
60
|
+
Net::HTTP.get_response(URI.parse(image_path)).body rescue ""
|
61
|
+
else
|
62
|
+
File.read(image_path) rescue ""
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embulk-filter-google_vision_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- toyama0919
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- embulk-filter-google_vision_api.gemspec
|
83
83
|
- lib/embulk/filter/google_vision_api.rb
|
84
|
+
- lib/embulk/filter/google_vision_api/vision_client.rb
|
84
85
|
homepage: https://github.com/toyama0919/embulk-filter-google_vision_api
|
85
86
|
licenses:
|
86
87
|
- MIT
|