hwping 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/hwping/extconf.rb +12 -0
- data/ext/hwping/webcam.cpp +70 -0
- data/ext/hwping/webcam.h +17 -0
- data/lib/hwping.rb +1 -0
- data/lib/hwping/bot.rb +1 -0
- data/lib/hwping/config.rb +6 -4
- data/lib/hwping/constants.rb +1 -1
- data/lib/hwping/handler.rb +22 -0
- data/lib/hwping/webcam.rb +22 -0
- metadata +22 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 32cbdfd706a94a81aecf8807cd2de61b4ade411c
         | 
| 4 | 
            +
              data.tar.gz: 22eb2499eb61d123e2d1e5307cf2ca2d9d978e1a
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 4dc074af5d06181bd539ef541cc940eb659f4505880d1278f449eaef4cc46e388df2e26c72d3ede91813acc8015ba0093a3fa4b148acf43ff5e91fa9639a9bbe
         | 
| 7 | 
            +
              data.tar.gz: 5171b07619fc5debb1b11fbf087b63e02fa53c8de66ab768e3a06ad7853ef205efd438dcf75831f6663c9340c5b784940084186a668556808d9acb9b2edd59c4
         | 
| @@ -0,0 +1,12 @@ | |
| 1 | 
            +
            require 'mkmf'
         | 
| 2 | 
            +
            $CFLAGS = '--std=gnu++14 -O3'
         | 
| 3 | 
            +
            $LDFLAGS = '-lopencv_core -lopencv_highgui -lopencv_stitching'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            have_library('opencv_core')
         | 
| 6 | 
            +
            have_library('opencv_highui')
         | 
| 7 | 
            +
            have_library('opencv_stitching')
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            create_header
         | 
| 10 | 
            +
            extension_name = 'webcam'
         | 
| 11 | 
            +
            dir_config(extension_name)
         | 
| 12 | 
            +
            create_makefile(extension_name)
         | 
| @@ -0,0 +1,70 @@ | |
| 1 | 
            +
            #include "webcam.h"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            using namespace cv;
         | 
| 4 | 
            +
            VALUE mHWPing = Qnil;
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            void destroy_snapshot(void *ptr) {
         | 
| 7 | 
            +
              ((Mat*) ptr)->release();
         | 
| 8 | 
            +
              delete (Mat*) ptr;
         | 
| 9 | 
            +
            }
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            void* stitch_without_gvl(void *data) {
         | 
| 12 | 
            +
              cv::Stitcher stitcher = cv::Stitcher::createDefault();
         | 
| 13 | 
            +
              VALUE snapshots = *((VALUE*)data);
         | 
| 14 | 
            +
              std::vector<Mat> images;
         | 
| 15 | 
            +
              Mat *image, panorama;
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              for (int i = 0; i < RARRAY_LEN(snapshots); i++) {
         | 
| 18 | 
            +
                Data_Get_Struct(rb_ary_entry(snapshots, i), Mat, image);
         | 
| 19 | 
            +
                images.push_back(*image);
         | 
| 20 | 
            +
              }
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              stitcher.stitch(images, panorama);
         | 
| 23 | 
            +
              images.clear();
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              image = new Mat(panorama.rows, panorama.cols, CV_8U, 3);
         | 
| 26 | 
            +
              panorama.copyTo(*image);
         | 
| 27 | 
            +
              return (void*) image;
         | 
| 28 | 
            +
            }
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            static VALUE rb_create_snapshot(VALUE self) {
         | 
| 31 | 
            +
              VideoCapture cap(0);
         | 
| 32 | 
            +
              cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
         | 
| 33 | 
            +
              cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
         | 
| 34 | 
            +
              Mat frame, *image = new Mat(frame.rows, frame.cols, CV_8U, 3);
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              Scalar mean;
         | 
| 37 | 
            +
              cap >> frame;
         | 
| 38 | 
            +
              for (int i=0; i<3; i++) {
         | 
| 39 | 
            +
                mean = cv::mean(frame);
         | 
| 40 | 
            +
                if (mean[0] + mean[1] + mean[2] > 100) break;
         | 
| 41 | 
            +
                frame.release();
         | 
| 42 | 
            +
                cap >> frame;
         | 
| 43 | 
            +
              }
         | 
| 44 | 
            +
             | 
| 45 | 
            +
              frame.copyTo(*image);
         | 
| 46 | 
            +
              frame.release();
         | 
| 47 | 
            +
              cap.release();
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              return Data_Wrap_Struct(self, NULL, destroy_snapshot, image);
         | 
| 50 | 
            +
            }
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            static VALUE rb_save_snapshot(VALUE self, VALUE file) {
         | 
| 53 | 
            +
              Mat *image;
         | 
| 54 | 
            +
              Data_Get_Struct(self, Mat, image);
         | 
| 55 | 
            +
              return INT2NUM(imwrite(StringValuePtr(file), *image));
         | 
| 56 | 
            +
            }
         | 
