Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ This project is quite experimental so do not expect great results. More classes

### Useful functions

At the moment only `load_jpeg_data` function to load a jpeg file and get important jpeg data (width, height, dpi and raw data).
At the moment you can try `load_jpeg_data` function to load a jpeg file and get important jpeg data (width, height, dpi and raw data).
See grayscale_example or grayscale_m_example to see the function in action.
Try `yape06_detect` function as a compact version for the yape06 detect routine. See it in action in the yape06_detect_video_example.html

## Libs
Libs are stored in `build` folder:
Expand Down Expand Up @@ -90,6 +91,7 @@ Take a look at our examples in the examples folder:
- resample_video_example.html
- sample_orb_mixed.html
- yape06_video_example.html
- yape06_detect_video_example.html

Both examples use the debug version of the lib but, of course you can use the non-debug version as well.

Expand All @@ -103,4 +105,7 @@ For the first time run:
this will build all the libs. After that if you need to compile and build only jsfeat libs:
`./build.sh emscripten`
and the two libs will be compiled in the build directory.

If you run under Windows use the `./build.Unix.sh` script instead.

We used emsdk 3.1.20.
104 changes: 104 additions & 0 deletions build.Unix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env bash
function usage {
echo "Usage: $(basename $0) [--debug | --clean-em] ( libar | linux | emscripten | emscripten-all )"
exit 1
}

if [ $# -eq 0 ]; then
usage
fi

# -e = exit on errors
set -e

# Parse parameters
while test $# -gt 0
do
case "$1" in
libar) BUILD_LIBAR=1
;;
linux) BUILD_LINUX=1
;;
emscripten) BUILD_EM=1
;;
emscripten-all) BUILD_EM_ALL=1
;;
--clean-em) CLEAN_EM=1
;;
--*) echo "bad option $1"
usage
;;
*) echo "bad argument $1"
usage
;;
esac
shift
done

# Set OS-dependent variables.
OS=`uname -s`
ARCH=`uname -m`
TAR='/usr/bin/tar'
if [ "$OS" = "Linux" ]
then
CPUS=`/usr/bin/nproc`
TAR='/bin/tar'
# Identify Linux OS. Sets useful variables: ID, ID_LIKE, VERSION, NAME, PRETTY_NAME.
source /etc/os-release
# Windows Subsystem for Linux identifies itself as 'Linux'. Additional test required.
if grep -qE "(Microsoft|WSL)" /proc/version &> /dev/null ; then
OS='Windows'
fi
else
CPUS=1
fi

if [ $BUILD_LIBAR ] ; then
npm run build
fi

if [ $BUILD_LINUX ] ; then
g++ -std=gnu++17 -Isrc test.cpp -o test
g++ -std=gnu++17 -Isrc test.cpp -g -o test_d
fi

if [ $BUILD_EM ]; then
echo "Entering in build folder"
cd build
echo "Building jsfeatcpp.js with emscripten (emcmake)..."
emcmake cmake .. -DCMAKE_BUILD_TYPE="Release"
echo "Running command make..."
make
echo "Building jsfeatcpp_debug.js with emscripten (emcmake)..."
emcmake cmake .. -DCMAKE_BUILD_TYPE="Debug"
echo "Running command make..."
make
echo "Build completed."
fi

if [ $BUILD_EM_ALL ]; then
echo "Building libar libs"
npm run build
echo "Done!"
echo "preparing to build jsfeat libs..."
echo "Entering in build folder"
cd build
echo "Building jsfeatcpp.js with emscripten (emcmake)..."
emcmake cmake .. -DCMAKE_BUILD_TYPE="Release"
echo "Running command make..."
make
echo "Building jsfeatcpp_debug.js with emscripten (emcmake)..."
emcmake cmake .. -DCMAKE_BUILD_TYPE="Debug"
echo "Running command make..."
make
echo "Build completed."
fi

if [ $CLEAN_EM ]; then
echo "Entering in build folder"
cd build
echo "Cleaning build folder."
rm -rf CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
rm -rf ./jsfeatcpp.js ./jsfeatcpp_debug.js
echo "Removed files."
fi
2 changes: 1 addition & 1 deletion build/jsfeatES6cpp.js

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions build/jsfeatES6cpp_debug.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/jsfeatcpp.js

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions build/jsfeatcpp_debug.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions emscripten/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,6 @@ EMSCRIPTEN_BINDINGS(webarkit) {
// Extern jsfeat functions

function("load_jpeg_data", &load_jpeg_data);
function("yape06_detect", &yape06_detect);

};
32 changes: 31 additions & 1 deletion emscripten/webarkitJsfeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,37 @@ emscripten::val load_jpeg(const char* filename) {
emscripten::val load_jpeg_data(std::string filename) {
auto out = load_jpeg(filename.c_str());
return out;
}
};

