rb_webcam 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ /spec/rb_webcam_spec_flymake.rb
23
+ /spec/rb_webcam_spec_flymake_flymake.rb
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Hirotoshi YOSHITAKA
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ = rb_webcam
2
+ Crossplatform video capture library for Ruby
3
+ (Powered by OpenCV)
4
+
5
+ == Platform
6
+ Windows, Mac OS X, Linux, etc...
7
+ (OpenCV capable systems)
8
+
9
+ == Dependency
10
+ nice-ffi >= 0.3
11
+ OpenCV Library >= 2.0.0
12
+
13
+ == Install
14
+ gem install rb_webcam
15
+
16
+ == Usage
17
+ camera_id = 0
18
+ Webcam.open(camera_id) {|capture| image = capture.grab}
19
+
20
+ or
21
+
22
+ capture = Webcam.new
23
+ image = capture.grab
24
+ capture.close
25
+
26
+ == TODO
27
+ * output 'image' is *IplImage yet. we'll implement feature for JPG, BMP output.
28
+ * specity size at initialize, or anytime.
29
+
30
+ == LICENSE
31
+ The MIT License
32
+ Copyright (c) 2010 Hirotoshi YOSHITAKA. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rb_webcam"
8
+ gem.summary = "Crossplatform video capture library for Ruby (Powered by OpenCV)"
9
+ gem.description = "Crossplatform video capture library for Ruby (Powered by OpenCV)"
10
+ gem.email = "tyounan.moti@gmail.com"
11
+ gem.homepage = "http://github.com/TyounanMOTI/rb_webcam"
12
+ gem.authors = ["TyounanMOTI"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "nice-ffi", ">= 0.3"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "rb_webcam #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
@@ -0,0 +1,161 @@
1
+ require 'nice-ffi'
2
+
3
+ # wrapper module of OpenCV::Highgui
4
+ module Highgui
5
+ extend NiceFFI::Library
6
+
7
+ load_library("highgui")
8
+
9
+ class CvCapture < NiceFFI::OpaqueStruct
10
+ end
11
+
12
+ SIGNED_FLAG = 0x80000000
13
+ enum :color_depth, [ :unsinged_1bit, 1,
14
+ :unsigned_8bit, 8,
15
+ :unsigned_16bit, 16,
16
+ :unsinged_32bit, 32,
17
+ :signed_8bit, SIGNED_FLAG | 8,
18
+ :signed_16bit, SIGNED_FLAG | 16,
19
+ :signed_32bit, SIGNED_FLAG | 32 ]
20
+
21
+ # Grabbed webcam image will be stored in this structure.
22
+ # http://opencv.willowgarage.com/documentation/basic_structures.html#iplimage
23
+ class IplImage < NiceFFI::Struct
24
+ layout :n_size, :int,
25
+ :id, :int,
26
+ :n_channels, :int,
27
+ :alpha_channel, :int,
28
+ :depth, :color_depth,
29
+ :color_mode, [:char, 4], # pointer to char[4]
30
+ :channel_seq, [:char, 4], # pointer to char[4]
31
+ :data_order, :int,
32
+ :origin, :int,
33
+ :align, :int,
34
+ :width, :int,
35
+ :height, :int,
36
+ :roi, :pointer, # pointer to struct _IplROI
37
+ :mask_roi, :pointer, # pointer to struct _IplImage
38
+ :image_id, :pointer, # pointer to void
39
+ :tile_info, :pointer, # pointer to struct _IplTileInfo
40
+ :image_size, :int,
41
+ :image_data, :pointer, # pointer to char array
42
+ :width_step, :int,
43
+ :border_mode, [:int, 4], # pointer to int[4]
44
+ :border_const, [:int, 4], # pointer to int[4]
45
+ :image_data_origin, :pointer # pointer to char array
46
+ end
47
+
48
+ enum :property, [ :position_msec, 0,
49
+ :position_frames,
50
+ :position_avi_ratio,
51
+ :width,
52
+ :height,
53
+ :fps,
54
+ :fourcc,
55
+ :frame_count,
56
+ :format,
57
+ :mode,
58
+ :brightness,
59
+ :contrast,
60
+ :saturation,
61
+ :hue,
62
+ :gain,
63
+ :exposure,
64
+ :convert_rgb,
65
+ :white_balance,
66
+ :rectification ]
67
+
68
+ attach_function :create_camera_capture, :cvCreateCameraCapture, [:int], :pointer
69
+ attach_function :release_capture, :cvReleaseCapture, [:pointer], :void
70
+ attach_function :query, :cvQueryFrame, [:pointer], IplImage.typed_pointer
71
+ attach_function :get_property, :cvGetCaptureProperty, [:pointer, :property], :double
72
+ attach_function :set_property, :cvSetCaptureProperty, [:pointer, :property, :double], :int
73
+ end
74
+
75
+ # Class to controll your webcam.
76
+ # Initialize camera, grab image, then close.
77
+ class Webcam
78
+ # Open camera with 'method with block' sentence.
79
+ # This will open then close at start and end of block.
80
+ # ex.
81
+ # Webcam.open { |camera| @image = camera.grab }
82
+ def self.open(camera_id=0)
83
+ webcam = Webcam.new(camera_id)
84
+ yield webcam
85
+ webcam.close
86
+ end
87
+
88
+ # Open camera with camera_id, and size.
89
+ # camera_id: '0' to autodetect.
90
+ def initialize(camera_id=0)
91
+ @capture_handler = Highgui.create_camera_capture(camera_id)
92
+ end
93
+
94
+ # Grab a frame from camera and returns IplImage struct.
95
+ # This needs camera still opened.
96
+ def grab
97
+ raise "Camera has'nt be initialized" if @capture_handler.nil?
98
+ image = Highgui.query(@capture_handler)
99
+ return Image.new(image)
100
+ end
101
+
102
+ # Close camera. You need close opened camera for cleaner behavior.
103
+ def close
104
+ Highgui.release_capture(FFI::MemoryPointer.new(:pointer).write_pointer(@capture_handler))
105
+ @capture_handler = nil
106
+ end
107
+
108
+ # Set resolution of camera output.
109
+ # [usage] @webcam.resolution_mode = {width: 160, height: 120}
110
+ # Available resolution_mode is depends on your camera.
111
+ def resolution_mode=(resolution)
112
+ Highgui.set_property(@capture_handler, :width, resolution[:width])
113
+ Highgui.set_property(@capture_handler, :height, resolution[:height])
114
+ end
115
+
116
+ # Get resolution mode of camera.
117
+ # return format is written in resolution_mode=(resolution)
118
+ def resolution_mode
119
+ {width: Highgui.get_property(@capture_handler, :width),
120
+ height: Highgui.get_property(@capture_handler, :height)}
121
+ end
122
+
123
+ # Getter for debug use.
124
+ # Internally used to call OpenCV C functions for specified camera.
125
+ attr_reader :capture_handler
126
+
127
+ # Property Container of image from webcam.
128
+ # Get size, color_depth, mode, etc.. of image.
129
+ # Each instance have IplImage struct data.
130
+ # TODO: Having pointer to IplImage struct will be good for performance?
131
+ # (Does whole structure copying occur at Image.new(iplimage) ?)
132
+ class Image
133
+ def initialize(iplimage_struct)
134
+ @iplimage_struct = iplimage_struct
135
+ end
136
+
137
+ # get size of image. Hash with :width, :height elements.
138
+ def size
139
+ {width: @iplimage_struct.width, height: @iplimage_struct.height}
140
+ end
141
+
142
+ # get color depth of image.
143
+ def color_depth
144
+ @iplimage_struct.depth
145
+ end
146
+
147
+ # FFI::Pointer to image data
148
+ def data
149
+ @iplimage_struct.image_data
150
+ end
151
+
152
+ # data size of image
153
+ def data_size
154
+ @iplimage_struct.image_size
155
+ end
156
+
157
+ # IplImage structure stored grabbed image from camera.
158
+ attr_reader :iplimage_struct
159
+ end
160
+ end
161
+
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rb_webcam}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["TyounanMOTI"]
12
+ s.date = %q{2010-03-25}
13
+ s.description = %q{Crossplatform video capture library for Ruby (Powered by OpenCV)}
14
+ s.email = %q{tyounan.moti@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rb_webcam.rb",
27
+ "rb_webcam.gemspec",
28
+ "spec/rb_webcam_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/TyounanMOTI/rb_webcam}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Crossplatform video capture library for Ruby (Powered by OpenCV)}
37
+ s.test_files = [
38
+ "spec/rb_webcam_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
+ s.add_runtime_dependency(%q<nice-ffi>, [">= 0.3"])
49
+ else
50
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
51
+ s.add_dependency(%q<nice-ffi>, [">= 0.3"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ s.add_dependency(%q<nice-ffi>, [">= 0.3"])
56
+ end
57
+ end
58
+
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Webcam do
4
+ share_examples_for "Webcam which closed" do
5
+ it { lambda{ @c_webcam.grab }.should raise_error(RuntimeError, "Camera has'nt be initialized") }
6
+ end
7
+
8
+ share_examples_for "Webcam which lives a full life" do
9
+ it { @c_webcam.should_not be_nil }
10
+
11
+ it "capture handler should be instance of FFI::Pointer" do
12
+ @c_webcam.capture_handler.should be_instance_of(FFI::Pointer)
13
+ end
14
+
15
+ describe "grabbed image" do
16
+ before do
17
+ @image = @c_webcam.grab
18
+ end
19
+
20
+ it { @image.should be_instance_of Webcam::Image }
21
+
22
+ it "should have @iplimage_struct instance of Highgui::IplImage" do
23
+ @image.iplimage_struct.should be_instance_of Highgui::IplImage
24
+ end
25
+
26
+ it "color_depth should equal to camera resolution mode" do
27
+ @image.size.should == @c_webcam.resolution_mode
28
+ end
29
+
30
+ it "color_depth should be Symbol" do
31
+ @image.color_depth.should be_instance_of Symbol
32
+ end
33
+
34
+ it "data should be instance of FFI::Pointer" do
35
+ @image.data.should be_instance_of FFI::Pointer
36
+ end
37
+
38
+ it "data size should > 0" do
39
+ @image.data_size.should > 0
40
+ end
41
+ end
42
+
43
+ it "resolution mode of width should > 0" do
44
+ @c_webcam.resolution_mode[:width].should > 0.0
45
+ end
46
+
47
+ it "close() should return nil" do
48
+ @c_webcam.close.should be_nil
49
+ end
50
+
51
+ it_should_behave_like "Webcam which closed"
52
+ end
53
+
54
+ context "when given camera_id: 0" do
55
+ before(:all) do
56
+ @c_webcam = Webcam.new(0)
57
+ end
58
+
59
+ it_should_behave_like "Webcam which lives a full life"
60
+ end
61
+
62
+ context "when grab a frame using method with block" do
63
+ before do
64
+ @c_webcam
65
+ Webcam.open(0) do |camera|
66
+ @c_webcam = camera
67
+ end
68
+ end
69
+
70
+ it_should_behave_like "Webcam which closed"
71
+ end
72
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rb_webcam'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb_webcam
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
10
+ platform: ruby
11
+ authors:
12
+ - TyounanMOTI
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-25 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: nice-ffi
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 3
44
+ version: "0.3"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ description: Crossplatform video capture library for Ruby (Powered by OpenCV)
48
+ email: tyounan.moti@gmail.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - LICENSE
55
+ - README.rdoc
56
+ files:
57
+ - .document
58
+ - .gitignore
59
+ - LICENSE
60
+ - README.rdoc
61
+ - Rakefile
62
+ - VERSION
63
+ - lib/rb_webcam.rb
64
+ - rb_webcam.gemspec
65
+ - spec/rb_webcam_spec.rb
66
+ - spec/spec.opts
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/TyounanMOTI/rb_webcam
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Crossplatform video capture library for Ruby (Powered by OpenCV)
98
+ test_files:
99
+ - spec/rb_webcam_spec.rb
100
+ - spec/spec_helper.rb