framegrabber 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e85230c8feff750acc1216b7e0a300ec237879b0e2fa6b09e3ce0b22d38768d8
4
+ data.tar.gz: 50ccf739a1d30d016a2958081bc9e856fca5d47c28d6b1d020d3c02c41eff303
5
+ SHA512:
6
+ metadata.gz: 2d09d0a476009f5df20eabfb458284f7908721532f64653d3031e582c6d5bb8d334f977058951b38ee27e698e876405675257b559bf1cf7847a70b6596a29189
7
+ data.tar.gz: db51d1a46ce336c1368a00877106a6246037f70340e5318bd49e1522696f2e4b7c5a31d4003c7f897df5695170e6b3b8921020f9a2d64349f606d1ec18afdeea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at krzysztof.hasinski@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in framegrabber.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ framegrabber (0.1.0)
5
+ pkg-config (~> 1.4)
6
+ rmagick (~> 4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.5.0)
12
+ pkg-config (1.4.9)
13
+ rake (13.0.6)
14
+ rake-compiler (1.2.0)
15
+ rake
16
+ rmagick (4.3.0)
17
+ rspec (3.11.0)
18
+ rspec-core (~> 3.11.0)
19
+ rspec-expectations (~> 3.11.0)
20
+ rspec-mocks (~> 3.11.0)
21
+ rspec-core (3.11.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-expectations (3.11.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.11.0)
26
+ rspec-mocks (3.11.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.11.0)
29
+ rspec-support (3.11.0)
30
+
31
+ PLATFORMS
32
+ arm64-darwin-21
33
+
34
+ DEPENDENCIES
35
+ framegrabber!
36
+ rake (~> 13.0)
37
+ rake-compiler (~> 1.0)
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 2.3.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Krzysztof Hasiński
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Framegrabber
2
+
3
+ This gem uses OpenCV to grab an image from a camera.
4
+
5
+ It's meant to replace a dozen of difficult to execute setups
6
+ for grabbing images and uses much newer OpenCV than [ruby-opencv](https://github.com/ruby-opencv/ruby-opencv).
7
+
8
+ ## Prerequisites
9
+
10
+ Framegrabber relies only on OpenCV, however it can use [RMagick's](https://github.com/rmagick/rmagick)
11
+ Image format if it is included, which in turn uses ImageMagick.
12
+
13
+ #### OpenCV
14
+
15
+ macOS:
16
+
17
+ ```
18
+ $ brew install opencv
19
+ ```
20
+
21
+ Ubuntu/Debian:
22
+
23
+ ```
24
+ $ sudo apt install libopencv-dev
25
+ ```
26
+
27
+ It should work with other *nix systems if OpenCV is available.
28
+
29
+ ## Installation
30
+
31
+ Install the gem and add to the application's Gemfile by executing:
32
+
33
+ $ bundle add framegrabber
34
+
35
+ If bundler is not being used to manage dependencies, install the gem by executing:
36
+
37
+ $ gem install framegrabber
38
+
39
+ ## Usage
40
+
41
+ ```ruby
42
+ def grab_a_nice_image
43
+ # Open a camera, camera ids are numbered starting with 0.
44
+ # We support only one camera at a time.
45
+ # You can call this now and grab multiple frames
46
+ # before you close it.
47
+ Framegrabber.open(0)
48
+
49
+ # Array of arrays of RGB values for pixels
50
+ @rgb = Framegrabber.grab_frame
51
+
52
+ # RMagick's Image object
53
+ @image = Framegrabber.grab_image
54
+
55
+ rescue RuntimeError => e
56
+
57
+ # Camera may be unavailable or frame might be empty
58
+ # At this point we don't have nice error handling
59
+ puts "Oh no! #{e.message}"
60
+ ensure
61
+
62
+ # Camera will stay active until it is released
63
+ Framegrabber.release
64
+ end
65
+ ```
66
+
67
+ ## Development
68
+
69
+ Clone this repo, run `bundle` and `rake compile`.
70
+
71
+ Use `bin/console` to poke around and see what you can improve.
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/khasinski/framegrabber. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/framegrabber/blob/master/CODE_OF_CONDUCT.md).
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+
81
+ ## Code of Conduct
82
+
83
+ Everyone interacting in the Framegrabber project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/khasinski/framegrabber/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
9
+ task test: :compile
10
+
11
+ require "rake/extensiontask"
12
+ Rake::ExtensionTask.new("grab_frame_ext")
@@ -0,0 +1,9 @@
1
+ require "mkmf"
2
+ require "pkg-config"
3
+
4
+ $CXXFLAGS << " -std=c++11 "
5
+
6
+ $INCFLAGS << " " + PKGConfig.cflags("opencv4")
7
+ $LDFLAGS << " " + PKGConfig.libs("opencv4")
8
+
9
+ create_makefile("grab_frame_ext")
@@ -0,0 +1,71 @@
1
+ #include <ruby.h>
2
+ #include <opencv2/core.hpp>
3
+ #include <opencv2/videoio.hpp>
4
+ #include <grab_frame_ext.hpp>
5
+
6
+ using namespace cv;
7
+ using namespace std;
8
+
9
+ static VALUE open(VALUE _self, VALUE deviceID) {
10
+ int camID = NUM2INT(deviceID);
11
+ Mat frame;
12
+ cap.open(camID);
13
+
14
+ cap.read(frame);
15
+ sleep(1);
16
+
17
+ if (!cap.isOpened()) {
18
+ rb_raise(rb_eRuntimeError, "Unable to open camera %d", camID);
19
+ }
20
+ return Qnil;
21
+ }
22
+
23
+ static VALUE release(VALUE _self) {
24
+ cap.release();
25
+ return Qnil;
26
+ }
27
+
28
+ Mat grab()
29
+ {
30
+ Mat frame;
31
+ cap.read(frame);
32
+
33
+ if (frame.empty()) {
34
+ rb_raise(rb_eRuntimeError, "Empty frame captured");
35
+ }
36
+ return frame;
37
+ }
38
+
39
+ static VALUE grab_frame(VALUE _self) {
40
+ Mat frame = grab();
41
+
42
+ VALUE result = rb_ary_new_capa(frame.cols);
43
+
44
+ if(frame.empty()) {
45
+ return result;
46
+ }
47
+
48
+ int i = 0, j = 0;
49
+ for(i=0; i<frame.rows; ++i) {
50
+ VALUE row = rb_ary_new_capa(frame.cols);
51
+ for(j=0; j<frame.cols; j++) {
52
+ VALUE pixel = rb_ary_new_capa(3);
53
+ unsigned char * p = frame.ptr(i, j); // Y first, X after
54
+ rb_ary_store(pixel, 2, ULL2NUM(p[2]*255)); // R
55
+ rb_ary_store(pixel, 1, ULL2NUM(p[1]*255)); // G
56
+ rb_ary_store(pixel, 0, ULL2NUM(p[0]*255)); // B
57
+
58
+ rb_ary_store(row, j, pixel);
59
+ }
60
+ rb_ary_store(result, i, row);
61
+ }
62
+ return result;
63
+ }
64
+
65
+ extern "C"
66
+ void Init_grab_frame_ext(void) {
67
+ VALUE Framegrabber = rb_const_get(rb_cObject, rb_intern("Framegrabber"));
68
+ rb_define_module_function(Framegrabber, "grab_frame", grab_frame, 0);
69
+ rb_define_module_function(Framegrabber, "open", open, 1);
70
+ rb_define_module_function(Framegrabber, "release", release, 0);
71
+ }
@@ -0,0 +1,18 @@
1
+ #include <ruby.h>
2
+ #include <opencv2/core.hpp>
3
+ #include <opencv2/videoio.hpp>
4
+
5
+ using namespace cv;
6
+ using namespace std;
7
+
8
+ VideoCapture cap;
9
+
10
+ static VALUE open(VALUE _self, VALUE deviceID);
11
+
12
+ static VALUE release(VALUE _self);
13
+
14
+ Mat grab();
15
+
16
+ static VALUE grab_frame(VALUE _self);
17
+
18
+ extern "C" void Init_grab_frame_ext(void);
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Framegrabber
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "framegrabber/version"
4
+ require "grab_frame_ext"
5
+ begin
6
+ require 'rmagick'
7
+ rescue LoadError
8
+ end
9
+
10
+
11
+ module Framegrabber
12
+ if const_defined?("Magick")
13
+ def self.grab_image
14
+ frame = grab_frame
15
+ Magick::Image.constitute(frame.first.size, frame.size, "BGR", frame.flatten)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Framegrabber
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: framegrabber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Hasiński
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pkg-config
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rmagick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ description: Allows for capturing images easily
56
+ email:
57
+ - krzysztof.hasinski@gmail.com
58
+ executables: []
59
+ extensions:
60
+ - ext/grab_frame_ext/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - ext/grab_frame_ext/extconf.rb
71
+ - ext/grab_frame_ext/grab_frame_ext.cpp
72
+ - ext/grab_frame_ext/grab_frame_ext.hpp
73
+ - lib/framegrabber.rb
74
+ - lib/framegrabber/version.rb
75
+ - sig/framegrabber.rbs
76
+ homepage: https://github.com/khasinski/framegrabber
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/khasinski/framegrabber
81
+ source_code_uri: https://github.com/khasinski/framegrabber
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.7.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.3.7
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Uses OpenCV to grab frames from cameras
101
+ test_files: []