From 635c5ea5529a2939f5c6af8a8d870c6071de9c22 Mon Sep 17 00:00:00 2001 From: Dmitry Yakovlev Date: Thu, 15 Sep 2022 16:53:40 +0000 Subject: [PATCH 1/2] OpenCV component for IDF. --- .github/workflows/upload_component.yml | 2 +- .gitmodules | 6 + opencv/CMakeLists.txt | 170 +++++++++++++++++++++ opencv/LICENSE | 202 +++++++++++++++++++++++++ opencv/README.md | 7 + opencv/dummy.c | 5 + opencv/idf_component.yml | 9 ++ opencv/opencv | 1 + opencv/opencv_contrib | 1 + test_app/CMakeLists.txt | 14 ++ 10 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 opencv/CMakeLists.txt create mode 100644 opencv/LICENSE create mode 100644 opencv/README.md create mode 100644 opencv/dummy.c create mode 100644 opencv/idf_component.yml create mode 160000 opencv/opencv create mode 160000 opencv/opencv_contrib diff --git a/.github/workflows/upload_component.yml b/.github/workflows/upload_component.yml index 94cc15d086..bbc35e81e3 100644 --- a/.github/workflows/upload_component.yml +++ b/.github/workflows/upload_component.yml @@ -15,6 +15,6 @@ jobs: - name: Upload components to component service uses: espressif/upload-components-ci-action@v1 with: - directories: "bdc_motor;cbor;jsmn;led_strip;libsodium;pid_ctrl;qrcode;nghttp;sh2lib;expat;esp_encrypted_img;coap;pcap;json_generator;json_parser;usb/usb_host_cdc_acm;usb/usb_host_msc;usb/usb_host_uvc;esp_serial_slave_link;esp_jpeg" + directories: "bdc_motor;cbor;jsmn;led_strip;libsodium;pid_ctrl;qrcode;nghttp;sh2lib;expat;esp_encrypted_img;coap;pcap;json_generator;json_parser;usb/usb_host_cdc_acm;usb/usb_host_msc;usb/usb_host_uvc;esp_serial_slave_link;esp_jpeg;opencv" namespace: "espressif" api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }} diff --git a/.gitmodules b/.gitmodules index 8572eac7f2..bfab07132f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,9 @@ [submodule "usb/usb_host_uvc/libuvc"] path = usb/usb_host_uvc/libuvc url = https://github.com/libuvc/libuvc.git +[submodule "opencv/opencv"] + path = opencv/opencv + url = https://github.com/opencv/opencv.git +[submodule "opencv/opencv_contrib"] + path = opencv/opencv_contrib + url = https://github.com/opencv/opencv_contrib.git diff --git a/opencv/CMakeLists.txt b/opencv/CMakeLists.txt new file mode 100644 index 0000000000..bc98cae602 --- /dev/null +++ b/opencv/CMakeLists.txt @@ -0,0 +1,170 @@ +idf_component_register( + # We need the dummy source file so that the component + # library is not an interface library. This allows to + # get the list of include directories from other components + # via INCLUDE_DIRECTORIES property later on. + SRCS dummy.c + # OpenCV librarires depend on some of the IDF libraries, + # add them here: + PRIV_REQUIRES vfs lwip pthread newlib) + +# Determine compilation flags used for building OpenCV +# Flags inherited from IDF build system and other IDF components: +set(idf_include_directories $) +set(includes "-I$") +# IDF is built with -D_GNU_SOURCE, but we don't actually implement all _GNU_SOURCE +# features. Defining _GNU_SOURCE causes OpenCV to try to use them, which fails. +# Use _DEFAULT_SOURCE instead, it provides enough features to build OpenCV. +set(extra_defines -D_DEFAULT_SOURCE) +# Final flags for C and C++: +set(c_flags "${includes} ${extra_defines}") +set(cxx_flags "${includes} ${extra_defines}") + +set(common_flags "-ggdb -ffunction-sections -fdata-sections") + +# Calculate flags for optimizations, assertions, debug info: +if(COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE) + set(assert_flags "-DNDEBUG") +else() + set(assert_flags "-DDEBUG -D_DEBUG") +endif() + +if(CONFIG_IDF_TARGET_ARCH_XTENSA) + set(assert_flags "${assert_flags} -mlongcalls") +endif() + +if(CONFIG_COMPILER_OPTIMIZATION_DEFAULT) + set(opt_flags "-Og ${common_flags} ${assert_flags}") + set(opt_args -DCMAKE_BUILD_TYPE=Debug + -DCMAKE_C_FLAGS_DEBUG=${opt_flags} + -DCMAKE_CXX_FLAGS_DEBUG=${opt_flags}) +elseif(CONFIG_COMPILER_OPTIMIZATION_SIZE) + # Normally we should use MinSizeRel build type for this, + # however OpenCV CMake files only handle Debug and Release + # (see e.g. opencv/cmake/OpenCVCompilerOptions.cmake). + # So we redefine the flags for Release instead. + set(opt_flags "-Os ${common_flags} ${assert_flags}") + set(opt_args -DCMAKE_BUILD_TYPE=Release + -DCMAKE_C_FLAGS_RELEASE=${opt_flags} + -DCMAKE_CXX_FLAGS_RELEASE=${opt_flags}) +elseif(COMPILER_OPTIMIZATION_PERF) + # Currently OpenCV fails to compile at -O2 level on Xtensa due to a compiler issue, + # details in https://github.com/jcmvbkbc/gcc-xtensa/issues/14. + # Compiling at -O3 happens to be okay. + set(opt_flags "-O3 ${common_flags} ${assert_flags}") + set(opt_args -DCMAKE_BUILD_TYPE=Release + -DCMAKE_C_FLAGS_RELEASE=${opt_flags} + -DCMAKE_CXX_FLAGS_RELEASE=${opt_flags}) +elseif(COMPILER_OPTIMIZATION_NONE) + set(opt_flags "-O0 ${common_flags} ${assert_flags}") + set(opt_args -DCMAKE_BUILD_TYPE=Debug + -DCMAKE_C_FLAGS_DEBUG=${opt_flags} + -DCMAKE_CXX_FLAGS_DEBUG=${opt_flags}) +else() + message(FATAL_ERROR "Unsupported optimization level") +endif() + +# Python to use for OpenCV build +idf_build_get_property(python PYTHON) + +include(ExternalProject) + +# Build OpenCV in this directory: +set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/opencv-build) + +# List of OpenCV static libraries we expect to see after the build. +# Obtained manually by looking at ${BINARY_DIR}/install/lib directory. +# In some cases it's important to have right order of libraries here. +set(opencv_libraries + calib3d flann fuzzy + objdetect tracking video videostab features2d + surface_matching + shape stereo stitching structured_light + line_descriptor ml phase_unwrapping photo plot quality rapid + imgcodecs imgproc core +) + +set(3rdparty_libraries + zlib + libjasper + libjpeg-turbo + libpng + libtiff + libwebp +) + +# List of all static libraries to be produced +set(all_libraries) +foreach(libname ${opencv_libraries}) + set(lib_path ${BINARY_DIR}/install/lib/libopencv_${libname}.a) + list(APPEND all_libraries ${lib_path}) +endforeach() +foreach(libname ${3rdparty_libraries}) + set(lib_path ${BINARY_DIR}/install/lib/opencv4/3rdparty/lib${libname}.a) + list(APPEND all_libraries ${lib_path}) +endforeach() + +# Add OpenCV as a subproject. +ExternalProject_Add(opencv_proj + SOURCE_DIR ${COMPONENT_DIR}/opencv + BINARY_DIR ${BINARY_DIR} + BUILD_BYPRODUCTS ${all_libraries} + # These two options are set so that Ninja immediately outputs + # the subproject build to the terminal. Otherwise it looks like the + # build process "hangs" for too long until OpenCV build is complete. + USES_TERMINAL_CONFIGURE TRUE + USES_TERMINAL_BUILD TRUE + # Arguments to pass to OpenCV CMake invocation: + CMAKE_ARGS + -DCMAKE_C_FLAGS=${c_flags} + -DCMAKE_CXX_FLAGS=${cxx_flags} + ${opt_args} + -DCMAKE_INSTALL_PREFIX=${BINARY_DIR}/install + -DENABLE_CCACHE=${CCACHE_ENABLE} + -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} + -DPYTHON_DEFAULT_EXECUTABLE=${python} + -DCV_CPU_BASELINE_MODE=${CONFIG_IDF_TARGET} + -DCV_DISABLE_OPTIMIZATION=0 + -DOPENCV_EXTRA_MODULES_PATH=${CMAKE_CURRENT_LIST_DIR}/opencv_contrib/modules + -DOPENCV_FORCE_3RDPARTY_BUILD=1 + -DCMAKE_SYSTEM_PROCESSOR=${CONFIG_IDF_TARGET_ARCH} + -DENABLE_PIC=0 + -DTARGET=${CONFIG_IDF_TARGET} + -DWITH_ADE=0 + -DWITH_JPEG=1 + -DWITH_OPENCL=0 + -DWITH_OPENEXR=0 + -DWITH_OPENGL=0 + -DWITH_OPENMP=0 + -DWITH_PNG=1 + -DWITH_PROTOBUF=0 + -DWITH_TIFF=1 + -DWITH_WEBP=1 + -DBUILD_EXAMPLES=0 + -DBUILD_PACKAGE=1 + -DBUILD_PERF_TESTS=0 + -DBUILD_PNG=1 + -DBUILD_SHARED_LIBS=0 + -DBUILD_TESTS=0 + -DBUILD_ZLIB=1 + -DBUILD_opencv_apps=0 + -DBUILD_opencv_bgsegm=0 + -DBUILD_opencv_datasets=0 + -DBUILD_opencv_imgcodecs=1 + -DBUILD_opencv_objdetect=1 + -DBUILD_opencv_imgproc=1 + -DBUILD_opencv_tracking=1 + -DBUILD_opencv_video=1 + -DBUILD_opencv_videoio=0 +) + +add_dependencies(${COMPONENT_LIB} opencv_proj) + +# Attach generated libraries and header files to an interface library: +add_library(opencv_interface_lib INTERFACE) +set_target_properties(opencv_interface_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${BINARY_DIR}/install/include/opencv4) +set_target_properties(opencv_interface_lib PROPERTIES INTERFACE_LINK_LIBRARIES "${all_libraries}") +add_dependencies(opencv_interface_lib opencv_proj) + +# Finally, link the interface library to the component library: +target_link_libraries(${COMPONENT_LIB} INTERFACE opencv_interface_lib) diff --git a/opencv/LICENSE b/opencv/LICENSE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/opencv/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/opencv/README.md b/opencv/README.md new file mode 100644 index 0000000000..b2b9354f7e --- /dev/null +++ b/opencv/README.md @@ -0,0 +1,7 @@ +# OpenCV library build for esp-idf +This project used to build and include OpenCV library into the esp-idf as a component for esp32, esp32s2 and esp32s3 CPUs. +Now the opencv could be build with IDF version 4.4. +This component was tested with opencv V 4.6.0 +The build is working only on Linux machines. + +More details about OpenCV can be found [here](https://opencv.org/). diff --git a/opencv/dummy.c b/opencv/dummy.c new file mode 100644 index 0000000000..d8b6de4fa7 --- /dev/null +++ b/opencv/dummy.c @@ -0,0 +1,5 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ \ No newline at end of file diff --git a/opencv/idf_component.yml b/opencv/idf_component.yml new file mode 100644 index 0000000000..65ef999382 --- /dev/null +++ b/opencv/idf_component.yml @@ -0,0 +1,9 @@ +version: "4.6.0~1" +description: OpenCV build for esp-idf +url: https://github.com/espressif/idf-extra-components/tree/master/opencv +dependencies: + idf: "==4.4" +targets: + - esp32 + - esp32s2 + - esp32s3 diff --git a/opencv/opencv b/opencv/opencv new file mode 160000 index 0000000000..b0dc474160 --- /dev/null +++ b/opencv/opencv @@ -0,0 +1 @@ +Subproject commit b0dc474160e389b9c9045da5db49d03ae17c6a6b diff --git a/opencv/opencv_contrib b/opencv/opencv_contrib new file mode 160000 index 0000000000..db16caf6ce --- /dev/null +++ b/opencv/opencv_contrib @@ -0,0 +1 @@ +Subproject commit db16caf6ceee76b43b94c846be276e92a43e9700 diff --git a/test_app/CMakeLists.txt b/test_app/CMakeLists.txt index f4588cf30c..e28032913e 100644 --- a/test_app/CMakeLists.txt +++ b/test_app/CMakeLists.txt @@ -12,6 +12,20 @@ if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "4.4") list(APPEND EXTRA_COMPONENT_DIRS ../pid_ctrl ../esp_encrypted_img ../pcap ../esp_jpeg) endif() +# 2.1. Add here if the component is compatible with IDF == v4.3 +if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_EQUAL "4.3") + if(NOT ((${IDF_TARGET} MATCHES "esp32c3") OR (${IDF_TARGET} MATCHES "esp32c2"))) + list(APPEND EXTRA_COMPONENT_DIRS ../opencv) + endif() +endif() + +# 2.2. Add here if the component is compatible with IDF == v4.4 +if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_EQUAL "4.4") + if(NOT ((${IDF_TARGET} MATCHES "esp32c3") OR (${IDF_TARGET} MATCHES "esp32c2"))) + list(APPEND EXTRA_COMPONENT_DIRS ../opencv) + endif() +endif() + # 3. Add here if the component is compatible with IDF >= v5.0 if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.0") list(APPEND EXTRA_COMPONENT_DIRS ../bdc_motor ../led_strip ../sh2lib ../nghttp ../esp_serial_slave_link) From 568a7b55d168faa8dde896fea1651405c0eba970 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 24 Sep 2024 11:44:54 +0300 Subject: [PATCH 2/2] Add esp32p4 to the list of component targets --- opencv/idf_component.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/opencv/idf_component.yml b/opencv/idf_component.yml index 65ef999382..6202ea2414 100644 --- a/opencv/idf_component.yml +++ b/opencv/idf_component.yml @@ -7,3 +7,4 @@ targets: - esp32 - esp32s2 - esp32s3 + - esp32p4