Module: Credo::Detector

Defined in:
lib/credo/detector.rb,
lib/credo/detector/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#last_pingObject (readonly)

Returns the value of attribute last_ping.



11
12
13
# File 'lib/credo/detector.rb', line 11

def last_ping
  @last_ping
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



11
12
13
# File 'lib/credo/detector.rb', line 11

def threshold
  @threshold
end

Class Method Details

.detect(device_id: 0, threshold: 60) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/credo/detector.rb', line 15

def self.detect(device_id: 0, threshold: 60)
  Framegrabber.open(device_id)

  detections = 0

  loop do
    grab_frame!
    sleep(0.000001)
    subpixels = @frame.flatten

    next print "\rToo much light detected, please cover your camera" if too_bright?(subpixels, threshold)
    print "\rBrightest subpixel: #{subpixels.max}"
    next if subpixels.max < threshold

    detections += 1

    multi_detections = 0
    @frame.each.with_index do |row, x|
      row.each.with_index do |pixel, y|
        next if pixel.max < threshold

        multi_detections += 1

        cropped = crop(x, y)
        filename = "detections/Sample no. #{detections}:#{multi_detections}.png"

        save_hit(cropped, filename)
        puts "\n#{filename} saved"
      end
    end
  end
ensure
  Framegrabber.release
  puts "Camera disabled"
end