Skip to content

Commit

Permalink
Import code for optical flow in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
squishyhuman committed Oct 4, 2023
1 parent 48f0425 commit f296b53
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ jobs:
- name: Build depends
if: steps.restore-depends.outputs.cache-hit != 'true'
run: tools/build-depends.sh all

- name: Build libraries
run: lib/build-ci.sh
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Generated dependencies
/src/generated

# Generated libraries
/frontend/public/motion_tracker

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -143,3 +146,8 @@ Temporary

# libzip build files
/third-party/libzip

# Eclipse project files
.cproject
.project
.settings
174 changes: 174 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
################################################################################
#
# Copyright (C) 2020-2023 retro.ai
# This file is part of retro-dapp - https://github.com/RetroAI/retro-dapp
#
# SPDX-License-Identifier: Apache-2.0
# See the file LICENSE.md for more information.
#
################################################################################

################################################################################
#
# Build system for C++ libraries
#
# Required CMake variables:
#
# CMAKE_FIND_ROOT_PATH - Point this to dependencies compiled with Emscripten
# CMAKE_INSTALL_PREFIX - Point this to the "public" folder
#
################################################################################

################################################################################
#
# Project settings
#
################################################################################

project(retroai)

cmake_minimum_required(VERSION 3.0.0)

set(CMAKE_CXX_STANDARD 17)

################################################################################
#
# Dependencies
#
################################################################################

find_package(Ceres REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Glog REQUIRED)
find_package(OpenCV REQUIRED)

add_definitions(-DCERES_FOUND=1)

################################################################################
#
# Define sources
#
################################################################################

#
# Motion tracker
#

set(MOTION_TRACKER_SOURCES
api/imgproc.cpp
api/reconstruction.cpp
api/scene.cpp
api/video.cpp
kernels/cpu/cpu_imgproc.cpp
kernels/cpu/cpu_reconstruction.cpp
kernels/cpu/cpu_scene.cpp
kernels/cpu/cpu_video.cpp
motion_tracker/motion_tracker.cpp
motion_tracker/motion_tracker_embinder.cpp
motion_tracker/vision_graph.cpp
utils/emscripten_utils.cpp
utils/frame_pool.cpp
utils/image_utils.cpp
utils/math_utils.cpp
)

################################################################################
#
# Build libraries
#
# TODO:
#
# * Build properly instead of shelling out
# * Could refactor this into macros
#
################################################################################

include_directories(
${CMAKE_SOURCE_DIR}
)

string(APPEND EMSCRIPTEN_LINK_FLAGS
"--bind "
# "-o dist/engine.js "
# " -std=c++11 "
# " -O2 "
# " --preload-file textures "
# " --preload-file shaders "
# " --preload-file fonts "
# " --pre-js pre-module.j "
# " --post-js post-module.j "
"-s ALLOW_MEMORY_GROWTH=1 "
"-s ASSERTIONS=1 "
# " -s DEMANGLE_SUPPORT=1 "
# " -s DISABLE_EXCEPTION_CATCHING=0 "
"-s ERROR_ON_UNDEFINED_SYMBOLS=0 "
# " -s FULL_ES3=1 "
# " -s GL_ASSERTIONS=1 "
# " -s GL_UNSAFE_OPTS=0 "
# " -s INVOKE_RUN=0 "
# " -s LEGACY_GL_EMULATION=0 "
#"-s LLD_REPORT_UNDEFINED "
# " -s OFFSCREENCANVAS_SUPPORT=1 "
# " -s SAFE_HEAP=1 "
#"-s TOTAL_MEMORY=67108864 "
# " -s USE_FREETYPE=1 "
# " -s USE_GLFW=3 "
# " -s USE_WEBGL2=1 "
"-s USE_ZLIB=1 "
# " -s WASM=1 "
)

#
# Motion tracker
#

add_executable(motion_tracker
${MOTION_TRACKER_SOURCES}
)

target_include_directories(motion_tracker PRIVATE
${OpenCV_INCLUDE_DIRS}
)

target_link_libraries(motion_tracker PRIVATE
${OpenCV_LIBS}
)

if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
set_target_properties(motion_tracker PROPERTIES
COMPILE_FLAGS " \
-O0 \
-g4 \
-s DISABLE_EXCEPTION_CATCHING=0 \
-s INITIAL_MEMORY=26214400 \
"
# 26214400 is 25 MiB
LINK_FLAGS " \
--bind \
--source-map-base https://retro.ai/ \
-O0 \
-g4 \
-s DISABLE_EXCEPTION_CATCHING=0 \
-s INITIAL_MEMORY=26214400 \
"
)
endif ()

################################################################################
#
# Install libraries
#
################################################################################

#
# Motion tracker
#

INSTALL(
FILES
"${CMAKE_BINARY_DIR}/motion_tracker.js"
"${CMAKE_BINARY_DIR}/motion_tracker.wasm"
"${CMAKE_BINARY_DIR}/motion_tracker.wasm.map"
DESTINATION
motion_tracker
)
67 changes: 67 additions & 0 deletions lib/build-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash
################################################################################
#
# Copyright (C) 2000-2023 retro.ai
# This file is part of retro-dapp - https://github.com/RetroAI/retro-dapp
#
# SPDX-License-Identifier: Apache-2.0
# See the file LICENSE.md for more information.
#
################################################################################

################################################################################
#
# Helper for CI infrastructure. Sets the appropriate paths and calls CMake.
#
################################################################################

# Enable strict shell mode
set -o errexit
set -o nounset
set -o pipefail

#
# Environment paths
#

# Get the absolute path to this script
SOURCE_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Directory of the depends build system
TOOL_DIRECTORY="${SOURCE_DIRECTORY}/../tools"

# Directory for intermediate build files
BUILD_DIRECTORY="${TOOL_DIRECTORY}/build/cpp-libs"

# Directory of the Emscripten SDK
EMSDK_DIRECTORY="${TOOL_DIRECTORY}/repos/emsdk"

# Directory of the installed dependency files
DEPENDS_DIRECTORY="${TOOL_DIRECTORY}/dist"

# Directory to place the generated libraries
INSTALL_DIRECTORY="${SOURCE_DIRECTORY}/../frontend/public"

# Ensure directories exist
mkdir -p "${BUILD_DIRECTORY}"
mkdir -p "${INSTALL_DIRECTORY}"

#
# Setup environment
#

source "${EMSDK_DIRECTORY}/emsdk_set_env.sh"

#
# Call CMake
#

cd "${BUILD_DIRECTORY}"

emcmake cmake \
"${SOURCE_DIRECTORY}" \
-DCMAKE_FIND_ROOT_PATH="${DEPENDS_DIRECTORY}" \
-DCMAKE_INSTALL_PREFIX="${INSTALL_DIRECTORY}" \
$(! command -v ccache &> /dev/null || echo "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache") \

cmake --build "${BUILD_DIRECTORY}" -j$(shell getconf _NPROCESSORS_ONLN) --target install

0 comments on commit f296b53

Please sign in to comment.