Building OpenCV.js error, help please

I am trying to build OpenCV.js
I have installed python3 and EMSCRIPTEN
I downloaded OpenCV 4.8.0 release

When I try to run:
emcmake python ./opencv/platforms/js/build_js.py build_js

I get the following error:

configure: python ./opencv/platforms/js/build_js.py build_js -DCMAKE_TOOLCHAIN_FILE=/home/frank/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/frank/emsdk/node/16.20.0_64bit/bin/node
emcmake: error: 'python ./opencv/platforms/js/build_js.py build_js -DCMAKE_TOOLCHAIN_FILE=/home/frank/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/frank/emsdk/node/16.20.0_64bit/bin/node' failed: [Errno 2] No such file or directory: 'python'

When I try to run:
emcmake python3 ./opencv/platforms/js/build_js.py build_js

I get the following error:

configure: python3 ./opencv/platforms/js/build_js.py build_js -DCMAKE_TOOLCHAIN_FILE=/home/frank/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/frank/emsdk/node/16.20.0_64bit/bin/node
usage: build_js.py [-h] [--opencv_dir OPENCV_DIR]
                   [--emscripten_dir EMSCRIPTEN_DIR] [--build_wasm]
                   [--disable_wasm] [--disable_single_file] [--threads]
                   [--simd] [--build_test] [--build_perf] [--build_doc]
                   [--build_loader] [--clean_build_dir] [--skip_config]
                   [--config_only] [--enable_exception]
                   [--cmake_option CMAKE_OPTION] [--build_flags BUILD_FLAGS]
                   [--build_wasm_intrin_test] [--config CONFIG] [--webnn]
                   build_dir
build_js.py: error: unrecognized arguments: -DCMAKE_TOOLCHAIN_FILE=/home/frank/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/frank/emsdk/node/16.20.0_64bit/bin/node
emcmake: error: 'python3 ./opencv/platforms/js/build_js.py build_js -DCMAKE_TOOLCHAIN_FILE=/home/frank/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/frank/emsdk/node/16.20.0_64bit/bin/node' failed (returned 2)

I would really appreciate your help

1 Like

you’re on windows, eh?

i’vve never done this, but script looks, like you have to add your cmake options like:

--cmake_option="-Dop1=x -Dop2=y"

also, the toolcchain file seems already handled here

take a look :

No, I am on Linux (Ubuntu)

Yeah, explicitly passing cmake options is necessary to get emcmake to begin building. This is the command I ran.

emcmake python3 opencv/platforms/js/build_js.py build_asm --disable_wasm --emscripten_dir="/mnt/c/users/magma/emsdk/upstream/emscripten" --cmake_option="-DCMAKE_TOOLCHAIN_FILE=/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" --cmake_option="-DCMAKE_CROSSCOMPILING_EMULATOR=/mnt/c/Users/magma/emsdk/node/16.20.0_64bit/bin/node"

That being said, upon build completion, opencv.js doesn’t seem to work. When I load it and attempt to use an openCV function, I get a TypeError saying it is not a function. When I replace the opencv.js file I built with the downloadable base one, the same program works. Anyone know what might be the cause of this? Should I move this inquiry into a new thread?

Code in question:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title> Please work </title>
	</head>
	<body>
		<h2 id="checker" style="color: green;">OpenCV is loading...</h2>
		<script type="text/javascript">
			function opencvcheck(){
				document.getElementById('checker').innerHTML="OpenCV is ready. (hallelujah)"
				console.log(cv.getBuildInformation());
						}
		</script>
		<script async src="./opencv.js" onload="opencvcheck()"
			      type="text/javascript"></script>

	</body>
</html>

The error that comes about using custom built opencv.js (with the command I put above)

Uncaught TypeError: cv.getBuildInformation is not a function
    at opencvcheck (example2.html:12:20)
    at HTMLScriptElement.onload (example2.html:16:33)

where is the cv var initialized ?
(can’t see anything in your setup, and it probably isn’t.)

(and ty for confirming the --cmake_option issue above !!)

1 Like

I’m pretty sure cv is initialized as a global var within opencv.js. When I swap my built opencv.js for the stock downloaded one, the setup I posted works as is. Maybe the built opencv.js doesn’t have a defined cv var?

