"opencv2: no such file or directory" 在尝试构建 Darknet 时?

"opencv2: no such file or directory" while trying to build Darknet?

我正在尝试将 Darkent 与 OpenCV 和 CUDA 结合使用。我根据这些说明安装了暗网:

https://pjreddie.com/darknet/install/

我根据这些说明安装了 CUDA:

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

最后,我按照这些说明安装了 OpenCV:

http://www.linuxfromscratch.org/blfs/view/svn/general/opencv.html

然后我将以下行添加到我的 bashrc 的末尾:

export PATH=/usr/local/cuda-11.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}

接下来,我修改了darknet目录下的Makefile,GPU=1,OPENCV=1。我重新制作了,并且 运行 变成了一堆重复的错误说:

No package 'opencv' found
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -c ./src/lstm_layer.c -o obj/lstm_layer.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable

我检查了一下,虽然我已经将目录“/usr/local/lib/pkgconfig”添加到我的 PKG_CONFIG_PATH,但那里没有 opencv.pc 文件。我用谷歌搜索了这个,并阅读了一个建议手动创建文件的答案,所以这就是我所做的,内容如下:

prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

这解决了上面提到的重复错误,但是我在做的时候仍然得到类似的错误:

./src/image_opencv.cpp:5:10: fatal error: opencv2/opencv.hpp: No such file or directory
    5 | #include "opencv2/opencv.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.

我不确定我是否解决了第一个问题,这是一个单独的问题,或者第一个问题是否只解决了一个症状,而不是问题所在。此后,我也尝试过:

sudo apt install libopencv-dev

没有效果。

pkg-config --modversion

产生:2.x.x

pkg-config --cflags opencv

产生:

[code]
-I/usr/include/opencv -I/usr/include/opencv2
[/code]

如有任何帮助,我们将不胜感激。我是 运行 ubuntu 20.04,内核 5.4.0-53-generic。

我试过了,成功了,用这些内容创建 opencv.pc 文件:

# Package Information for pkg-config

prefix=/usr
exec_prefix=${prefix}
libdir=${prefix}/lib/x86_64-linux-gnu
includedir_new=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.0
Libs: -L${libdir} -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_new}

并将其放入 /usr/local/lib/pkgconfig/.