Skip to content

core: add samples::findFile() function - #12354

Merged
alalek merged 5 commits into
opencv:3.4from
alalek:samples_find_file
Nov 16, 2018
Merged

alalek merged 5 commits into
opencv:3.4from
alalek:samples_find_file

Conversation

@alalek

@alalek alalek commented Aug 30, 2018 •

Copy link
Copy Markdown
Member

TODO:

  • update description
  • Winpack support: cmake ... "-DOPENCV_INSTALL_DATA_DIR_RELATIVE=../../../../sources"
tests_filter=*findFile*

@mshabunin mshabunin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this scheme is too complex. What about hacking into CommandLineParser? It knows argv[0] and can build absolute path to the application being run. Then it can probe two locations:

  • relative path <build>/bin -> <src>/samples/data
  • relative path <install>/<samples> -> <install>/<data>
    And an environment variable which will override both.

Comment thread modules/core/include/opencv2/core.hpp Outdated
@defgroup core_utils_sse SSE utilities
@defgroup core_utils_neon NEON utilities
@defgroup core_utils_softfloat Softfloat support
@defgroup samples_utils Utility functions for OpenCV samples

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core_utils_samples?

@alalek

alalek commented Sep 12, 2018

Copy link
Copy Markdown
Member Author
  1. Ideally we should package samples in the source code form without binaries. Users want to "play" with samples (modify the source code and run their experiments).
    On Linux samples are build outside of installation folder. Installation folder is usually owned by root (/usr/local or /opt). So relative paths would not work.

  2. This "complex" scheme is not for searching data for samples only. User apps can use some data from OpenCV: for example, HAAR cascades. But function entry point should be be renamed (current patch is about cv:::samples::findFile()).

  3. argv[0] - it is not a reliable way. argv[0] is useless if binary's path is in PATH env variable.

@alalek

alalek commented Sep 13, 2018

Copy link
Copy Markdown
Member Author

hacking into CommandLineParser

Not sure that this is a reliable way for this:

  • .get<string> is used widely, not only for files: for stitching modes, for camera IDs, GStreamer params, etc.

Adding new string-compatible .get<cv::DataPath>() requires similar changes in samples compared to adding samples::findFile() calls.

@alalek

alalek commented Sep 14, 2018

Copy link
Copy Markdown
Member Author

Patch is updated:

  • added generic cv::utils::findDataFile()
  • cv::samples::findFile() is a wrapper for the function above.

BTW, CommandLineParser is C++ only. It is not used in Python/Java samples.

@alalek

alalek commented Sep 15, 2018

Copy link
Copy Markdown
Member Author

@berak @LaurentBerger @sturkmen72 Any thoughts / suggestions about this patch?

@LaurentBerger

LaurentBerger commented Sep 17, 2018 •

Copy link
Copy Markdown
Contributor

I clone your branch
git clone -b samples_find_file --single-branch https://github.com/alalek/opencv.git
and compile suiing VS2017 win 64 and cmake 3.11.0

I inserted in a sample :

    String relative_path;
    vector<string> s = { "baboon.jpg","basketball1.png","1.png" };
    for (auto n : s)
    {
        try 
        {
            string fName1 = samples::findFile(n, false);
            cout << n<<" findFile(,false)-> " << fName1 << "\n";
            if (!fName1.empty())
            {
                Mat img = imread(fName1);
                imshow(fName1, img);
            }

        }
        catch (cv::Exception &e)
        {
            cout << "EXCEPTION -> "<< e.msg << "\n";
        }
        try
        {
            string fName1 = samples::findFile(n, true);
            cout << n << " findFile(,true)-> " << fName1 << "\n";
            if (!fName1.empty())
            {
                Mat img = imread(fName1);
                imshow(fName1, img);
            }
        }
        catch (cv::Exception &e)
        {
            cout << "EXCEPTION -> " << e.msg << "\n";
        }
    }
    waitKey(0);

and run it inside vS2017 :

baboon.jpg findFile(,false)-> F:/lib/tmp/build\../opencv/samples/data\baboon.jpg
baboon.jpg findFile(,true)-> F:/lib/tmp/build\../opencv/samples/data\baboon.jpg
basketball1.png findFile(,false)-> F:/lib/tmp/build\../opencv/samples/data\basketball1.png
basketball1.png findFile(,true)-> F:/lib/tmp/build\../opencv/samples/data\basketball1.png
1.png findFile(,false)->
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: 1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: 1.png in function 'cv::samples::findFile'


Results is OK

Now I install opencv using CMakeTargets project and I open windows build/install/samples/cpp and click executable file (or in build/bin/debug) results :

