Skip to content

Commit

Permalink
Merge pull request #16 from fetlife/switch-to-magnus
Browse files Browse the repository at this point in the history
Replace `rutie` with `magnus`
  • Loading branch information
Antti authored Jan 10, 2024
2 parents 8022811 + cbfb9b6 commit 1922206
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 176 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ jobs:
- uses: ruby/setup-ruby-pkgs@v1
with:
ruby-version: 3.3.0
- run: sudo apt-get update && sudo apt-get install -y libopencv-dev
- run: sudo apt-get update && sudo apt-get install -y libopencv-dev libvips
- run: gem update --system
- run: gem build libfacedetection.gemspec
- run: gem install $(ls -1 *.gem)
- run: rake gem
- run: gem install $(ls -1 pkg/*.gem)
- run: bundle install
- run: ruby tests/test_detection.rb
# compile_native_gem:
# name: Compile native gem
# runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/target
Gemfile.lock
.ruby-version
*.gem
183 changes: 151 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ authors = ["Fetlife <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[lib]
name = "libfacedetection_ruby"
Expand Down
47 changes: 47 additions & 0 deletions lib/libfacedetection/facedetect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Libfacedetection
module FaceDetection
require 'ruby-vips'
require 'ffi'

LIB_FACEDETECTION_MAX_SIZE = 400.0

extend self

# vips_img: Vips::Image
def detect_faces(vips_img)
scale =
if vips_img.width > vips_img.height
LIB_FACEDETECTION_MAX_SIZE / vips_img.width
else
LIB_FACEDETECTION_MAX_SIZE / vips_img.height
end
# convert RGB -> BGR
rgb_bands = vips_img.bandsplit
bgr = Vips::Image.bandjoin([rgb_bands[2], rgb_bands[1], rgb_bands[0]])
# resize to a smaller size - libfacedetection works with smaller images
# or perhaps a larger one, but results are not guaranteed to be better
resized = bgr.resize(scale)

mem = resized.write_to_memory
ptr = FFI::MemoryPointer.from_string(mem)
faces = Libfacedetection.detect_libfacedetection_image_data(ptr.address, resized.width, resized.height)
faces.map do |face|
scale_face(face, scale)
end
end

private

def scale_face(face, scale)
face[:x] = (face[:x] / scale).round
face[:y] = (face[:y] / scale).round
face[:width] = (face[:width] / scale).round
face[:height] = (face[:height] / scale).round
face[:landmarks] = face[:landmarks].map do |landmark|
[(landmark[0] / scale).round, (landmark[1] / scale).round]
end
face
end
end

end
Loading

0 comments on commit 1922206

Please sign in to comment.