EDIT: According to this, “once opencv.js is ready, you can access OpenCV objects and functions through cv object.”

So, I’m not quite sure :confused: According to this, calling cv should work without explicit initialization, which is why I’m leaning towards thinking I’m doing something wrong in the actual building process somehow.

Hi Tarek,
I can’t even get the build to work.
Here is what I get.

emcmake python3 opencv/platforms/js/build_js.py build_asm --disable_wasm --emscripten_dir=“/mnt/c/users/magma/emsdk/upstream/emscripten” --cmake_option=“-DCMAKE_TOOLCHAIN_FILE=/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake” --cmake_option=“-DCMAKE_CROSSCOMPILING_EMULATOR=/mnt/c/Users/magma/emsdk/node/16.20.0_64bit/bin/node”
configure: python3 opencv/platforms/js/build_js.py build_asm --disable_wasm --emscripten_dir=/mnt/c/users/magma/emsdk/upstream/emscripten --cmake_option=-DCMAKE_TOOLCHAIN_FILE=/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake --cmake_option=-DCMAKE_CROSSCOMPILING_EMULATOR=/mnt/c/Users/magma/emsdk/node/16.20.0_64bit/bin/node
EMSCRIPTEN environment variable is not available. Please properly activate Emscripten SDK and consider using ‘emcmake’ launcher
Args: Namespace(build_dir=‘build_asm’, opencv_dir=‘/home/frank/opencv’, emscripten_dir=‘/mnt/c/users/magma/emsdk/upstream/emscripten’, build_wasm=False, disable_wasm=True, disable_single_file=False, threads=False, simd=False, build_test=False, build_perf=False, build_doc=False, build_loader=False, clean_build_dir=False, skip_config=False, config_only=False, enable_exception=False, cmake_option=[‘-DCMAKE_TOOLCHAIN_FILE=/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake’, ‘-DCMAKE_CROSSCOMPILING_EMULATOR=/mnt/c/Users/magma/emsdk/node/16.20.0_64bit/bin/node’], build_flags=None, build_wasm_intrin_test=False, config=‘/home/frank/opencv/platforms/js/opencv_js.config.py’, webnn=False)
Check dir /home/frank/build_asm (create: True, clean: False)
Check dir /home/frank/opencv (create: False, clean: False)

options.opencv_dir: /home/frank/opencv
Check dir /mnt/c/users/magma/emsdk/upstream/emscripten (create: False, clean: False)

===== Config OpenCV.js build for asm.js