baboon.jpg findFile(,false)-> F:/lib/tmp/build\../opencv/samples/data\baboon.jpg
baboon.jpg findFile(,true)-> F:/lib/tmp/build\../opencv/samples/data\baboon.jpg
basketball1.png findFile(,false)-> F:/lib/tmp/build\../opencv/samples/data\basketball1.png
basketball1.png findFile(,true)-> F:/lib/tmp/build\../opencv/samples/data\basketball1.png
1.png findFile(,false)->
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: 1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: 1.png in function 'cv::samples::findFile'

Now in cmake-gui I changed cmake install prefix (in f:/tmp/opencv ) results are

baboon.jpg findFile(,false)->
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: baboon.jpg) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: baboon.jpg in function 'cv::samples::findFile'

basketball1.png findFile(,false)->
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: basketball1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: basketball1.png in function 'cv::samples::findFile'

1.png findFile(,false)->
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: 1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: 1.png in function 'cv::samples::findFile'

Now I think for an user who click in cmake and build opencv and may be installed without any change It's
a good PR : he can run an example with default data.

Last case (a specific path to install opencv) I think that an user who changes path is not a noobs so may be it's not a problem

@alalek

alalek commented Sep 17, 2018

Copy link
Copy Markdown
Member Author

@LaurentBerger Thank you for looking on this!

  1. For debugging you can use this:
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_VERBOSE);
  1. Install directory: did you use cmake -DINSTALL_C_EXAMPLES=ON ...? (by default samples data is not installed)

@LaurentBerger

LaurentBerger commented Sep 17, 2018 •

Copy link
Copy Markdown
Contributor

Inside VS2017

[DEBUG:0] cv::samples::findFile('baboon.jpg', false)
[DEBUG:0] utils::findDataFile('baboon.jpg', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'baboon.jpg'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): the current directory is build sub-directory: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): <build>/../opencv/
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/baboon.jpg'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/data\baboon.jpg'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/samples/data\baboon.jpg'
baboon.jpg findFile(,false)-> F:/lib/tmp/build\../opencv/samples/data\baboon.jpg
[DEBUG:0] cv::samples::findFile('baboon.jpg', true)
[DEBUG:0] utils::findDataFile('baboon.jpg', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'baboon.jpg'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): the current directory is build sub-directory: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): <build>/../opencv/
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/baboon.jpg'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/data\baboon.jpg'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/samples/data\baboon.jpg'
baboon.jpg findFile(,true)-> F:/lib/tmp/build\../opencv/samples/data\baboon.jpg
[DEBUG:0] cv::samples::findFile('basketball1.png', false)
[DEBUG:0] utils::findDataFile('basketball1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'basketball1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): the current directory is build sub-directory: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): <build>/../opencv/
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/basketball1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/data\basketball1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/samples/data\basketball1.png'
basketball1.png findFile(,false)-> F:/lib/tmp/build\../opencv/samples/data\basketball1.png
[DEBUG:0] cv::samples::findFile('basketball1.png', true)
[DEBUG:0] utils::findDataFile('basketball1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'basketball1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): the current directory is build sub-directory: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): <build>/../opencv/
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/basketball1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/data\basketball1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/samples/data\basketball1.png'
basketball1.png findFile(,true)-> F:/lib/tmp/build\../opencv/samples/data\basketball1.png
[DEBUG:0] cv::samples::findFile('1.png', false)
[DEBUG:0] utils::findDataFile('1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open '1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): the current directory is build sub-directory: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): <build>/../opencv/
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/data\1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/samples/data\1.png'
[DEBUG:0] Detected module path: 'F:\lib\tmp\build\bin\Debug\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/tmp/opencv  path: F:\lib\tmp\build\bin\Debug\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\tmp\opencv  path: F:\lib\tmp\build\bin\Debug\opencv_core343d.dll
1.png findFile(,false)->
[DEBUG:0] cv::samples::findFile('1.png', true)
[DEBUG:0] utils::findDataFile('1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open '1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): the current directory is build sub-directory: F:\lib\tmp\build\samples\cpp
[DEBUG:0] utils::findDataFile(): <build>/../opencv/
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/data\1.png'
[DEBUG:0] ... Line 207: trying open 'F:/lib/tmp/build\../opencv/samples/data\1.png'
[DEBUG:0] Detected module path: 'F:\lib\tmp\build\bin\Debug\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/tmp/opencv  path: F:\lib\tmp\build\bin\Debug\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\tmp\opencv  path: F:\lib\tmp\build\bin\Debug\opencv_core343d.dll
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: 1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: 1.png in function 'cv::samples::findFile'

and in window f:/tmp/opencv/samples/cpp

