Skip to content

Commit

Permalink
Evision.__enabled_modules__/0 => Evision.enabled_modules/0
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-xu committed Jan 12, 2023
1 parent 9dbfde2 commit 086ca23
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Changed
- [c_src] check if we can use existing atom from `enif_make_existing_atom` before calling to `enif_make_atom`.
- [Evision] `Evision.__enabled_modules__/0` => `Evision.enabled_modules/0`. Result will now be computed using `HAVE_OPENCV_{MODULE_NAME}` macros.

## v0.1.25 (2022-12-18)
[Browse the Repository](https://github.com/cocoa-xu/evision/tree/v0.1.25) | [Released Assets](https://github.com/cocoa-xu/evision/releases/tag/v0.1.25)
Expand Down
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ endif()

if(WIN32)
execute_process(COMMAND "rmdir ${GENERATED_ELIXIR_SRC_DIR} /s /q && rmdir ${GENERATED_ERLANG_SRC_DIR} /s /q && mkdir ${GENERATED_ELIXIR_SRC_DIR} && mkdir ${GENERATED_ERLANG_SRC_DIR}")
message(STATUS "Enabled modules: ${ENABLED_CV_MODULES}")
execute_process(COMMAND python3.exe "${PY_SRC}\\gen2.py" "${C_SRC}" "${GENERATED_ELIXIR_SRC_DIR}" "${GENERATED_ERLANG_SRC_DIR}" "${C_SRC}\\${C_SRC_HEADERS_TXT}" "${EVISION_GENERATE_LANG}" "${ENABLED_CV_MODULES}" RESULT_VARIABLE STATUS)
else()
execute_process(COMMAND bash -c "rm -rf ${GENERATED_ELIXIR_SRC_DIR} && rm -rf ${GENERATED_ERLANG_SRC_DIR} && mkdir -p ${GENERATED_ELIXIR_SRC_DIR} && mkdir -p ${GENERATED_ERLANG_SRC_DIR}")
message(STATUS "Enabled modules: ${ENABLED_CV_MODULES}")
execute_process(COMMAND bash -c "python3 ${PY_SRC}/gen2.py ${C_SRC} ${GENERATED_ELIXIR_SRC_DIR} ${GENERATED_ERLANG_SRC_DIR} ${C_SRC}/${C_SRC_HEADERS_TXT} ${EVISION_GENERATE_LANG} ${ENABLED_CV_MODULES}" RESULT_VARIABLE STATUS)
endif()
if(STATUS STREQUAL "0")
Expand Down
3 changes: 3 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ defmodule Evision.MixProject do
# opencv/opencv_contrib
opencv: [
# module name: is_enabled
# note that these true values here only mean that we requested
# these module to be compiled
# some of them can be disabled due to dependency issues
calib3d: true,
core: true,
dnn: true,
Expand Down
8 changes: 4 additions & 4 deletions py_src/evision_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,20 +307,20 @@ def set_${property_name}(self, prop) do
'__to_struct__'(Ret).
""")

# template for `Evision.__enabled_modules__/0`
# template for `Evision.enabled_modules/0`
enabled_modules_code = Template("""
@doc \"\"\"
return a list of enabled modules in this build
\"\"\"
def __enabled_modules__ do
[${enabled_modules}]
def enabled_modules do
:evision_nif.enabled_modules()
end
""")

# template for `evision:enabled_modules/0`
enabled_modules_code_erlang = Template("""
enabled_modules() ->
[${enabled_modules}].
evision_nif:enabled_modules().
""")

gen_template_check_self = Template("""
Expand Down
114 changes: 114 additions & 0 deletions py_src/gen2.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,119 @@ def get_module_writer(self, module_name, wname, name, is_ns):
self.evision_modules[evision_module_filename] = module_file_generator
return self.evision_modules[evision_module_filename], inner_ns

def gen_enabled_modules(self):
all_modules = {
# opencv/opencv_contrib
'opencv': [
'calib3d',
'core',
'dnn',
'features2d',
'flann',
'highgui',
'imgcodecs',
'imgproc',
'ml',
'photo',
'stitching',
'ts',
'video',
'videoio',

'gapi',
'world',
'python2',
'python3',
'java',
],

'opencv_contrib': [
'aruco',
'barcode',
'bgsegm',
'bioinspired',
'dnn_superres',
'face',
'hfs',
'img_hash',
'line_descriptor',
'mcc',
'plot',
'quality',
'rapid',
'reg',
'rgbd',
'saliency',
'shape',
'stereo',
'structured_light',
'surface_matching',
'text',
'tracking',
'wechat_qrcode',
'xfeatures2d',
'ximgproc',
'xphoto',

# no bindings yet
'datasets',
'dnn_objdetect',
'dpm',
'optflow',
'sfm',
'videostab',
'xobjdetect',
],

'cuda': [
'cudaarithm',
'cudabgsegm',
'cudacodec',
'cudafeatures2d',
'cudafilters',
'cudaimgproc',
'cudalegacy',
'cudaobjdetect',
'cudaoptflow',
'cudastereo',
'cudawarping',
'cudev',
]
}

num_total_modules = sum([len(compoents) for s, compoents in all_modules.items()])
self.code_funcs.write("static ERL_NIF_TERM evision_cv_enabled_modules(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]){\n")
self.code_funcs.write(f""" const size_t num_total_modules = {num_total_modules};
size_t index = 0;
ERL_NIF_TERM keys[num_total_modules];
ERL_NIF_TERM values[num_total_modules];
""")

for _, compoents in all_modules.items():
for compoent in compoents:
self.code_funcs.write(f""" keys[index] = evision::nif::atom(env, "{compoent}");
#ifdef HAVE_OPENCV_{compoent.upper()}
values[index] = evision::nif::atom(env, "true");
#else
values[index] = evision::nif::atom(env, "false");
#endif
index++;
""")

self.code_funcs.write(""" ERL_NIF_TERM map;
if (enif_make_map_from_arrays(env, keys, values, index, &map)) {
return map;
} else {
return evision::nif::error(env, "enif_make_map_from_arrays failed in evision_from_as_map");
}
""")

self.code_funcs.write("}\n")
self.code_ns_reg.write(f' F(enabled_modules, evision_cv_enabled_modules, 0),\n')
self.evision_nif.write(f' def enabled_modules, do: :erlang.nif_error("enabled_modules not loaded")\n')
self.evision_nif_erlang.write(f'enabled_modules() ->\n not_loaded(?LINE).\n')

def gen(self, srcfiles, output_path, erl_output_path, erlang_output_path):
self.output_path = output_path
self.clear()
Expand All @@ -411,6 +524,7 @@ def gen(self, srcfiles, output_path, erl_output_path, erlang_output_path):
self.evision_nif_erlang.write('-module(evision_nif).\n-compile(nowarn_export_all).\n-compile([export_all]).\n\n{}\n{}\n'.format(ET.gen_evision_nif_load_nif_erlang, ET.gen_cv_types_erlang))

self.code_ns_reg.write('static ErlNifFunc nif_functions[] = {\n')
self.gen_enabled_modules()

# step 1: scan the headers and build more descriptive maps of classes, consts, functions
for hdr in srcfiles:
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Evision.TestHelper do
end
end

compiled_modules = Evision.__enabled_modules__()
compiled_modules = Evision.enabled_modules()

ExUnit.configure(
exclude: [
Expand Down

0 comments on commit 086ca23

Please sign in to comment.