Executing: [‘cmake’, ‘-DPYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3’, ‘-DENABLE_PIC=FALSE’, ‘-DCMAKE_BUILD_TYPE=Release’, “-DCMAKE_TOOLCHAIN_FILE=‘/mnt/c/users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake’”, “-DCPU_BASELINE=‘’”, ‘-DCMAKE_INSTALL_PREFIX=/usr/local’, “-DCPU_DISPATCH=‘’”, ‘-DCV_TRACE=OFF’, ‘-DBUILD_SHARED_LIBS=OFF’, ‘-DWITH_1394=OFF’, ‘-DWITH_ADE=OFF’, ‘-DWITH_VTK=OFF’, ‘-DWITH_EIGEN=OFF’, ‘-DWITH_FFMPEG=OFF’, ‘-DWITH_GSTREAMER=OFF’, ‘-DWITH_GTK=OFF’, ‘-DWITH_GTK_2_X=OFF’, ‘-DWITH_IPP=OFF’, ‘-DWITH_JASPER=OFF’, ‘-DWITH_JPEG=OFF’, ‘-DWITH_WEBP=OFF’, ‘-DWITH_OPENEXR=OFF’, ‘-DWITH_OPENGL=OFF’, ‘-DWITH_OPENVX=OFF’, ‘-DWITH_OPENNI=OFF’, ‘-DWITH_OPENNI2=OFF’, ‘-DWITH_PNG=OFF’, ‘-DWITH_TBB=OFF’, ‘-DWITH_TIFF=OFF’, ‘-DWITH_V4L=OFF’, ‘-DWITH_OPENCL=OFF’, ‘-DWITH_OPENCL_SVM=OFF’, ‘-DWITH_OPENCLAMDFFT=OFF’, ‘-DWITH_OPENCLAMDBLAS=OFF’, ‘-DWITH_GPHOTO2=OFF’, ‘-DWITH_LAPACK=OFF’, ‘-DWITH_ITT=OFF’, ‘-DWITH_QUIRC=ON’, ‘-DBUILD_ZLIB=ON’, ‘-DBUILD_opencv_apps=OFF’, ‘-DBUILD_opencv_calib3d=ON’, ‘-DBUILD_opencv_dnn=ON’, ‘-DBUILD_opencv_features2d=ON’, ‘-DBUILD_opencv_flann=ON’, ‘-DBUILD_opencv_gapi=OFF’, ‘-DBUILD_opencv_ml=OFF’, ‘-DBUILD_opencv_photo=ON’, ‘-DBUILD_opencv_imgcodecs=OFF’, ‘-DBUILD_opencv_shape=OFF’, ‘-DBUILD_opencv_videoio=OFF’, ‘-DBUILD_opencv_videostab=OFF’, ‘-DBUILD_opencv_highgui=OFF’, ‘-DBUILD_opencv_superres=OFF’, ‘-DBUILD_opencv_stitching=OFF’, ‘-DBUILD_opencv_java=OFF’, ‘-DBUILD_opencv_js=ON’, ‘-DBUILD_opencv_python2=OFF’, ‘-DBUILD_opencv_python3=OFF’, ‘-DBUILD_EXAMPLES=ON’, ‘-DBUILD_PACKAGE=OFF’, ‘-DBUILD_TESTS=ON’, ‘-DBUILD_PERF_TESTS=ON’, ‘-DCMAKE_TOOLCHAIN_FILE=/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake’, ‘-DCMAKE_CROSSCOMPILING_EMULATOR=/mnt/c/Users/magma/emsdk/node/16.20.0_64bit/bin/node’, ‘-DBUILD_DOCS=OFF’, ‘-DWITH_PTHREADS_PF=OFF’, ‘-DCV_ENABLE_INTRINSICS=OFF’, ‘-DBUILD_WASM_INTRIN_TESTS=OFF’, “-DCMAKE_C_FLAGS='-s WASM=0 -s SINGLE_FILE=1 -s USE_PTHREADS=0 '”, “-DCMAKE_CXX_FLAGS='-s WASM=0 -s SINGLE_FILE=1 -s USE_PTHREADS=0 '”, ‘/home/frank/opencv’]
Re-run cmake no build system arguments
CMake Error at /usr/share/cmake-3.22/Modules/CMakeDetermineSystem.cmake:130 (message):
Could not find toolchain file:
/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
Call Stack (most recent call first):
CMakeLists.txt:113 (enable_language)

CMake Error: CMake was unable to find a build program corresponding to “Unix Makefiles”. CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
– Configuring incomplete, errors occurred!
Traceback (most recent call last):
File “/home/frank/opencv/platforms/js/build_js.py”, line 291, in
builder.config()
File “/home/frank/opencv/platforms/js/build_js.py”, line 202, in config
execute(cmd)
File “/home/frank/opencv/platforms/js/build_js.py”, line 23, in execute
raise Fail(“Child returned: %s” % retcode)
main.Fail: Child returned: 1
emcmake: error: ‘python3 opencv/platforms/js/build_js.py build_asm --disable_wasm --emscripten_dir=/mnt/c/users/magma/emsdk/upstream/emscripten --cmake_option=-DCMAKE_TOOLCHAIN_FILE=/mnt/c/Users/magma/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake --cmake_option=-DCMAKE_CROSSCOMPILING_EMULATOR=/mnt/c/Users/magma/emsdk/node/16.20.0_64bit/bin/node’ failed (returned 1)

Hey,

The command I ran was using paths that correlate to the files on my computer. You’ll have to substitute the paths I use for ones that are on your machine. For example, I set empscripten_dir equal to “/mnt/c/users/magma/emsdk/upstream/emscripten” because I have a User named magma on my machine, and that’s the location where I saved the emsdk directory. You’ll have to sub it out for wherever your emsdk file is located. Same for essentially every path in that command.

Stupid me!
Thank you so much