Skip to content

Commit 1922206

Browse files
authored
Merge pull request #16 from fetlife/switch-to-magnus
Replace `rutie` with `magnus`
2 parents 8022811 + cbfb9b6 commit 1922206

File tree

10 files changed

+337
-176
lines changed

10 files changed

+337
-176
lines changed

.github/workflows/workflow.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ jobs:
99
- uses: ruby/setup-ruby-pkgs@v1
1010
with:
1111
ruby-version: 3.3.0
12-
- run: sudo apt-get update && sudo apt-get install -y libopencv-dev
12+
- run: sudo apt-get update && sudo apt-get install -y libopencv-dev libvips
1313
- run: gem update --system
14-
- run: gem build libfacedetection.gemspec
15-
- run: gem install $(ls -1 *.gem)
14+
- run: rake gem
15+
- run: gem install $(ls -1 pkg/*.gem)
16+
- run: bundle install
17+
- run: ruby tests/test_detection.rb
1618
# compile_native_gem:
1719
# name: Compile native gem
1820
# runs-on: ubuntu-latest

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/target
66
Gemfile.lock
77
.ruby-version
8+
*.gem

Cargo.lock

+151-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ authors = ["Fetlife <[email protected]>"]
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
11-
anyhow = "1.0.62"
1211
libfacedetection = { git = "https://github.com/fetlife/libfacedetection-rs.git", optional = true, rev="7426f33ba101514932a5ef58456761735c7bf3dc" }
13-
opencv = { version = "0.66", optional = true }
14-
rutie = { version="0.8.4", features = ["no-link"] }
12+
opencv = { version = "0.88.6", optional = true, features=["clang-runtime"] }
13+
magnus = { version="0.6.2" }
1514

1615
[lib]
1716
name = "libfacedetection_ruby"

lib/libfacedetection/facedetect.rb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Libfacedetection
2+
module FaceDetection
3+
require 'ruby-vips'
4+
require 'ffi'
5+
6+
LIB_FACEDETECTION_MAX_SIZE = 400.0
7+
8+
extend self
9+
10+
# vips_img: Vips::Image
11+
def detect_faces(vips_img)
12+
scale =
13+
if vips_img.width > vips_img.height
14+
LIB_FACEDETECTION_MAX_SIZE / vips_img.width
15+
else
16+
LIB_FACEDETECTION_MAX_SIZE / vips_img.height
17+
end
18+
# convert RGB -> BGR
19+
rgb_bands = vips_img.bandsplit
20+
bgr = Vips::Image.bandjoin([rgb_bands[2], rgb_bands[1], rgb_bands[0]])
21+
# resize to a smaller size - libfacedetection works with smaller images
22+
# or perhaps a larger one, but results are not guaranteed to be better
23+
resized = bgr.resize(scale)
24+
25+
mem = resized.write_to_memory
26+
ptr = FFI::MemoryPointer.from_string(mem)
27+
faces = Libfacedetection.detect_libfacedetection_image_data(ptr.address, resized.width, resized.height)
28+
faces.map do |face|
29+
scale_face(face, scale)
30+
end
31+
end
32+
33+
private
34+
35+
def scale_face(face, scale)
36+
face[:x] = (face[:x] / scale).round
37+
face[:y] = (face[:y] / scale).round
38+
face[:width] = (face[:width] / scale).round
39+
face[:height] = (face[:height] / scale).round
40+
face[:landmarks] = face[:landmarks].map do |landmark|
41+
[(landmark[0] / scale).round, (landmark[1] / scale).round]
42+
end
43+
face
44+
end
45+
end
46+
47+
end

0 commit comments

Comments
 (0)