| 57 | 
            +
             | 
| 58 | 
            +
            static VALUE rb_stitch_snapshots(VALUE self, VALUE snapshots) {
         | 
| 59 | 
            +
              Mat *image = (Mat*) rb_thread_call_without_gvl(stitch_without_gvl, (void*) &snapshots, RUBY_UBF_IO, NULL);
         | 
| 60 | 
            +
              return Data_Wrap_Struct(self, NULL, destroy_snapshot, image);
         | 
| 61 | 
            +
            }
         | 
| 62 | 
            +
             | 
| 63 | 
            +
            void Init_webcam(void) {
         | 
| 64 | 
            +
              mHWPing = rb_define_module("HWPing");
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              VALUE webcam = rb_define_class_under(mHWPing, "Webcam", rb_cObject);
         | 
| 67 | 
            +
              rb_define_alloc_func(webcam, rb_create_snapshot);
         | 
| 68 | 
            +
              rb_define_method(webcam, "write", RUBY_METHOD_FUNC(rb_save_snapshot), 1);
         | 
| 69 | 
            +
              rb_define_singleton_method(webcam, "stitch", RUBY_METHOD_FUNC(rb_stitch_snapshots), 1);
         | 
| 70 | 
            +
            }
         | 
    
        data/ext/hwping/webcam.h
    ADDED
    
    | @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            #ifndef __hwping_webcam_h__
         | 
| 2 | 
            +
            #define __hwping_webcam_h__
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            #include <ruby.h>
         | 
| 5 | 
            +
            #include <ruby/thread.h>
         | 
| 6 | 
            +
            #include <opencv2/opencv.hpp>
         | 
| 7 | 
            +
            #include <opencv2/stitching/stitcher.hpp>
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            extern VALUE mHWPing;
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            extern "C" void Init_webcam(void);
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            static VALUE rb_create_snapshot(VALUE self);
         | 
| 14 | 
            +
            static VALUE rb_save_snapshot(VALUE self, VALUE file);
         | 
| 15 | 
            +
            static VALUE rb_stitch_snapshots(VALUE self, VALUE snapshots);
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            #endif
         | 
    
        data/lib/hwping.rb
    CHANGED
    
    
    
        data/lib/hwping/bot.rb
    CHANGED
    
    
    
        data/lib/hwping/config.rb
    CHANGED
    
    | @@ -1,10 +1,11 @@ | |
| 1 1 | 
             
            module HWPing
         | 
| 2 2 | 
             
              class Config
         | 
| 3 | 
            -
                attr_reader :server
         | 
| 4 | 
            -
                attr_reader :port
         | 
| 5 | 
            -
                attr_reader :nick
         | 
| 6 | 
            -
                attr_reader :channels
         | 
| 7 3 | 
             
                attr_reader :auth
         | 
| 4 | 
            +
                attr_reader :channels
         | 
| 5 | 
            +
                attr_reader :nick
         | 
| 6 | 
            +
                attr_reader :port
         | 
| 7 | 
            +
                attr_reader :server
         | 
| 8 | 
            +
                attr_reader :webcam
         | 
| 8 9 | 
             
                attr_accessor :targets
         | 
| 9 10 |  | 
| 10 11 | 
             
                def initialize(hash = {})
         | 
| @@ -14,6 +15,7 @@ module HWPing | |
| 14 15 | 
             
                  @channels = Array(hash.fetch('channels', ['hwping-test']))
         | 
| 15 16 | 
             
                  @auth     = Array(hash.fetch('auth', []))
         | 
| 16 17 | 
             
                  @targets  = hash.fetch('targets', {}).to_h
         | 
| 18 | 
            +
                  @webcam   = hash.fetch('webcam', {}).to_h
         | 
| 17 19 | 
             
                end
         | 
| 18 20 |  | 
| 19 21 | 
             
                def to_hash
         | 
    
        data/lib/hwping/constants.rb
    CHANGED
    
    
    
        data/lib/hwping/handler.rb
    CHANGED
    
    | @@ -5,6 +5,7 @@ module HWPing | |
| 5 5 | 
             
                  @targets    = config.targets
         | 
| 6 6 | 
             
                  @auth       = config.auth
         | 
| 7 7 | 
             
                  @nick       = config.nick
         | 
| 8 | 
            +
                  @webcam     = config.webcam
         | 
| 8 9 | 
             
                  @launcher   = launcher
         | 
| 9 10 | 
             
                end
         | 
| 10 11 |  | 
| @@ -28,6 +29,10 @@ module HWPing | |
| 28 29 | 
             
                def private(event)
         | 
| 29 30 | 
             
                  if authorized?(event.user.to_s)
         | 
| 30 31 | 
             
                    case event.message
         | 
