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
2 changes: 1 addition & 1 deletion .github/workflows/build_opencv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
${{ runner.os }}-node-
- run: git submodule update --init
- run: npm install
- run: docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) -e "EMSCRIPTEN=/emsdk/upstream/emscripten" emscripten/emsdk:3.1.26 emcmake python3 ./opencv/platforms/js/build_js.py opencv_js --config="./opencv.webarkit_config.py" --build_wasm --cmake_option="-DBUILD_opencv_dnn=OFF" --cmake_option="-DBUILD_opencv_objdetect=OFF" --cmake_option="-DBUILD_opencv_photo=OFF" --build_flags="-s EXPORT_ES6=1 -s USE_ES6_IMPORT_META=0"
- run: docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) -e "EMSCRIPTEN=/emsdk/upstream/emscripten" emscripten/emsdk:3.1.69 emcmake python3 ./opencv/platforms/js/build_js.py opencv_js --config="./opencv.webarkit_config.py" --build_wasm --cmake_option="-DBUILD_opencv_dnn=OFF" --cmake_option="-DBUILD_opencv_objdetect=OFF" --cmake_option="-DBUILD_opencv_photo=OFF"--cmake_option="-DCPU_BASELINE=DETECT" --cmake_option="-DCPU_DISPATCH=" --cmake_option="-DCPU_BASELINE_DISABLE=SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2;POPCNT;AVX;FP16;FMA3;AVX2;AVX_512F" --build_flags="-fwasm-exceptions -mbulk-memory -mnontrapping-fptoint -s WASM_BIGINT=1 -s SUPPORT_LONGJMP=wasm -s EXPORT_ES6=1 -s USE_ES6_IMPORT_META=0 -s ENVIRONMENT=web,worker"
- run: cp -r opencv_js/bin/opencv_js.js build
- uses: actions/upload-artifact@v4
with:
Expand Down
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@

# webarkit-cv

A set of tools to build a custom and light opencv lib to be used in WebAR projects. This is a WIP, but at this moment opencv_js.js can be imported as a **ES6** module.
The main goal of this project is to develop a WebAR library that can be used to build a WebAR application, based on the OpenCV library.
We do not reccomend to use this library in production, but if you want to try it, you can clone the project and test the examples. The API and the structure of the project can change in the future, so be aware of this.
WebARKitCV is an opinionated toolchain for producing lightweight OpenCV builds tailored to WebAR workloads. It bundles the scripts, configuration, and TypeScript helpers we use to compile our fork of OpenCV into a single-file ES module (`opencv_js.js`) that runs in modern browsers and Web Workers.

The project currently targets OpenCV **4.12.0** (forked under `webarkit/opencv`) and **emsdk 3.1.69**. Expect breaking changes while we continue to tune the build for smaller payloads and a smoother developer experience; treat the artifacts as experimental until we cut a stable release.

Getting started is as simple as cloning the repo, running one of the build scripts (`build.sh` on Unix, `build_w_docker.bat` on Windows), and loading the generated example pages under `examples/` to validate your environment. Feedback and contributions are welcome—see the Issues section for discussion topics and the roadmap.

## Typescript

WebARKitCV is developed in the Typescript language; Type definitions are in the `types` folder.

## OpenCV and Emscripten emsdk
We are using OpenCV version 4.7.0 (our modified fork) and emsdk 3.1.26

We are using OpenCV version 4.12.0 (our modified fork) and emsdk 3.1.69

### Tools

