credo-detector 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 905e46749f9bd9954908098155eae5ec9af84178589e1421a5d5bbd7848d31cb
4
- data.tar.gz: da1d43425df1458dc5c8734d56917c28ac29908b74ceaff04113d55e229e1971
3
+ metadata.gz: ac9f99f1a615f2e9a287e0af9906802d8bdee4cc8eae16c21607081501fc2c64
4
+ data.tar.gz: 298e77d46495f6785f78f189f66fe6128c57c36a747eafd02b074ec9bd8c4bfd
5
5
  SHA512:
6
- metadata.gz: 9a51bfba3f70da442f2a299c86e5ea37538f79608b8c3697c65c20d9a15c9a3a0df6e56106810741af293e956539474e1e969dcb1168fc01d6b6b9006149eeaa
7
- data.tar.gz: 28bb26cf7fdcf0bb739e44d51242a318a9cbe302a3159f25cfdbd1b143d3c17d847727730e8998db23547c4a2a631e327dd876bb981310eb698121a822b86ba4
6
+ metadata.gz: 1c46fdedfa2f40f5b0fe2c0dfa757f28f7e6238329c979568455c68ae8b7cb4fb7e22eb277c6704084e32df6afb6ff390084c96747599b8becafb4bb06f3af16
7
+ data.tar.gz: 2e11ac4204f3f3c1654214fc9312a80d9018f94c0e2a40a3930a85d9f3086d90bbfb6885ab6f107224f20ae68ea82b14a335278405b23278d0c716bcb885ed90
data/Gemfile.lock CHANGED
@@ -1,17 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- credo-detector (0.1.0)
5
- rmagick
4
+ credo-detector (0.1.3)
5
+ rmagick (~> 4.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  diff-lcs (1.5.0)
11
+ pkg-config (1.4.9)
11
12
  rake (13.0.6)
12
13
  rake-compiler (1.2.0)
13
14
  rake
14
- rmagick (4.2.6)
15
+ rmagick (4.3.0)
15
16
  rspec (3.11.0)
16
17
  rspec-core (~> 3.11.0)
17
18
  rspec-expectations (~> 3.11.0)
@@ -31,6 +32,7 @@ PLATFORMS
31
32
 
32
33
  DEPENDENCIES
33
34
  credo-detector!
35
+ pkg-config (~> 1.4)
34
36
  rake (~> 13.0)
35
37
  rake-compiler (~> 1.0)
36
38
  rspec (~> 3.0)
data/README.md CHANGED
@@ -1,22 +1,73 @@
1
- # Credo::Detector
1
+ # Credo Detector
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/credo/detector`. To experiment with that code, run `bin/console` for an interactive prompt.
4
3
 
5
- TODO: Delete this and the text above, and describe your gem
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+ [Credo](https://credo.science/) detector for Ruby!
13
+
14
+ This allows you to both calibrate your sensor and run detection on multiple cameras.
6
15
 
7
16
  ## Installation
8
17
 
9
- Install the gem and add to the application's Gemfile by executing:
18
+ ### Prerequirements
19
+
20
+ #### macOS
21
+
22
+ Install OpenCV4 with:
23
+
24
+ $ brew install opencv
25
+
26
+ Install imagemagick with:
27
+
28
+ $ brew install pkg-config imagemagick
29
+
30
+ #### Ubuntu/Debian
31
+
32
+ Install OpenCV4 with:
10
33
 
11
- $ bundle add credo-detector
34
+ $ sudo apt install libopencv-dev
12
35
 
13
- If bundler is not being used to manage dependencies, install the gem by executing:
36
+ Install imagemagick with:
14
37
 
15
- $ gem install credo-detector
38
+ $ sudo apt install libmagickwand-dev
39
+
40
+ ### Other systems
41
+
42
+ We require pkg-config for native library compilation, a C++ compiler, ImageMagick and OpenCV4.
43
+
44
+ ### I have met all the requirements
45
+
46
+ Install this gem with:
47
+
48
+ $ gem install credo detector
49
+
50
+ You can also use it in your app, in which case add
51
+
52
+ gem "credo-detector"
53
+
54
+ to your Gemfile.
16
55
 
17
56
  ## Usage
18
57
 
19
- TODO: Write usage instructions here
58
+ ### CLI
59
+
60
+ We do not support sending results to Credo since we're still haven't validated the detection, but you can calibrate your cameras and run the code locally with:
61
+
62
+ $ credo calibrate
63
+
64
+ and run the detection with
65
+
66
+ $ credo detect
67
+
68
+ You can choose camera with `--camera={camera_id}`. Cameras are numbered starting from 0.
69
+
70
+ If you want to run multiple cameras run the application multiple times with different camera ids.
20
71
 
21
72
  ## Development
22
73
 
@@ -26,7 +77,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
26
77
 
27
78
  ## Contributing
28
79
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/credo-detector. 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]/credo-detector/blob/master/CODE_OF_CONDUCT.md).
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/khasinski/credo-detector. 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]/credo-detector/blob/master/CODE_OF_CONDUCT.md).
30
81
 
31
82
  ## License
32
83
 
@@ -34,4 +85,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
34
85
 
35
86
  ## Code of Conduct
36
87
 
37
- Everyone interacting in the Credo::Detector project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/credo-detector/blob/master/CODE_OF_CONDUCT.md).
88
+ Everyone interacting in the Credo Detector project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/khasinski/credo-detector/blob/master/CODE_OF_CONDUCT.md).
File without changes
data/exe/credo CHANGED
@@ -12,7 +12,7 @@ help = "usage: credo [options]
12
12
  These are common Credo commands used in various situations:
13
13
 
14
14
  calibrate [--tests=300] [--camera=0] Calibrates the camera
15
- detect [--camera=0] [--config] Starts detection, sends the results back to server
15
+ detect [--camera=0] [--threshold=60] Starts detection
16
16
  version Prints version, same as credo -v or credo --version
17
17
  "
18
18
 
@@ -35,8 +35,17 @@ OptionParser.new do |opt|
35
35
  @camera = [camera_number.to_i, 0].max
36
36
  end
37
37
 
38
+ opt.on("-t", "--threshold=60", Integer, "Sets the minimum detection threshold, 0-255") do |threshold|
39
+ @threshold = [[threshold.to_i, 0].max, 255].min
40
+ end
41
+
38
42
  opt.on("calibrate") do
39
- Credo::Calibration.calibrate(tests: @tests.to_i, device_id: @camera.to_i)
43
+ Credo::Calibration.calibrate(tests: @tests.to_i || 0, device_id: @camera.to_i)
44
+ exit
45
+ end
46
+
47
+ opt.on("detect") do
48
+ Credo::Detector.detect(device_id: @camera.to_i, threshold: @threshold || 60)
40
49
  exit
41
50
  end
42
51
  end.parse!
@@ -1,7 +1,9 @@
1
1
  require "mkmf"
2
+ require "pkg-config"
2
3
 
3
4
  $CXXFLAGS << " -std=c++11 "
4
- $INCFLAGS << " -I/opt/homebrew/opt/opencv/include/opencv4/ "
5
- $LDFLAGS << " -L/opt/homebrew/opt/opencv/lib -lopencv_core -lopencv_videoio"
5
+
6
+ $INCFLAGS << " " + PKGConfig.cflags("opencv4")
7
+ $LDFLAGS << " " + PKGConfig.libs("opencv4")
6
8
 
7
9
  create_makefile("grab_frame_ext")
@@ -16,11 +16,9 @@ module Credo
16
16
  puts "Calibration completed"
17
17
  puts "Maximum value: #{max_values.max}"
18
18
  puts "Average of maximum values: #{max_values.sum.to_f / max_values.size}"
19
- rescue Interrupt, RuntimeError
20
- puts
21
- puts "Calibration interrupted"
22
19
  ensure
23
20
  Framegrabber.release
21
+ "Calibration interrupted"
24
22
  end
25
23
 
26
24
  def self.grab_frame
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Credo
4
4
  module Detector
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "detector/version"
4
+ require_relative "../framegrabber"
5
+ require "grab_frame_ext"
4
6
  require "date"
7
+ require "rmagick"
5
8
 
6
9
  module Credo
7
10
  module Detector
@@ -10,41 +13,64 @@ module Credo
10
13
  class Error < StandardError; end
11
14
 
12
15
  def self.detect(device_id: 0, threshold: 60)
13
- @last_ping = linux_time
14
- @last_detection_time = 0
15
-
16
16
  Framegrabber.open(device_id)
17
17
 
18
- sample_number = sample_save = 0
19
- print("Start sampling...")
20
- sleep(2)
18
+ detections = 0
19
+
21
20
  loop do
22
- time_dat = DateTime.now.strftime("%Y-%m-%d %H:%M:%S:%f")
23
- detection_time = linux_time
24
- sample_number += 1
25
21
  grab_frame!
26
- print("\r Sample: #{sample_number} | Saved #{sample_save} | Press Ctrl + c to exit")
27
22
  sleep(0.000001)
23
+ subpixels = @frame.flatten
24
+
25
+ next print "\rToo much light detected, please cover your camera" if too_bright?(subpixels, threshold)
26
+ print "\rBrightest subpixel: #{subpixels.max}"
27
+ next if subpixels.max < threshold
28
+
29
+ detections += 1
28
30
 
29
- counter = 0
30
- xy_coordinates = []
31
+ multi_detections = 0
32
+ @frame.each.with_index do |row, x|
33
+ row.each.with_index do |pixel, y|
34
+ next if pixel.max < threshold
31
35
 
32
- flattened = @frame.flatten
33
- coords = []
34
- if flattened.any? { |subpixel| subpixel > threshold }
35
- @frame.each_with_index do |row, x|
36
- row.each_with_index do |pixel, y|
37
- coords << [x, y] if pixel.any? { |subpixel| subpixel > threshold }
38
- end
36
+ multi_detections += 1
37
+
38
+ cropped = crop(x, y)
39
+ filename = "detections/Sample no. #{detections}:#{multi_detections}.png"
40
+
41
+ save_hit(cropped, filename)
42
+ puts "\n#{filename} saved"
39
43
  end
40
44
  end
41
45
  end
42
46
  ensure
43
47
  Framegrabber.release
48
+ puts "Camera disabled"
44
49
  end
45
50
 
46
51
  private
47
52
 
53
+ def self.save_hit(cropped, filename)
54
+ image = Magick::Image.constitute(cropped.first.size, cropped.size, "I", bgr_to_gray(cropped))
55
+ image.write(filename) do |img|
56
+ img.depth = 8
57
+ img.format = "png"
58
+ end
59
+ end
60
+
61
+ def self.crop(x, y)
62
+ @frame.slice(x-10, 20).map { |row| row.slice(y-10, 20) }
63
+ end
64
+
65
+ def self.bgr_to_gray(image)
66
+ # https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html#color_convert_rgb_gray
67
+ image.flatten(1).map { |bgr| (0.299 * bgr[2] + 0.587 * bgr[1] + 0.114 * bgr[0])/255.0 }
68
+ end
69
+
70
+ def self.too_bright?(subpixels, threshold)
71
+ subpixels.sum.fdiv(subpixels.size) >= threshold
72
+ end
73
+
48
74
  def self.grab_frame!
49
75
  @frame = Framegrabber.grab_frame
50
76
  end
@@ -55,6 +81,49 @@ module Credo
55
81
  end
56
82
  end
57
83
 
58
- require_relative "../framegrabber"
59
- require_relative "calibration"
60
- require "grab_frame_ext"
84
+
85
+ # time.sleep(0.000001)
86
+ # counter = 0
87
+ # xy_coordinates = []
88
+ # if np.max(data) >= int(threshold):
89
+ #
90
+ # all_ziped = list(zip(list(np.where(data >= int(threshold))[1]), list(np.where(data >= int(threshold))[0])))
91
+ # all_ziped.sort()
92
+ #
93
+ # while counter < len(all_ziped):
94
+ # if len(all_ziped) == 1:
95
+ # xy_coordinates.append(all_ziped[0])
96
+ # break
97
+ # elif counter == 0:
98
+ # xy_coordinates.append(all_ziped[counter])
99
+ # counter += 1
100
+ # elif all_ziped[counter][0] - 10 < all_ziped[counter - 1][0]:
101
+ # counter += 1
102
+ # else:
103
+ # xy_coordinates.append(all_ziped[counter])
104
+ # counter += 1
105
+ # multi_detection = 0
106
+ # for x, y in xy_coordinates:
107
+ # if x >= 11 and y >= 11:
108
+ # img_crop = data[y-10:y + 10,
109
+ # x-10:x + 10]
110
+ # r = 50.0 / img_crop.shape[1]
111
+ # dim = (50, int(img_crop.shape[0] * r))
112
+ # sample_save = sample_save + 1
113
+ # if img_crop is None:
114
+ # pass
115
+ # else:
116
+ # print(time_dat, sample_number, x, y,
117
+ # round(np.average(data), 4), np.max(data),
118
+ # sep=',', file=open(str(path) + "/Report.txt", "a"))
119
+ # img_zoom = cv2.resize(img_crop, dim, interpolation=cv2.INTER_AREA)
120
+ # gray_img_zoom = cv2.cvtColor(img_zoom, cv2.COLOR_BGR2GRAY)
121
+ # cv2.imwrite(str(path) + "/" + str(time_dat) +
122
+ # " Sample no. %i:%i.png" % (sample_number,multi_detection), gray_img_zoom)
123
+ # picture = open(str(path) + "/" + str(time_dat) +" Sample no. %i:%i.png"
124
+ # % (sample_number,multi_detection), 'rb')
125
+ # picture_read = picture.read()
126
+ # base64_picture = str(base64.b64encode(picture_read))[2:]
127
+ # multi_detection += 1
128
+
129
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credo-detector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Hasiński
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-09-04 00:00:00.000000000 Z
12
+ date: 2022-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler
@@ -25,20 +25,34 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: pkg-config
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.4'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.4'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: rmagick
30
44
  requirement: !ruby/object:Gem::Requirement
31
45
  requirements:
32
- - - ">="
46
+ - - "~>"
33
47
  - !ruby/object:Gem::Version
34
- version: '0'
48
+ version: '4.0'
35
49
  type: :runtime
36
50
  prerelease: false
37
51
  version_requirements: !ruby/object:Gem::Requirement
38
52
  requirements:
39
- - - ">="
53
+ - - "~>"
40
54
  - !ruby/object:Gem::Version
41
- version: '0'
55
+ version: '4.0'
42
56
  description:
43
57
  email:
44
58
  - krzysztof.hasinski@gmail.com
@@ -56,6 +70,7 @@ files:
56
70
  - LICENSE.txt
57
71
  - README.md
58
72
  - Rakefile
73
+ - detections/.gitkeep
59
74
  - exe/credo
60
75
  - ext/grab_frame_ext/extconf.rb
61
76
  - ext/grab_frame_ext/grab_frame_ext.cpp
@@ -78,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
93
  requirements:
79
94
  - - ">="
80
95
  - !ruby/object:Gem::Version
81
- version: 2.6.0
96
+ version: '2.7'
82
97
  required_rubygems_version: !ruby/object:Gem::Requirement
83
98
  requirements:
84
99
  - - ">="