[DEBUG:0] cv::samples::findFile('baboon.jpg', false)
[DEBUG:0] utils::findDataFile('baboon.jpg', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'baboon.jpg'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp
[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build/install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build\install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
baboon.jpg findFile(,false)->
[DEBUG:0] cv::samples::findFile('baboon.jpg', true)
[DEBUG:0] utils::findDataFile('baboon.jpg', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'baboon.jpg'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp
[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build/install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build\install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: baboon.jpg) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: baboon.jpg in function 'cv::samples::findFile'

[DEBUG:0] cv::samples::findFile('basketball1.png', false)
[DEBUG:0] utils::findDataFile('basketball1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'basketball1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp
[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build/install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build\install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
basketball1.png findFile(,false)->
[DEBUG:0] cv::samples::findFile('basketball1.png', true)
[DEBUG:0] utils::findDataFile('basketball1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open 'basketball1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp
[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build/install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build\install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: basketball1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: basketball1.png in function 'cv::samples::findFile'

[DEBUG:0] cv::samples::findFile('1.png', false)
[DEBUG:0] utils::findDataFile('1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open '1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp
[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build/install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build\install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
1.png findFile(,false)->
[DEBUG:0] cv::samples::findFile('1.png', true)
[DEBUG:0] utils::findDataFile('1.png', OPENCV_SAMPLES_DATA_PATH)
[DEBUG:0] ... Line 153: trying open '1.png'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp
[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:/lib/tmp/build/install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
[VERB0:0] isSubDirectory(): base: F:\lib\tmp\build\install  path: F:\tmp\opencv\samples\cpp\opencv_core343d.dll
OpenCV(3.4.3-dev) Error: Unspecified error (OpenCV samples: Can't find required data file: 1.png) in cv::samples::findFile, file f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp, line 58
EXCEPTION -> OpenCV(3.4.3-dev) f:\lib\tmp\opencv\modules\core\src\utils\samples.cpp:58: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: 1.png in function 'cv::samples::findFile'


I attached my cmakecache.txt : i changed cmake variable CMAKE_INSTALL_PREFIX and all examples are installed in ${CMAKE_INSTALL_PREFIX}/samples
CMakeCache.txt

PS I copied all dlls in tmp/opencv/samples/cpp ( no conflict my regular install of opencv)

@alalek

alalek commented Sep 17, 2018

Copy link
Copy Markdown
Member Author

[DEBUG:0] Detected module path: 'F:\tmp\opencv\samples\cpp\opencv_core343d.dll'

I copied all dlls in tmp/opencv/samples/cpp

Actually this breaks the logic. Code assumes that DLLs are located in "bin" directory. And this "bin" directory is added to PATH (to launch ".exe").
Unfortunately Windows is not user friendly to do this without touching system-wide PATH environment (it is bad practice - other OpenCV versions will break). Just double click on ".exe" doesn't work =(

INSTALL_C_EXAMPLES:BOOL=OFF

Need to enable this for installation of samples data too. Currently there is nothing to search in "install" directory (no files at all).

@mshabunin

Copy link
Copy Markdown
Contributor

I have following suggestions:

  • if environment variable OPENCV_SAMPLES_DATA_PATH is set but the file can not be found there we should stop trying and log a warning
  • we should not store and check absolute paths, only relative locations (e.g "binaries build location" -> "source dir samples location"); otherwise we can get incorrect matches, e.g. from installed version
  • the search process should be based only on executable location, it can be determined using argv[0] (we can try locations from the PATH environment variable if it can not be found relative to current dir) or this method; users assume that application looks for files relative to the binary, not to the library located somewhere else
  • we should not check multiple subfolders, file location should contain full path, e.g. findFile("samples/data/lena.jpg)
  • this function should be possible to be combined with CommandLineParser: parser.setDefaultValue("filename", samples::findFile("samples/data/lena.jpg", EmptyOnFailure)) or incorporated into it: parser.setDefaultLocation("filename", "samples/data/lena.jpg")
  • we should not wrap this functionality in Python and Java, they should have thier own functions in separate modules: parser.add_argument(..., default=samples.getSampleDataFile("samples/data/lena.jpg"))
  • we should not provide general routine for data search, because it just can not be reliable by its nature
  • addDataSearch* functions seems to be unnecessary - we are trying to make automated solution, it does not need so many handles

I think we should cover only two usecases for C++ samples:

  • experimenting with build: user builds OpenCV with samples enabled, runs them from build folder, modifies the source, rebuilds;
    in this case we should check only "samples binaries location in build folder" -> "sample data in source folder" relative location
  • experimenting with prebuilt Windows package: user downloads Windows package, runs example application;
    in this case we should check "samples binaries install location" -> "samples data install location" relative location

Similar for python, but relative locations will be different.

All other cases can be considered "Advanced" and will be covered by OPENCV_SAMPLES_DATA_PATH environment variable and/or good old manual providing of file location in command line.

@alalek

alalek commented Sep 18, 2018

Copy link
Copy Markdown
Member Author
  • Warning is here.
  • Configuration variable here is just a hint. For example for extra custom collection of user pictures. User should be able to use OpenCV builtin collection too.
  1. I don't see here real problems. If "incorrect matches" can find file, then it is not bad.
    Complete absolute install path is for Linux system packages at first.
  2. As I say above, there is findDataFile() for searching OpenCV data by user apps to. Location of user custom application doesn't help to find OpenCV data. The same for custom build path of samples.
    Only location of OpenCV module can here (doesn't help with static builds). This approach works already in videoio module for ffmpeg wrapper search.
  3. Just 'baboon.jpg' should work too. It is useful after ideas like "lets move files" ... into modules/<name>/samples/data.
    Yes, subfolders is list generated by CMake. And there is an idea to add "text" module samples data from opencv_contrib.
  4. setDefaultValue() - definitely not "default" only. if user want to change "lena.jpg" to "baboon.jpg", then findFile() must work too without any requirement with providing of complete absolute path.
    Designed usage is:
    String input_file = parser.get<string>(...)
    Mat input = imread(samples::findFile(input_file));
    
    The same for Python / Java code. And later using the similar unified way for tests (ts, Python, Java).
  5. Few hours ago I fixed fresh code for searching tests data in Python tests (missing 'required' parameter). I believe the same problem is in Java tests, but they are not run on embedded platforms on CI. Searching for data files must be unified.
  6. Not reliable - is support for multiple implementations of the same stuff.
  7. Above

Usecases:

  • <build>/bin folder works now
  • winpack case: definitely winpack itself requires additional fixes to support this: there is no installed data, no prebuilt samples. This should be revised as a separate PR, but there is no way (and no more time) to integrate this with required quality before release because this PR is "on hold" for more than 2 weeks.
  • Running Python samples from <source>/samples/python / <source>/samples/dnn / etc. This patch cover these cases. Python with static linkage requires additional fixes.

@mshabunin

Copy link
Copy Markdown
Contributor
  1. Environment variable can contain a list of locations; IMO setting the variable implies "I want you to check only these locations" user intention, similar to behavior of PATH: export PATH= ; ls
  2. I do not agree, when user desperately modifies a file which he thinks is being loaded, but our smart mechanism decides that /usr/.../lena.jpg is also good, visible failure is better
  3. findDataFile is our commitment to support any OpenCV build and installation layouts, samples::findFile is a handy tool for example apps, similar to CommandLineParser; custom builds of samples should not be considered.
  4. I do not agree, deeper paths works as a safety check mechanism: data/img.jpg is better than "some file named img.jpg"; <module>/samples/data should not be considered, can be added to environment
  5. But in this case providing incomplete relative path can play a bad joke with a user if he mistakes a little
  6. Testdata search should be a different case with even less assumptions and no guessing; python scripts can be run from different locations and can use system/conda/venv OpenCV package, thus they should rely only on the script location
  7. see 3
  8. see 3

@alalek

alalek commented Oct 9, 2018

Copy link
Copy Markdown
Member Author

Updated.

@swoodford

Copy link
Copy Markdown

Oh, now I see I'm not the first one to try to fix this problem! 😄 Glad to help with the fix for the Python samples in any way.

@alalek

alalek commented Oct 31, 2018

Copy link
Copy Markdown
Member Author

Some samples are updated.

@alalek

alalek commented Nov 1, 2018

Copy link
Copy Markdown
Member Author

Any thoughts how to deal with "paths" like these: ../data/left%02d.jpg ?

@dkurt

dkurt commented Nov 1, 2018 •

Copy link
Copy Markdown
Member

@alalek, Can we insert some special symbol into default command line arguments which can be replaced by samples::findFile method?

update: All the methods try to find a single file. As for me, we can leave it for now.

if(WIN32)
file(RELATIVE_PATH INSTALL_DATA_DIR_RELATIVE "${CMAKE_INSTALL_PREFIX}/${OPENCV_BIN_INSTALL_PATH}" "${CMAKE_INSTALL_PREFIX}/${OPENCV_OTHER_INSTALL_PATH}")
else()
file(RELATIVE_PATH INSTALL_DATA_DIR_RELATIVE "${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}" "${CMAKE_INSTALL_PREFIX}/${OPENCV_OTHER_INSTALL_PATH}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here OPENCV_SAMPLES_SRC_INSTALL_PATH is used to determine relative location, but in samples/CMakeLists.txt, data directory is installed to the OPENCV_OTHER_INSTALL_PATH location.

SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants