-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from fetlife/switch-to-magnus
Replace `rutie` with `magnus`
- Loading branch information
Showing
10 changed files
with
337 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/target | ||
Gemfile.lock | ||
.ruby-version | ||
*.gem |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.