emscripten::val yape06_detect(emscripten::val inputSrc, int w, int h) {
auto src = emscripten::convertJSArrayToNumberVector<u_char>(inputSrc);
Imgproc imgproc;
Yape06 yape;

std::unique_ptr<KeyPoints> keypoints = std::make_unique<KeyPoints>(w * h);
std::unique_ptr<Matrix_t> lev0_img = std::make_unique<Matrix_t>(w, h, ComboTypes::U8C1_t);

imgproc.grayscale_internal<u_char, Matrix_t>(src.data(), w, h, lev0_img.get(), Colors::COLOR_RGBA2GRAY);
imgproc.gaussian_blur_internal(lev0_img.get(), lev0_img.get(), 5, 2);
auto obj = yape.detect_internal(lev0_img.get(), keypoints.get(), 17);

emscripten::val outObj = emscripten::val::object();
emscripten::val pointsArr = emscripten::val::array();
KPoint_t pt;
for (auto i = 0; i < obj.pts.kpoints.size(); i++) {
pt.x = obj.pts.kpoints[i].x;
pt.y = obj.pts.kpoints[i].y;
pt.level = obj.pts.kpoints[i].level;
pt.score = obj.pts.kpoints[i].score;
pt.angle = obj.pts.kpoints[i].angle;
pointsArr.call<void>("push", pt);
}
outObj.set("count", obj.count);
outObj.set("points", pointsArr);

return outObj;
};

}

#include "bindings.cpp"
119 changes: 119 additions & 0 deletions examples/yape06_detector_video_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JsfeatCpp video grayscale example</title>
<link rel="stylesheet" href="css/style.css">
</head>

<body>
<video id="video" autoplay loop muted playsinline></video>
<canvas id="canvas"></canvas>

<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

window.addEventListener('jsfeatCpp-loaded', function (e) {
var jsfeatCpp = Module;
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
var hint = {
audio: false,
video: true
};
if (window.innerWidth < 800) {
var width = (window.innerWidth < window.innerHeight) ? 240 : 360;
var height = (window.innerWidth < window.innerHeight) ? 360 : 240;

var aspectRatio = window.innerWidth / window.innerHeight;

console.log(width, height);

hint = {
audio: false,
video: {
facingMode: 'environment',
width: {
min: width,
max: width
}
},
};

console.log(hint);
}

navigator.mediaDevices.getUserMedia(hint).then(function (stream) {
video.srcObject = stream;
video.addEventListener('loadedmetadata', function () {
video.play();

console.log('video', video, video.videoWidth, video.videoHeight);

var canvasWidth = video.videoWidth;
var canvasHeight = video.videoHeight;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
process();
});
});

function moveBytes(src, dest, width, height) {
var i, j = 0;
for (i = 0; i < width * height * 4;) {
dest[i++] = src[j];
dest[i++] = src[j];
dest[i++] = src[j++];
dest[i++] = 255;
}
}

function render_mono_image(src, dst, sw, sh, dw) {
var alpha = (0xff << 24);
for (var i = 0; i < sh; ++i) {
for (var j = 0; j < sw; ++j) {
var pix = src[i * sw + j];
dst[i * dw + j] = alpha | (pix << 16) | (pix << 8) | pix;
}
}
}
function render_corners(corners, count, img, step) {
var pix = (0xff << 24) | (0x00 << 16) | (0xff << 8) | 0x00;
for(var i=0; i < count; ++i)
{
var x = corners[i].x;
var y = corners[i].y;
var off = (x + y * step);
img[off] = pix;
img[off-1] = pix;
img[off+1] = pix;
img[off-step] = pix;
img[off+step] = pix;
}
}

function process() {
var width = 640,
height = 480;
ctx.drawImage(video, 0, 0, width, height);
var image_data = ctx.getImageData(0, 0, width, height);
var obj = jsfeatCpp.yape06_detect(image_data.data, width, height);
// console.log("count is: ", obj);
var data_u32 = new Uint32Array(image_data.data.buffer);
// we convert to mono gray image, check both methods
//render_mono_image(img_u8.data, data_u32, width, height, 640)
render_corners(obj.points, obj.count, data_u32, 640);
//moveBytes(img_u8.data, image_data.data, width, height);
ctx.putImageData(image_data, 0, 0);
requestAnimationFrame(process);
}
}
})
</script>
<script src="../build/jsfeatcpp.js"></script>
</body>

</html>