levdon 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/denoise.rb +10 -0
- data/denoise.sh +3 -0
- data/example1.rb +24 -0
- data/example1.sh +1 -0
- data/example2.rb +44 -0
- data/example2.sh +1 -0
- data/lib/levdon/version.rb +1 -1
- data/lib/levdon.rb +94 -96
- data/test.rb +83 -0
- data/test.sh +1 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f644b9bcbe5fcd9e5a453bae0b8a0e34c9da18f5
|
4
|
+
data.tar.gz: 831a339cec878dc9266c50476d9d36c623f5d677
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a4baebf126b1b294c46ac94db7abed58ead303dfa9ca4df11179c4f3304c234ac947a6e1be89302880771790f1cc0905c2e4b881c0097345464732204ee4bf7
|
7
|
+
data.tar.gz: 15a5682baca2fc30062b2d01d8d82e48fe71d4d14f74df9f357ca7c5506039a58cf4d58db5d57b92e21b6ee696ed74ed446839bd632e8341f270077fd4d1e6b6
|
data/denoise.rb
ADDED
data/denoise.sh
ADDED
data/example1.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'levdon'
|
4
|
+
|
5
|
+
# Account is not necessary during for free version.
|
6
|
+
ACCESS_TOKEN = "TODO: specify your API key."
|
7
|
+
TARGET_PATH = "/Users/johndoe/Desktop/Unknown.jpeg"
|
8
|
+
|
9
|
+
api = Levdon.new()
|
10
|
+
state = api.start(ACCESS_TOKEN)
|
11
|
+
if state[:error]
|
12
|
+
puts state[:error]
|
13
|
+
else
|
14
|
+
|
15
|
+
res = api.predict({:target=>TARGET_PATH})
|
16
|
+
if res[:error]
|
17
|
+
puts res[:error]
|
18
|
+
else
|
19
|
+
puts res[:results]
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
api.close()
|
24
|
+
|
data/example1.sh
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
bundle exec ruby example1.rb
|
data/example2.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'levdon'
|
4
|
+
|
5
|
+
# Account is not necessary during for free version.
|
6
|
+
ACCESS_TOKEN = "TODO: specify your API key."
|
7
|
+
TARGET_PATH = "/Users/johndoe/Desktop/data/examples"
|
8
|
+
|
9
|
+
api = Levdon.new()
|
10
|
+
api.option({
|
11
|
+
:CLASS => [:CLASS_ADULT],
|
12
|
+
:PARALLEL => 32
|
13
|
+
})
|
14
|
+
|
15
|
+
api.async_start(ACCESS_TOKEN) {|state|
|
16
|
+
if(state[:error])
|
17
|
+
puts state[:error]
|
18
|
+
else
|
19
|
+
Levdon.find_image(TARGET_PATH,{:no_check=>true}) {|obj|
|
20
|
+
path = obj[:source_path]
|
21
|
+
while(api.queue_size() > 32*4)
|
22
|
+
api.poll()
|
23
|
+
sleep(0.01)
|
24
|
+
end
|
25
|
+
|
26
|
+
api.async_predict({:target => path}) {|res|
|
27
|
+
if res[:error]
|
28
|
+
puts res[:error]
|
29
|
+
else
|
30
|
+
result = res[:results]
|
31
|
+
puts "Score: "+(result[:CLASS_ADULT]*100).to_i.to_s + " : " + path
|
32
|
+
end
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
while(api.queue_size()!=0)
|
37
|
+
api.poll()
|
38
|
+
sleep(0.01)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
}
|
43
|
+
api.close()
|
44
|
+
|
data/example2.sh
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
bundle exec ruby example2.rb
|
data/lib/levdon/version.rb
CHANGED
data/lib/levdon.rb
CHANGED
@@ -33,7 +33,7 @@ module Levdon
|
|
33
33
|
}
|
34
34
|
|
35
35
|
ONTOLOGY_REV_LIST = {}
|
36
|
-
ONTOLOGY_LIST.
|
36
|
+
ONTOLOGY_LIST.each{|k,v| ONTOLOGY_REV_LIST[v[:key]] = k }
|
37
37
|
|
38
38
|
class NonBlockLineStream
|
39
39
|
def initialize(input_stream, output_stream)
|
@@ -555,67 +555,60 @@ module Levdon
|
|
555
555
|
@ontologies = [:CLASS_ADULT]
|
556
556
|
end
|
557
557
|
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
558
|
+
def _predict_impl(parameter)
|
559
|
+
ret = nil
|
560
|
+
error = nil
|
561
|
+
|
562
|
+
f = parameter[:target]
|
563
|
+
request_id = parameter[:request_id]
|
564
|
+
ontologies = parameter[:ontologies]
|
565
|
+
access_token = parameter[:access_token]
|
566
|
+
url = URI.parse(API_URL)
|
567
|
+
|
568
|
+
options = {}
|
569
|
+
options['ontologies'] = []
|
570
|
+
error = "Class is not specified." if ontologies.length == 0
|
571
|
+
ontologies.each{|key|
|
572
|
+
obj = ONTOLOGY_LIST[key]
|
573
|
+
unless obj
|
574
|
+
error = "Invalid key => " + key.to_s
|
575
|
+
break
|
576
|
+
end
|
577
|
+
unless obj[:available]
|
578
|
+
error = key.to_s + " class is not available.\n Reason => #{obj[:desc]}"
|
579
|
+
break
|
580
|
+
end
|
581
|
+
options['ontologies'] += [obj[:key]]
|
582
|
+
}
|
583
|
+
if(!error)
|
584
|
+
stream = load(f)
|
585
|
+
if(stream)
|
586
|
+
io = nil
|
566
587
|
begin
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
io = StringIO.new(Levdon.prob_resize(stream))
|
592
|
-
req = Net::HTTP::Post::Multipart.new url.path,
|
593
|
-
"api_version" => API_VERSION,
|
594
|
-
"upload" => UploadIO.new(io, "image/jpeg", "image.jpg"),
|
595
|
-
"request_id" => request_id,
|
596
|
-
"access_token" => access_token,
|
597
|
-
"constant_id" => APPLICATION_ID,
|
598
|
-
"options" => JSON.generate(options)
|
599
|
-
|
600
|
-
n = Net::HTTP.new(url.host, url.port)
|
601
|
-
n.use_ssl = ENABLE_SSL
|
602
|
-
|
603
|
-
res = n.start {|http| http.request(req) }
|
604
|
-
|
605
|
-
j = JSON.parse(res.body)
|
606
|
-
if(res.code == '200' and j['results'])
|
607
|
-
# TODO: data struct problem
|
608
|
-
ret = {}
|
609
|
-
j['results'].each{|k,v|
|
610
|
-
ret[ONTOLOGY_REV_LIST[k]] = v
|
611
|
-
}
|
612
|
-
else
|
613
|
-
ret = nil
|
614
|
-
error = j['desc']
|
615
|
-
end
|
616
|
-
else
|
617
|
-
error = "Could not read a '#{f.to_s}'."
|
618
|
-
end
|
588
|
+
io = StringIO.new(Levdon.prob_resize(stream))
|
589
|
+
req = Net::HTTP::Post::Multipart.new url.path,
|
590
|
+
"api_version" => API_VERSION,
|
591
|
+
"upload" => UploadIO.new(io, "image/jpeg", "image.jpg"),
|
592
|
+
"request_id" => request_id,
|
593
|
+
"access_token" => access_token,
|
594
|
+
"constant_id" => APPLICATION_ID,
|
595
|
+
"options" => JSON.generate(options)
|
596
|
+
|
597
|
+
n = Net::HTTP.new(url.host, url.port)
|
598
|
+
n.use_ssl = ENABLE_SSL
|
599
|
+
|
600
|
+
res = n.start {|http| http.request(req) }
|
601
|
+
|
602
|
+
j = JSON.parse(res.body)
|
603
|
+
if(res.code == '200' and j['results'])
|
604
|
+
# TODO: data struct problem
|
605
|
+
ret = {}
|
606
|
+
j['results'].each{|k,v|
|
607
|
+
ret[ONTOLOGY_REV_LIST[k]] = v
|
608
|
+
}
|
609
|
+
else
|
610
|
+
ret = nil
|
611
|
+
error = j['desc']
|
619
612
|
end
|
620
613
|
rescue => e
|
621
614
|
error = ""
|
@@ -636,6 +629,31 @@ module Levdon
|
|
636
629
|
end
|
637
630
|
end
|
638
631
|
end
|
632
|
+
else
|
633
|
+
error = "Could not read a '#{f.to_s}'."
|
634
|
+
end
|
635
|
+
end
|
636
|
+
return {:error=>error,:result=>ret}
|
637
|
+
end
|
638
|
+
|
639
|
+
def async_start(access_token,&block)
|
640
|
+
@access_token = access_token
|
641
|
+
@workers = @request_cluster_num.times.map{
|
642
|
+
Worker.new{|*args|
|
643
|
+
ret = nil
|
644
|
+
error = nil
|
645
|
+
begin
|
646
|
+
parameter = args[0]
|
647
|
+
parameter[:access_token] = access_token
|
648
|
+
obj = _predict_impl(parameter)
|
649
|
+
ret = obj[:result]
|
650
|
+
error = obj[:error]
|
651
|
+
rescue => e
|
652
|
+
error = ""
|
653
|
+
error += e.class.to_s + "\n"
|
654
|
+
error += e.message.to_s + "\n"
|
655
|
+
error += e.backtrace.to_s + "\n"
|
656
|
+
end
|
639
657
|
|
640
658
|
{:results => ret, :error => error,:parameter => parameter}
|
641
659
|
}
|
@@ -658,14 +676,8 @@ module Levdon
|
|
658
676
|
end
|
659
677
|
|
660
678
|
def start(access_token)
|
661
|
-
|
662
|
-
|
663
|
-
ret = e
|
664
|
-
}
|
665
|
-
while(ret == nil)
|
666
|
-
sleep(0.01)
|
667
|
-
end
|
668
|
-
ret
|
679
|
+
@access_token = access_token
|
680
|
+
return {:erorr => nil}
|
669
681
|
end
|
670
682
|
|
671
683
|
def option(obj)
|
@@ -686,9 +698,7 @@ module Levdon
|
|
686
698
|
# from network
|
687
699
|
return open(any).read
|
688
700
|
else
|
689
|
-
|
690
|
-
if(any.length < 1024)
|
691
|
-
# from file
|
701
|
+
if File::ftype(any) == "file"
|
692
702
|
return File.open(any).read # File IO
|
693
703
|
else
|
694
704
|
# from memory
|
@@ -764,33 +774,21 @@ module Levdon
|
|
764
774
|
raise "required key: target. Specify a file path or URL or data." unless(parameter[:target])
|
765
775
|
parameter[:request_id] = SecureRandom.uuid.gsub("-","")
|
766
776
|
parameter[:ontologies] = @ontologies
|
767
|
-
|
777
|
+
parameter[:access_token] = @access_token
|
778
|
+
|
779
|
+
ret = nil
|
780
|
+
error = nil
|
768
781
|
begin
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
puts ("Timeout")
|
773
|
-
break
|
774
|
-
end
|
775
|
-
result = nil
|
776
|
-
@workers.each(&:poll)
|
777
|
-
@workers.map(&:nonblock_read_from_child).each_with_index{|ret,i|
|
778
|
-
result = ret
|
779
|
-
break if(ret)
|
780
|
-
}
|
781
|
-
if(result)
|
782
|
-
return result
|
783
|
-
else
|
784
|
-
sleep(0.01)
|
785
|
-
end
|
786
|
-
}
|
782
|
+
obj = _predict_impl(parameter)
|
783
|
+
ret = obj[:result]
|
784
|
+
error = obj[:error]
|
787
785
|
rescue => e
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
786
|
+
error = ""
|
787
|
+
error += e.class.to_s + "\n"
|
788
|
+
error += e.message.to_s + "\n"
|
789
|
+
error += e.backtrace.to_s + "\n"
|
792
790
|
end
|
793
|
-
return
|
791
|
+
return {:results => ret, :error => error,:parameter => parameter}
|
794
792
|
end
|
795
793
|
|
796
794
|
end
|
data/test.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'levdon'
|
4
|
+
|
5
|
+
# Account is not necessary during for free version.
|
6
|
+
APIKEY = ENV['EATER_ACCESS_TOKEN']
|
7
|
+
TARGET_PATH = "/Users/johndoe/Desktop/data/test_data"
|
8
|
+
|
9
|
+
USE_SYNC = true
|
10
|
+
if(USE_SYNC)
|
11
|
+
api = Levdon.new()
|
12
|
+
api.option({:CLASS => [:CLASS_ADULT]})
|
13
|
+
state = api.start(APIKEY)
|
14
|
+
if(state[:error])
|
15
|
+
puts state[:error]
|
16
|
+
else
|
17
|
+
Levdon.find_image(TARGET_PATH,{:no_check=>true}) {|obj|
|
18
|
+
path = obj[:source_path]
|
19
|
+
res = api.predict({:target => path})
|
20
|
+
|
21
|
+
if res[:error]
|
22
|
+
puts res[:error]
|
23
|
+
else
|
24
|
+
result = res[:results]
|
25
|
+
score = result[:CLASS_ADULT]
|
26
|
+
sscore1 = (( (score * 10000).to_i)/100).to_s.rjust(5, " ")+"%"
|
27
|
+
sscore2 = (score * 10000).to_i.to_s.rjust(5, "0")
|
28
|
+
puts "Score: "+sscore1 + " : " + path
|
29
|
+
|
30
|
+
dst_dirname = File.dirname(path)
|
31
|
+
FileUtils.mv(path,File.join(dst_dirname,"CLASS_ADULT_"+sscore2+"_"+SecureRandom.uuid+"."+path.split(".")[-1]))
|
32
|
+
end
|
33
|
+
|
34
|
+
}
|
35
|
+
end
|
36
|
+
api.close()
|
37
|
+
else
|
38
|
+
api = Levdon.new()
|
39
|
+
api.option({
|
40
|
+
:CLASS => [:CLASS_ADULT],
|
41
|
+
:PARALLEL => 32
|
42
|
+
})
|
43
|
+
|
44
|
+
api.async_start(APIKEY) {|state|
|
45
|
+
if(state[:error])
|
46
|
+
puts state[:error]
|
47
|
+
else
|
48
|
+
Levdon.find_image(TARGET_PATH,{:no_check=>true}) {|obj|
|
49
|
+
path = obj[:source_path]
|
50
|
+
#next if(File.basename(path).index("CLASS_ADULT_") == 0)
|
51
|
+
while(api.queue_size() > 32*4)
|
52
|
+
api.poll()
|
53
|
+
sleep(0.01)
|
54
|
+
end
|
55
|
+
|
56
|
+
api.async_predict({:target => path}) {|res|
|
57
|
+
if res[:error]
|
58
|
+
puts res[:error]
|
59
|
+
else
|
60
|
+
result = res[:results]
|
61
|
+
score = result[:CLASS_ADULT]
|
62
|
+
sscore1 = (( (score * 10000).to_i)/100).to_s.rjust(5, " ")+"%"
|
63
|
+
sscore2 = (score * 10000).to_i.to_s.rjust(5, "0")
|
64
|
+
puts "Score: "+sscore1 + " : " + path
|
65
|
+
|
66
|
+
dst_dirname = File.dirname(path)
|
67
|
+
FileUtils.mv(path,File.join(dst_dirname,"CLASS_ADULT_"+sscore2+"_"+SecureRandom.uuid+"."+path.split(".")[-1]))
|
68
|
+
|
69
|
+
end
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
while(api.queue_size()!=0)
|
74
|
+
api.poll()
|
75
|
+
sleep(0.01)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
}
|
80
|
+
api.close()
|
81
|
+
|
82
|
+
end
|
83
|
+
|
data/test.sh
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
bundle exec ruby test.rb
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: levdon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pegara, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,9 +80,17 @@ files:
|
|
80
80
|
- Rakefile
|
81
81
|
- bin/console
|
82
82
|
- bin/setup
|
83
|
+
- denoise.rb
|
84
|
+
- denoise.sh
|
85
|
+
- example1.rb
|
86
|
+
- example1.sh
|
87
|
+
- example2.rb
|
88
|
+
- example2.sh
|
83
89
|
- levdon.gemspec
|
84
90
|
- lib/levdon.rb
|
85
91
|
- lib/levdon/version.rb
|
92
|
+
- test.rb
|
93
|
+
- test.sh
|
86
94
|
homepage: https://www.leviadon.com
|
87
95
|
licenses:
|
88
96
|
- MIT
|