See the build.sh script. It will build the opencv_js.js lib and after it will copy in the build folder. We are trying to build a lighter opencv_js.js lib,
So we use a config to minimize the file size. Without this the final lib will be too big in size with features that we don't need.
The opencv_js.js file is based on a modified version of OpenCV that let you import the OpenCV library as a ES6 module. If you are interested read this [issue](https://github.com/kalwalt/webarkit-cv/issues/1).
See the build scripts below; each one compiles the OpenCV ES module and copies it into the `build` folder. We are trying to build a lighter `opencv_js.js`, so we use a custom config to trim features we do not currently need. The module is based on our forked OpenCV tree, which adds ES module support—details live in [issue #1](https://github.com/kalwalt/webarkit-cv/issues/1).

### Build scripts

- `build.sh`: native Linux workflow that assumes `emsdk`/Emscripten is installed and on your `PATH`.
- `build_w_docker.sh`: Linux/macOS workflow that wraps the build in the official Emscripten Docker image—no local `emsdk` install required.
- `build_w_docker.bat`: Windows PowerShell/Command Prompt variant that uses the same Docker-based flow.

### Future development

- npm package
- documentation
- first WebAR example
- first WebAR example
14 changes: 7 additions & 7 deletions build/opencv_js.js

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions build_w_docker.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@echo off
setlocal enabledelayedexpansion

set "OUTPUT_DIR=opencv_js"
set "BUILD_DIR=build"

if not exist "%OUTPUT_DIR%" (
mkdir "%OUTPUT_DIR%"
rem Grant broad access so Docker can write build artifacts when using Windows file permissions
icacls "%OUTPUT_DIR%" /grant *S-1-1-0:^(OI^)(CI^)F >nul 2>&1
echo mkdir opencv_js
)

set "UID="
set "GID="
set "DOCKER_USER="
for /f "usebackq tokens=* delims=" %%i in (`wsl id -u 2^>nul`) do set "UID=%%i"
for /f "usebackq tokens=* delims=" %%i in (`wsl id -g 2^>nul`) do set "GID=%%i"
if defined UID if defined GID (
set "DOCKER_USER=-u %UID%:%GID%"
)

docker run --rm -v "%cd%:/src" %DOCKER_USER% -e "EMSCRIPTEN=/emsdk/upstream/emscripten" emscripten/emsdk:3.1.69 emcmake python3 ./opencv/platforms/js/build_js.py %OUTPUT_DIR% --config="./opencv.webarkit_config.py" --build_wasm --cmake_option="-DBUILD_opencv_dnn=OFF" --cmake_option="-DBUILD_opencv_objdetect=OFF" --cmake_option="-DBUILD_opencv_photo=OFF" --cmake_option="-DCPU_BASELINE=DETECT" --cmake_option="-DCPU_DISPATCH=" --cmake_option="-DCPU_BASELINE_DISABLE=SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2;POPCNT;AVX;FP16;FMA3;AVX2;AVX_512F" --build_flags="-fwasm-exceptions -mbulk-memory -mnontrapping-fptoint -s WASM_BIGINT=1 -s SUPPORT_LONGJMP=wasm -s EXPORT_ES6=1 -s USE_ES6_IMPORT_META=0 -s MODULARIZE=1 -s ENVIRONMENT=web,worker"
if errorlevel 1 (
echo Docker build failed.
exit /b 1
)

if not exist "%BUILD_DIR%" (
mkdir "%BUILD_DIR%"
)

copy /Y "%OUTPUT_DIR%\bin\opencv_js.js" "%BUILD_DIR%\opencv_js.js" >nul
if errorlevel 1 (
echo Failed to copy opencv_js.js to %BUILD_DIR%.
exit /b 1
)

echo Build completed successfully.
endlocal
18 changes: 11 additions & 7 deletions build_w_docker.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail

if [ ! -d "opencv_js" ] ; then
mkdir opencv_js
sudo chmod -R 777 opencv_js
echo "mkdir opencv_js"
OUTPUT_DIR="opencv_js"
BUILD_DIR="build"

if [ ! -d "${OUTPUT_DIR}" ]; then
mkdir -p "${OUTPUT_DIR}"
chmod -R a+rwx "${OUTPUT_DIR}" 2>/dev/null || sudo chmod -R 777 "${OUTPUT_DIR}" > /dev/null
echo "mkdir ${OUTPUT_DIR}"
fi

docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) -e "EMSCRIPTEN=/emsdk/upstream/emscripten" emscripten/emsdk:3.1.26 emcmake python3 ./opencv/platforms/js/build_js.py opencv_js --config="./opencv.webarkit_config.py" --build_wasm --cmake_option="-DBUILD_opencv_dnn=OFF" --cmake_option="-DBUILD_opencv_objdetect=OFF" --cmake_option="-DBUILD_opencv_photo=OFF" --build_flags="-s EXPORT_ES6=1 -s USE_ES6_IMPORT_META=0 -s MODULARIZE=1"
docker run --rm -v "$(pwd)":/src -u "$(id -u):$(id -g)" -e "EMSCRIPTEN=/emsdk/upstream/emscripten" emscripten/emsdk:3.1.69 emcmake python3 ./opencv/platforms/js/build_js.py "${OUTPUT_DIR}" --config="./opencv.webarkit_config.py" --build_wasm --cmake_option="-DBUILD_opencv_dnn=OFF" --cmake_option="-DBUILD_opencv_objdetect=OFF" --cmake_option="-DBUILD_opencv_photo=OFF" --cmake_option="-DCPU_BASELINE=DETECT" --cmake_option="-DCPU_DISPATCH=" --cmake_option="-DCPU_BASELINE_DISABLE=SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2;POPCNT;AVX;FP16;FMA3;AVX2;AVX_512F" --build_flags="-fwasm-exceptions -mbulk-memory -mnontrapping-fptoint -s WASM_BIGINT=1 -s SUPPORT_LONGJMP=wasm -s EXPORT_ES6=1 -s USE_ES6_IMPORT_META=0 -s MODULARIZE=1 -s ENVIRONMENT=web,worker"

# copy the output to the build folder
cp -r opencv_js/bin/opencv_js.js build
mkdir -p "${BUILD_DIR}"
cp -f "${OUTPUT_DIR}/bin/opencv_js.js" "${BUILD_DIR}/opencv_js.js"
Loading