| 32 | 
            +
                    when 'snap'
         | 
| 33 | 
            +
                      return [:image, ip_address(event), Webcam.new(@webcam).save]
         | 
| 34 | 
            +
                    when 'panorama'
         | 
| 35 | 
            +
                      return [:image, ip_address(event), panorama]
         | 
| 31 36 | 
             
                    when 'fire'
         | 
| 32 37 | 
             
                      @launcher.fire
         | 
| 33 38 | 
             
                      return [:fire]
         | 
| @@ -79,5 +84,22 @@ module HWPing | |
| 79 84 | 
             
                  end
         | 
| 80 85 | 
             
                  false
         | 
| 81 86 | 
             
                end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                def panorama
         | 
| 89 | 
            +
                  @launcher.reset
         | 
| 90 | 
            +
                  images = []
         | 
| 91 | 
            +
                  12.times do
         | 
| 92 | 
            +
                    images << Webcam.new(@webcam)
         | 
| 93 | 
            +
                    @launcher.right(400)
         | 
| 94 | 
            +
                  end
         | 
| 95 | 
            +
                  @launcher.reset
         | 
| 96 | 
            +
                  pano = Webcam.panorama(images, @webcam)
         | 
| 97 | 
            +
                  images.clear # garbage collection
         | 
| 98 | 
            +
                  pano.save
         | 
| 99 | 
            +
                end
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                def ip_address(event)
         | 
| 102 | 
            +
                  event.bot.irc.socket.addr[3]
         | 
| 103 | 
            +
                end
         | 
| 82 104 | 
             
              end
         | 
| 83 105 | 
             
            end
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            require 'hwping/webcam.so'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module HWPing
         | 
| 4 | 
            +
              class Webcam
         | 
| 5 | 
            +
                def initialize(options = {})
         | 
| 6 | 
            +
                  @dir = options.fetch('path', './tmp')
         | 
| 7 | 
            +
                  @path = 'image.jpg'
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def save
         | 
| 11 | 
            +
                  write(File.join(@dir, @path))
         | 
| 12 | 
            +
                  @path
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def self.panorama(images, options = {})
         | 
| 16 | 
            +
                  pano = stitch(images)
         | 
| 17 | 
            +
                  pano.instance_variable_set(:@dir, options.fetch('path', './tmp'))
         | 
| 18 | 
            +
                  pano.instance_variable_set(:@path, 'image.jpg')
         | 
| 19 | 
            +
                  pano
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: hwping
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: '0. | 
| 4 | 
            +
              version: '0.3'
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Dávid Halász
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016-04- | 
| 11 | 
            +
            date: 2016-04-28 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: libusb
         | 
| @@ -38,6 +38,20 @@ dependencies: | |
| 38 38 | 
             
                - - "~>"
         | 
| 39 39 | 
             
                  - !ruby/object:Gem::Version
         | 
| 40 40 | 
             
                    version: 2.3.1
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rake-compiler
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - "~>"
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: 0.9.7
         | 
| 48 | 
            +
              type: :runtime
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - "~>"
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: 0.9.7
         | 
| 41 55 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 42 56 | 
             
              name: codecov
         | 
| 43 57 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -112,10 +126,14 @@ description: IRC bot for HW pinging with the Dream Cheeky Thunder missile launch | |
| 112 126 | 
             
            email: skateman@skateman.eu
         | 
| 113 127 | 
             
            executables:
         | 
| 114 128 | 
             
            - hwping
         | 
| 115 | 
            -
            extensions: | 
| 129 | 
            +
            extensions:
         | 
| 130 | 
            +
            - ext/hwping/extconf.rb
         | 
| 116 131 | 
             
            extra_rdoc_files: []
         | 
| 117 132 | 
             
            files:
         | 
| 118 133 | 
             
            - bin/hwping
         | 
| 134 | 
            +
            - ext/hwping/extconf.rb
         | 
| 135 | 
            +
            - ext/hwping/webcam.cpp
         | 
| 136 | 
            +
            - ext/hwping/webcam.h
         | 
| 119 137 | 
             
            - lib/hwping.rb
         | 
| 120 138 | 
             
            - lib/hwping/bot.rb
         | 
| 121 139 | 
             
            - lib/hwping/config.rb
         | 
| @@ -123,6 +141,7 @@ files: | |
| 123 141 | 
             
            - lib/hwping/handler.rb
         | 
| 124 142 | 
             
            - lib/hwping/hwping.rb
         | 
| 125 143 | 
             
            - lib/hwping/launcher.rb
         | 
| 144 | 
            +
            - lib/hwping/webcam.rb
         | 
| 126 145 | 
             
            homepage: http://github.com/skateman/hwping
         | 
| 127 146 | 
             
            licenses:
         | 
| 128 147 | 
             
            - GPL-2
         |