arthropod_waifu2x 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 23719b9d187f57b84a271087e4ef2759b9e525f19c0c6accaa34b2fe16de1b0e
4
+ data.tar.gz: 24774ff0e9b481c61605a90d454867d2440978f49d91c5b1e0046b033843411f
5
+ SHA512:
6
+ metadata.gz: f79f867ff386d339a262af73200657861e22b28bfbbd0e48a7ee6f8a06f742fe6780fa02f46861135dd8192bf68c04db28140122469ee09fa8fca004d605a08e
7
+ data.tar.gz: 726886d15be2186bee299c7e7eaa8ecc30a9848bcfac8e0d9fabec439267385d0385ea109462c04c4e60f1025f4fd7f47231e4144f1f04b5ae871bc876f17e4d
@@ -0,0 +1,3 @@
1
+ ffmpeg*
2
+ .byebug_history
3
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ arthropod_waifu2x (0.0.1)
5
+ arthropod (= 0.0.2)
6
+ aws-sdk-sqs
7
+ fog-aws
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ arthropod (0.0.2)
13
+ aws-sdk-sqs
14
+ aws-eventstream (1.0.3)
15
+ aws-partitions (1.251.0)
16
+ aws-sdk-core (3.84.0)
17
+ aws-eventstream (~> 1.0, >= 1.0.2)
18
+ aws-partitions (~> 1, >= 1.239.0)
19
+ aws-sigv4 (~> 1.1)
20
+ jmespath (~> 1.0)
21
+ aws-sdk-sqs (1.23.1)
22
+ aws-sdk-core (~> 3, >= 3.71.0)
23
+ aws-sigv4 (~> 1.1)
24
+ aws-sigv4 (1.1.0)
25
+ aws-eventstream (~> 1.0, >= 1.0.2)
26
+ builder (3.2.3)
27
+ byebug (11.0.1)
28
+ excon (0.70.0)
29
+ fog-aws (3.5.2)
30
+ fog-core (~> 2.1)
31
+ fog-json (~> 1.1)
32
+ fog-xml (~> 0.1)
33
+ ipaddress (~> 0.8)
34
+ fog-core (2.1.2)
35
+ builder
36
+ excon (~> 0.58)
37
+ formatador (~> 0.2)
38
+ mime-types
39
+ fog-json (1.2.0)
40
+ fog-core
41
+ multi_json (~> 1.10)
42
+ fog-xml (0.1.3)
43
+ fog-core
44
+ nokogiri (>= 1.5.11, < 2.0.0)
45
+ formatador (0.2.5)
46
+ ipaddress (0.8.3)
47
+ jmespath (1.4.0)
48
+ mime-types (3.3)
49
+ mime-types-data (~> 3.2015)
50
+ mime-types-data (3.2019.1009)
51
+ mini_portile2 (2.4.0)
52
+ multi_json (1.14.1)
53
+ nokogiri (1.10.7)
54
+ mini_portile2 (~> 2.4.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ arthropod_waifu2x!
61
+ byebug
62
+
63
+ BUNDLED WITH
64
+ 2.0.2
@@ -0,0 +1 @@
1
+ worker: bundle exec bin/arthropod_waifu2x
@@ -0,0 +1,56 @@
1
+ # Arthropod-waifu2x
2
+
3
+ ## Installation
4
+
5
+ ```
6
+ gem install arthropod_waifu2x
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ Just run it with the required arguments.
12
+ ```shell
13
+ $ arthropod_waifu2x -h
14
+
15
+ Usage: arthropod_waifu2x [options]
16
+ -q, --queue [string] SQS queue name
17
+ -i, --access-key-id [string] AWS access key ID, default to the AWS_ACCESS_KEY_ID environment variable
18
+ -k, --secret-access-key [string] AWS secret access key, default to the AWS_SECRET_ACCESS_KEY environment variable
19
+ -r, --region [string] AWS region, default to the AWS_REGION environment variable
20
+ -w, --waifu [string] Waifu2X install directory, default to /opt/waifu2x
21
+ ```
22
+
23
+ Example of client side call:
24
+ ```ruby
25
+ result = Arthropod::Client.push(queue_name: "waifu2x", body: {
26
+ image_url: "https://s3-#{ENV['S3_REGION']}.amazonaws.com/#{ENV['S3_BUCKET']}/#{medium.temporary_key}",
27
+ root_dir: Digest::SHA1.hexdigest("#{ENV["SECURE_UPLOADER_KEY"]}#{medium.uuid}").insert(3, '/'),
28
+ aws_access_key_id: ENV['S3_ACCESS_KEY_ID'],
29
+ aws_secret_access_key: ENV['S3_SECRET_ACCESS_KEY'],
30
+ region: ENV['S3_REGION'],
31
+ endpoint: ENV['S3_ENDPOINT'],
32
+ host: ENV['S3_HOST'],
33
+ bucket: ENV['S3_BUCKET'],
34
+ })
35
+ ```
36
+
37
+ * `image_url`: the URL of the image you want to scale
38
+ * `root`: the destination directory in your bucket
39
+ * `aws_access_key_id`: an AWS access key to access your bucket
40
+ * `aws_secret_access_key`: an AWS secret access key to access your bucket
41
+ * `region`: the region of your bucket
42
+ * `endpoint`: the endpoint of your S3 instance if you have one (useful for Minio)
43
+ * `host`: the host of your S3 instance if you have one (useful for Minio)
44
+ * `bucket`: your bucket name
45
+
46
+ The result object is a follow.
47
+
48
+ ```ruby
49
+ {
50
+ key: "[string]",
51
+ }
52
+ ```
53
+
54
+ * `key`: the key the resulting image
55
+
56
+ *Both the input and output are very opiniated and follow my needs*
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "arthropod_waifu2x/version"
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "arthropod_waifu2x"
9
+ gem.version = ArthropodWaifu2x::VERSION
10
+ gem.authors = ["Victor Goya"]
11
+ gem.email = ["goya.victor@gmail.com"]
12
+ gem.description = "Use Waifu2X remotely Arthropod"
13
+ gem.summary = "Use Waifu2X remotely Arthropod"
14
+
15
+ gem.files = `git ls-files -z`.split("\x0")
16
+ gem.executables = %w(arthropod_waifu2x)
17
+ gem.require_paths = ["lib"]
18
+ gem.bindir = 'bin'
19
+
20
+ gem.licenses = ["MIT"]
21
+
22
+ gem.required_ruby_version = "~> 2.0"
23
+
24
+ gem.add_dependency 'arthropod', '= 0.0.2'
25
+ gem.add_dependency 'aws-sdk-sqs'
26
+ gem.add_dependency 'fog-aws'
27
+
28
+ gem.add_development_dependency "byebug"
29
+ end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'aws-sdk-sqs'
5
+ require 'arthropod'
6
+ require 'arthropod_waifu2x/scaler'
7
+
8
+ options = {}
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
11
+
12
+ opts.on("-q", "--queue [string]", "SQS queue name") do |q|
13
+ options[:queue] = q
14
+ end
15
+ opts.on("-i", "--access-key-id [string]", "AWS access key ID, default to the AWS_ACCESS_KEY_ID environment variable") do |i|
16
+ options[:access_key_id] = i
17
+ end
18
+ opts.on("-k", "--secret-access-key [string]", "AWS secret access key, default to the AWS_SECRET_ACCESS_KEY environment variable") do |k|
19
+ options[:secret_access_key] = k
20
+ end
21
+ opts.on("-r", "--region [string]", "AWS region, default to the AWS_REGION environment variable") do |r|
22
+ options[:region] = r
23
+ end
24
+ opts.on("-w", "--waifu [string]", "Waifu2X install directory, default to WAIFU environment variable or /opt/waifu2x") do |w|
25
+ options[:waifu] = w
26
+ end
27
+
28
+ end.parse!
29
+
30
+ client = Aws::SQS::Client.new({
31
+ access_key_id: options[:access_key_id] || ENV["AWS_ACCESS_KEY_ID"],
32
+ secret_access_key: options[:secret_access_key] || ENV["AWS_SECRET_ACCESS_KEY"],
33
+ region: options[:region] || ENV["AWS_REGION"],
34
+ })
35
+
36
+ loop do
37
+ Arthropod::Server.pull(client: client, queue_name: options[:queue] || ENV["QUEUE_NAME"]) do |request|
38
+ ArthropodWaifu2x::Encoder.new({
39
+ waifu: options[:waifu] || ENV["WAIFU"] || "/opt/waifu2x",
40
+ image_url: request.body["image_url"],
41
+ aws_access_key_id: request.body["aws_access_key_id"],
42
+ aws_secret_access_key: request.body["aws_secret_access_key"],
43
+ region: request.body["region"],
44
+ endpoint: request.body["endpoint"],
45
+ host: request.body["host"],
46
+ bucket: request.body["bucket"],
47
+ }).perform!
48
+ end
49
+ end
@@ -0,0 +1,88 @@
1
+ require 'shellwords'
2
+ require 'json'
3
+ require 'securerandom'
4
+ require 'fog/aws'
5
+ require 'pathname'
6
+
7
+ module ArthropodWaifu2x
8
+ class Scaler
9
+ attr_reader :image_url, :aws_access_key_id, :aws_secret_access_key, :region, :endpoint, :host, :bucket, :root_dir
10
+
11
+ def initialize(image_url:, root_dir:, aws_access_key_id:, aws_secret_access_key:, region:, endpoint:, host:, bucket:, root_dir:, waifu:)
12
+ @image_url = image_url
13
+ @aws_access_key_id = aws_access_key_id
14
+ @aws_secret_access_key = aws_secret_access_key
15
+ @region = region
16
+ @endpoint = endpoint
17
+ @host = host
18
+ @root_dir = root_dir
19
+ @bucket = bucket
20
+ @waifu = waifu
21
+ end
22
+
23
+ def perform!
24
+ Dir.mktmpdir do |wdir|
25
+ @wdir = wdir
26
+
27
+ download_input!
28
+
29
+ {
30
+ key: perform_scaling!
31
+ }
32
+ end
33
+ end
34
+
35
+ def download_input!
36
+ unless File.exists? input_path
37
+ call_command("curl #{Shellwords.escape(image_url)} -s -o #{input_path}")
38
+ end
39
+ end
40
+
41
+ def perform_scaling!
42
+ call_command("th #{waifu_bin} -m scale -i #{input_path} -o #{scaled_path}")
43
+ "#{root_dir}/#{SecureRandom.uuid}#{File.extname(image_url)}".tap do |key|
44
+ upload(scaled_path, key)
45
+ end
46
+ end
47
+
48
+ def waifu_bin
49
+ Shellwords.escape(Pathname.new(waifu).join("waifu2x").to_s)
50
+ end
51
+
52
+ def input_path
53
+ Shellwords.escape("#{@wdir}/input#{File.extname(image_url)}")
54
+ end
55
+
56
+ def scaled_path
57
+ Shellwords.escape("#{@wdir}/scaled#{File.extname(image_url)}")
58
+ end
59
+
60
+ def call_command(command)
61
+ system(command, out: File::NULL, err: File::NULL)
62
+ raise if $?.to_i != 0
63
+ end
64
+
65
+ def storage
66
+ @storage ||= Fog::Storage.new({
67
+ provider: 'AWS',
68
+ aws_access_key_id: aws_access_key_id,
69
+ aws_secret_access_key: aws_secret_access_key,
70
+ region: region,
71
+ endpoint: endpoint,
72
+ host: host,
73
+ path_style: true
74
+ })
75
+ @storage.directories.get(bucket)
76
+ end
77
+
78
+ def upload(path, key)
79
+ open(path) do |file|
80
+ storage.files.create({
81
+ key: key,
82
+ body: file,
83
+ public: true
84
+ })
85
+ end.public_url
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module ArthropodWaifu2x
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arthropod_waifu2x
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Victor Goya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: arthropod
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-sqs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fog-aws
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Use Waifu2X remotely Arthropod
70
+ email:
71
+ - goya.victor@gmail.com
72
+ executables:
73
+ - arthropod_waifu2x
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - Procfile
81
+ - README.md
82
+ - arthropod_waifu2x.gemspec
83
+ - bin/arthropod_waifu2x
84
+ - lib/arthropod_waifu2x/scaler.rb
85
+ - lib/arthropod_waifu2x/version.rb
86
+ homepage:
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '2.0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.0.3
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Use Waifu2X remotely Arthropod
109
+ test_files: []