Skip to content

Commit d5e41f2

Browse files
author
remi Taylor
committed
Add specs for Cloud Vision sample
1 parent 7cbaec4 commit d5e41f2

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

vision-sample/spec/vision_samples_spec.rb

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,60 @@
1414

1515
require_relative "../vision_samples"
1616
require "rspec"
17-
require "gcloud"
17+
require "tempfile"
1818

1919
describe "Vision sample" do
2020

21-
it "detect labels"
22-
it "detect landmark"
23-
it "detect faces"
21+
# Returns full path to sample image included in repository for testing
22+
def image_path filename
23+
File.expand_path "../images/#{filename}", __dir__
24+
end
2425

26+
# Capture and return STDOUT output by block
27+
def capture &block
28+
real_stdout = $stdout
29+
$stdout = StringIO.new
30+
block.call
31+
@captured_output = $stdout.string
32+
ensure
33+
$stdout = real_stdout
34+
end
35+
attr_reader :captured_output
36+
37+
# cat.jpg
38+
# eiffel_tower.jpg
39+
# face.png
40+
41+
example "detect labels" do
42+
capture { detect_labels image_path("cat.jpg") }
43+
44+
expect(captured_output).to start_with "Image labels:"
45+
expect(captured_output).to include "cat"
46+
expect(captured_output).to include "mammal"
47+
end
48+
49+
example "detect landmark" do
50+
expect { detect_landmark image_path("eiffel_tower.jpg") }.to output(
51+
"Found landmark: Eiffel Tower\n"
52+
).to_stdout
53+
end
54+
55+
example "detect faces" do
56+
output_image_file = Tempfile.new "cloud-vision-testing"
57+
expect(File.size output_image_file.path).to eq 0
58+
59+
begin
60+
capture { detect_faces image_path("face.png"), output_image_file.path }
61+
62+
expect(captured_output).to include "Face bounds:"
63+
expect(captured_output).to include "(154, 33)"
64+
expect(captured_output).to include "(301, 33)"
65+
expect(captured_output).to include "(301, 180)"
66+
expect(captured_output).to include "(154, 180)"
67+
expect(File.size output_image_file.path).to be > 0
68+
ensure
69+
output_image_file.close
70+
output_image_file.unlink
71+
end
72+
end
2573
end

vision-sample/vision_samples.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ def detect_faces path_to_image_file, path_to_output_file
8484
image = Magick::Image.read(path_to_image_file)[0]
8585

8686
faces.each do |face|
87+
puts "Face bounds:"
8788
face.bounds.face.each do |vector|
88-
puts "#{vector.x}, #{vector.y}"
89+
puts "(#{vector.x}, #{vector.y})"
8990
end
9091

9192
draw = Magick::Draw.new
@@ -116,22 +117,22 @@ def detect_faces path_to_image_file, path_to_output_file
116117
when "labels"
117118
detect_labels ARGV.shift
118119
when "landmarks"
119-
detect_landmarks ARGV.shift
120+
detect_landmark ARGV.shift
120121
when "faces"
121122
detect_faces ARGV.shift, ARGV.shift
122123
else
123124
puts <<-usage
124125
Usage: ruby vision_samples.rb <command> [arguments]
125126
126127
Commands:
127-
labels <image-path>
128-
landmarks <image-path>
129-
faces <image-path> <output-image-path>
128+
labels <image-path>
129+
landmark <image-path>
130+
faces <image-path> <output-image-path>
130131
131132
Examples:
132-
ruby vision_samples.rb labels /path/to/cat.jpg
133-
ruby vision_samples.rb landmarks /path/to/grand-canyon.jpg
134-
ruby vision_samples.rb faces /path/to/faces.jpg output-image.jpg
133+
ruby vision_samples.rb labels /path/to/cat.jpg
134+
ruby vision_samples.rb landmark /path/to/grand-canyon.jpg
135+
ruby vision_samples.rb faces /path/to/faces.jpg output-image.jpg
135136
usage
136137
end
137138
end

0 commit comments

Comments
 (0)