pjreddie/darknet

Cant make the YOLO when OPENCV=1 Ubuntu 17.10

rucsacman opened this issue · 43 comments

Show error when try to make

gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -Wall -Wno-unknown-pragmas -Wfatal-```
errors -fPIC -Ofast -DOPENCV -c ./src/gemm.c -o obj/gemm.o
In file included from /usr/local/include/opencv2/core/types_c.h:59:0,
                 from /usr/local/include/opencv2/core/core_c.h:48,
                 from /usr/local/include/opencv2/highgui/highgui_c.h:45,
                 from include/darknet.h:25,
                 from ./src/utils.h:5,
                 from ./src/gemm.c:2:
/usr/local/include/opencv2/core/cvdef.h:485:1: error: unknown type name ‘namespace’
 namespace cv {
 ^~~~~~~~~
compilation terminated due to -Wfatal-errors.
Makefile:85: recipe for target 'obj/gemm.o' failed
make: *** [obj/gemm.o] Error 1

I also encountered the same issue with Mac OS High Sierra

➜  darknet git:(master) ✗ make
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -c ./src/gemm.c -o obj/gemm.o
In file included from ./src/gemm.c:2:
In file included from src/utils.h:5:
In file included from include/darknet.h:25:
In file included from /usr/local/include/opencv2/highgui/highgui_c.h:45:
In file included from /usr/local/include/opencv2/core/core_c.h:48:
In file included from /usr/local/include/opencv2/core/types_c.h:59:
/usr/local/include/opencv2/core/cvdef.h:485:1: fatal error: unknown type name 'namespace'
namespace cv {
^
1 error generated.
make: *** [obj/gemm.o] Error 1

Huge flop on my side. I was compiling OpenCV 2x because i cloned from Github OpenCV. Turned out I have to check out the latest release to be able to compile the c++ version. I recompiled Opencv 3x and then recompiled darknet. Issue resolved. That's how it happened for me at least.

Same error, I've this version of OpenCV

>>> cv2.__version__
'3.4.1-dev'

But I still get the Error

make
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -c ./src/gemm.c -o obj/gemm.o
In file included from /usr/local/include/opencv2/core/types_c.h:59:0,
                 from /usr/local/include/opencv2/core/core_c.h:48,
                 from /usr/local/include/opencv2/highgui/highgui_c.h:45,
                 from include/darknet.h:25,
                 from ./src/utils.h:5,
                 from ./src/gemm.c:2:
/usr/local/include/opencv2/core/cvdef.h:485:1: error: unknown type name ‘namespace’
 namespace cv {
 ^
compilation terminated due to -Wfatal-errors.
Makefile:85: set di istruzioni per l'obiettivo "obj/gemm.o" non riuscito
make: *** [o
bj/gemm.o] Errore 1

How do you solved?

Can’t read your error @selection

Thanks for quick response.
Yes, Sorry, I translated it.

make
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -c ./src/gemm.c -o obj/gemm.o
In file included from /usr/local/include/opencv2/core/types_c.h:59:0,
                 from /usr/local/include/opencv2/core/core_c.h:48,
                 from /usr/local/include/opencv2/highgui/highgui_c.h:45,
                 from include/darknet.h:25,
                 from ./src/utils.h:5,
                 from ./src/gemm.c:2:
/usr/local/include/opencv2/core/cvdef.h:485:1: error: unknown type name ‘namespace’
 namespace cv {
 ^
compilation terminated due to -Wfatal-errors.
Makefile:85: set of instructions for the objective "obj / gemm.o" failed
make: *** [obj/gemm.o] Error 1

Seems it search for OpenCV2 instead of OpenCV3
(in 4th line)
In file included from /usr/local/include/**opencv2**/core/types_c.h:59:0,

Yes OpenCV is built and works fine in other projects (python projects and c++)
I've installed OpenCV from source and built it manually, maybe I'm missing something, but I don't know what

Did you enable CUDA? I only enabled my OpenCV because I don't have CUDA. It worked on mine without CUDA enabled but very slow.

Check out this issue cvjena/darknet#9

Btw, mine also points to the same path (but w/o Cuda)

image

Yeah me too, just OpenCV, How do you solved? Installing OpenCV3?

The folder seems rhe same

ls  /usr/local/include/opencv2/
aruco            highgui               shape.hpp
aruco.hpp        highgui.hpp           stereo
bgsegm.hpp       imgcodecs             stereo.hpp
bioinspired      imgcodecs.hpp         stitching
bioinspired.hpp  img_hash              stitching.hpp
calib3d          img_hash.hpp          structured_light
calib3d.hpp      imgproc               structured_light.hpp
ccalib           imgproc.hpp           superres
ccalib.hpp       line_descriptor       superres.hpp
core             line_descriptor.hpp   surface_matching
core_detect.hpp  ml                    surface_matching.hpp
core.hpp         ml.hpp                text
cvconfig.h       objdetect             text.hpp
datasets         objdetect.hpp         tracking
dnn              opencv.hpp            tracking.hpp
dnn.hpp          opencv_modules.hpp    video
dpm.hpp          optflow               video.hpp
face             optflow.hpp           videoio
face.hpp         phase_unwrapping      videoio.hpp
features2d       phase_unwrapping.hpp  videostab
features2d.hpp   photo                 videostab.hpp
flann            photo.hpp             xfeatures2d
flann.hpp        plot.hpp              xfeatures2d.hpp
freetype.hpp     reg                   ximgproc
fuzzy            rgbd                  ximgproc.hpp
fuzzy.hpp        rgbd.hpp              xobjdetect.hpp
hdf              saliency              xphoto
hdf.hpp          saliency.hpp          xphoto.hpp
hfs.hpp          shape

Could you try the following commands?

echo `pkg-config --cflags opencv`

and then

echo `pkg-config --libs --cflags opencv`

mine has the following output.

➜  darknet git:(master) ✗ echo `pkg-config --cflags opencv`
-I/usr/local/include/opencv -I/usr/local/include
➜  darknet git:(master) ✗ echo `pkg-config --libs --cflags opencv`
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core

That's mine:

~/Develop/darknet$ echo `pkg-config --cflags opencv`
-I/usr/local/include/opencv -I/usr/local/include


~/Develop/darknet$ echo `pkg-config --libs --cflags opencv`
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_videostab -lopencv_stitching -lopencv_superres -lopencv_freetype -lopencv_aruco -lopencv_xfeatures2d -lopencv_shape -lopencv_hfs -lopencv_hdf -lopencv_reg -lopencv_img_hash -lopencv_optflow -lopencv_dnn_objdetect -lopencv_xobjdetect -lopencv_xphoto -lopencv_ccalib -lopencv_bgsegm -lopencv_fuzzy -lopencv_rgbd -lopencv_surface_matching -lopencv_dpm -lopencv_bioinspired -lopencv_stereo -lopencv_saliency -lopencv_line_descriptor -lopencv_ximgproc -lopencv_face -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_video -lopencv_plot -lopencv_photo -lopencv_objdetect -lopencv_ml -lopencv_structured_light -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_phase_unwrapping -lopencv_imgproc -lopencv_flann -lopencv_core

I've this in addiotion at yours, but i don't thin that can be a problem
{'-lopencv_hfs', '-lopencv_hdf', '-lopencv_dnn_objdetect'}

not sure . i removed some of mine because they have no support on my laptop. im just gonna send my proj to you for reference.

this is the log for my successful build.

https://drive.google.com/open?id=1vwOO6FJq1UkmfvovMKBcqke47ATuVzdJ

and this is my make file

GPU=0
CUDNN=0
OPENCV=1
OPENMP=0
DEBUG=0

ARCH= -gencode arch=compute_30,code=sm_30 \
      -gencode arch=compute_35,code=sm_35 \
      -gencode arch=compute_50,code=[sm_50,compute_50] \
      -gencode arch=compute_52,code=[sm_52,compute_52]
#      -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?

# This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52

VPATH=./src/:./examples
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/

CC=gcc
NVCC=nvcc
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC

ifeq ($(OPENMP), 1)
CFLAGS+= -fopenmp
endif

ifeq ($(DEBUG), 1)
OPTS=-O0 -g
endif

CFLAGS+=$(OPTS)

ifeq ($(OPENCV), 1)
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`
endif

ifeq ($(GPU), 1)
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif

ifeq ($(CUDNN), 1)
COMMON+= -DCUDNN
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif

OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o  lstm_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o attention.o darknet.o
ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif

EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h

#all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj  results $(SLIB) $(ALIB) $(EXEC)


$(EXEC): $(EXECOBJ) $(ALIB)
	$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB)

$(ALIB): $(OBJS)
	$(AR) $(ARFLAGS) $@ $^

$(SLIB): $(OBJS)
	$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

$(OBJDIR)%.o: %.c $(DEPS)
	$(CC) $(COMMON) $(CFLAGS) -c $< -o $@

$(OBJDIR)%.o: %.cu $(DEPS)
	$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@

obj:
	mkdir -p obj
backup:
	mkdir -p backup
results:
	mkdir -p results

.PHONY: clean

clean:
	rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ)

and lastly, my mistake was mistakenly building and installing the old version of opencv when i cloned from git. i had to do a git checkout 3.4.0 within opencv git dir using the latest version found on the release.

It's funny how i built the older version on my first try because it was on top of the release page on github for opencv.
image

Thanks

And maybe lastly just to cover all bases here. You may want to install CUDA library. i installed mine from nvidia even though i had no GPU on my mac. maybe that influenced somehow for now. and also make sure to use a new terminal from scratch everytime you build if you've installed anything via make install or from the package installer. im using mac, and sometimes changes to environment variables dont reflect immediately if they're done externally.you either have to do a source (some file) or just summon a new terminal.

image

the webcam also works. but really really slow. im looking in to trying with GPU next.

I've AMD R9 Fury as GPU, should I install CUDA?

you can still instll cuda so that that libs are on - and maybe that has any influence on the build. but it will never work with gpu acceleration (and you shouldn't set gpu to true) because cuda is nvidia stuff.

most of these guys really only prioritize Nvidia stuff. case in point the Facebook implementation https://github.com/facebookresearch/Detectron/blob/master/INSTALL.md and they're really good too.
image

Same here... problem with the make...

gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -c ./src/gemm.c -o obj/gemm.o
In file included from ./src/gemm.c:2:
In file included from src/utils.h:5:
In file included from include/darknet.h:25:
In file included from /usr/local/Cellar/opencv/3.4.1_1/include/opencv2/highgui/highgui_c.h:45:
In file included from /usr/local/Cellar/opencv/3.4.1_1/include/opencv2/core/core_c.h:48:
In file included from /usr/local/Cellar/opencv/3.4.1_1/include/opencv2/core/types_c.h:59:
/usr/local/Cellar/opencv/3.4.1_1/include/opencv2/core/cvdef.h:485:1: fatal error: unknown type name 'namespace'

gcc version...

gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Anybody facing the same?

For the original issues : Under ubuntu 17.x it builds ok, provided you installed the OpenCV libraries
sudo apt install libopencv-dev

For Cuda ... you may try :
sudo apt install nvidia-cuda-toolkit
and replace nvcc with nvcc -ccbin clang-3.8

I have the same issue as @pcstory. Running mac high sierra.

Ran the following

brew install opencv
without error

updated Makefile to set:

OPENCV=1

then run
make

error with the following message
/usr/local/Cellar/opencv/3.4.1_2/include/opencv2/core/cvdef.h:485:1: fatal error: unknown type name 'namespace' namespace cv { ^ 1 error generated. make: *** [obj/gemm.o] Error 1

Anyone have a solution?

Thank you

I had the issue with not being able to compile with openCV on mac but have resolved the issue. As @NguyenAiSi points out (and @joshholla requested details for) openCV 3 didn't work and that openCV 2.4 works. Although installing and setting the environment variables aren't as simple as a typical 'brew install xxxx'.

First uninstall installed versions of opencv
$ brew uninstall opencv

Then install openCV 2.4
$ brew install opencv@2

The messages after install are:
`==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/opencv@2/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/opencv@2/lib
CPPFLAGS: -I/usr/local/opt/opencv@2/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/opencv@2/lib/pkgconfig`

First time around I missed these messages. Instead run the following commands:

echo 'export PATH="/usr/local/opt/opencv@2/bin:$PATH"' >> ~/.bash_profile

then
export LDFLAGS=-L/usr/local/opt/opencv@2/lib
export CPPFLAGS=-I/usr/local/opt/opencv@2/include
export PKG_CONFIG_PATH=/usr/local/opt/opencv@2/lib/pkgconfig

After that I edited the Makefile
$ vi Makefile

and set
OPENCV = 1

saved and then ran
$ make

This created a lot of warning messages but ultimately compiled.

Then I could run the webcam demo:
./darknet detector demo cfg/coco.data cfg/yolo.cfg yolo.weights

And while this strictly speaking "worked" the frame rate was less than 1 FPS and so very slow.

With a macbook pro I don't have the option to use the onboard non-nvidia GPU to speed this up.

I did everything as per lomnes instructions. The last step of $ make in darknet failed with linker error below. So detection works fine but still no luck with Opencv

Undefined symbols for architecture x86_64:
"_cvCreateCameraCapture", referenced from:
_demo in demo.o
"_cvCreateFileCapture", referenced from:
_demo in demo.o
"_cvCreateImage", referenced from:
_show_image in image.o
_save_image_jpg in image.o
_demo in demo.o
"_cvLoadImage", referenced from:
_load_image_cv in image.o
"_cvMoveWindow", referenced from:
_demo in demo.o
"_cvNamedWindow", referenced from:
_show_image_cv in image.o
_demo in demo.o
"_cvQueryFrame", referenced from:
_flush_stream_buffer in image.o
_get_image_from_stream in image.o
_fill_image_from_stream in image.o
"_cvReleaseImage", referenced from:
_show_image in image.o
_load_image_cv in image.o
_save_image_jpg in image.o
"_cvResizeWindow", referenced from:
_demo in demo.o
"_cvSaveImage", referenced from:
_save_image_jpg in image.o
"_cvSetCaptureProperty", referenced from:
_demo in demo.o
"_cvSetWindowProperty", referenced from:
_demo in demo.o
"_cvShowImage", referenced from:
_show_image_cv in image.o
"_cvWaitKey", referenced from:
_test_resize in image.o
_display_in_thread in demo.o
ld: symbol(s) not found for architecture x86_64
clang: fatal error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libdarknet.so] Error 1

Thanks for lomnes, and yolo.....
After uninstall opencv3 and follow lomnes's comments.
Webcam is working!
2018-04-05 10 23 27

I compile darknet with opencv 3.4.0 in Arch Linux, it works great.

Hi All,

I had the same problem. The message fatal error: unknown type name 'namespace' was a giveaway that somehow some C++ code was making it past the preprocessor (namespace is a keyword in C++ but not C, which darknet is written in). Basically this is a bug in OpenCV (I'm using 3.4.1_2 which I installed via Homebrew on my Mac), and I'm not sure when it was introduced or when it was resolved (looks like from what @hubutui commented, however, the bug may not have been in 3.4.0).

In any case, to work around this without playing Russian roulette with OpenCV versions, I made two changes to the OpenCV header files (one change is somewhat dubious, the other is most likely a bug fix).

Change 1: Highly Dubious, May Break Other Builds, Proceed at Own Risk

In /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/core/cvdef.h:
Change:

#else
#include <stdint.h>
namespace cv {
typedef ::int8_t int8_t;
typedef ::uint8_t uint8_t;
typedef ::int16_t int16_t;
typedef ::uint16_t uint16_t;
typedef ::int32_t int32_t;
typedef ::uint32_t uint32_t;
typedef ::int64_t int64_t;
typedef ::uint64_t uint64_t;
}
#endif

To this (just comment it out)

#else
#include <stdint.h>
//namespace cv {
//typedef ::int8_t int8_t;
//typedef ::uint8_t uint8_t;
//typedef ::int16_t int16_t;
//typedef ::uint16_t uint16_t;
//typedef ::int32_t int32_t;
//typedef ::uint32_t uint32_t;
//typedef ::int64_t int64_t;
//typedef ::uint64_t uint64_t;
//}
#endif

Change 2: Not as Dubious, Likely an Actual Bug

In /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/highgui/highgui_c.h
Change

CVAPI(cv::Rect)cvGetWindowImageRect(const char* name);

To this:

CVAPI(CvRect)cvGetWindowImageRect(const char* name);

Hope that works for for anyone else with this version. Otherwise library version Russian roulette may be a better option.

To fix using Homebrew without uninstalling opencv3, install opencv@2:
brew install opencv@2

Then change the LDFLAGS and COMMON flags to:

LDFLAGS+= `pkg-config --libs /usr/local/opt/opencv@2/lib/pkgconfig/opencv.pc` 
COMMON+= `pkg-config --cflags /usr/local/opt/opencv@2/lib/pkgconfig/opencv.pc` 

darknet/Makefile

Lines 44 to 45 in e84933b

LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`

my laptop have graphic processor Geforce Nvidia 820M and i also installed CUDA toolkit 9.1 and OPENCV 3.3.1.
after running this command,

./darknet detector demo cfg/coco.data cfg/yolo.cfg yolo.weights
everything worked fine, except the real time object detection wasn't in real time FPS was below 1 and streaming was tremendously slow. and also after cd darknet, & make command the fatal error is occuring, please guide me.

the answer from @JHumphreyJr work for me on macOS.

Besides, use brew info opencv@2 to check if all dependencies are installed.

I had the issue with not being able to compile with openCV on mac but have resolved the issue. As @NguyenAiSi points out (and @joshholla requested details for) openCV 3 didn't work and that openCV 2.4 works. Although installing and setting the environment variables aren't as simple as a typical 'brew install xxxx'.

First uninstall installed versions of opencv
$ brew uninstall opencv

Then install openCV 2.4
$ brew install opencv@2

The messages after install are:
`==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/opencv@2/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/opencv@2/lib
CPPFLAGS: -I/usr/local/opt/opencv@2/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/opencv@2/lib/pkgconfig`

First time around I missed these messages. Instead run the following commands:

echo 'export PATH="/usr/local/opt/opencv@2/bin:$PATH"' >> ~/.bash_profile

then
export LDFLAGS=-L/usr/local/opt/opencv@2/lib
export CPPFLAGS=-I/usr/local/opt/opencv@2/include
export PKG_CONFIG_PATH=/usr/local/opt/opencv@2/lib/pkgconfig

After that I edited the Makefile
$ vi Makefile

and set
OPENCV = 1

saved and then ran
$ make

This created a lot of warning messages but ultimately compiled.

Then I could run the webcam demo:
./darknet detector demo cfg/coco.data cfg/yolo.cfg yolo.weights

And while this strictly speaking "worked" the frame rate was less than 1 FPS and so very slow.

With a macbook pro I don't have the option to use the onboard non-nvidia GPU to speed this up.

Resolved my problem

Hello,
Same error on Win 10 and VC 17; tried all possible OpenCV versions mentioned above, nothing seems to work.

image

Using this repo: https://github.com/AlexeyAB/darknet#how-to-compile-on-windows

SOLUTION: 'namespace' is breaking stuff because it is C++ (while YOLO is in C). You can test for being in C++ with the if-statement: #if defined __cplusplus. See solution here: opencv/opencv#10963 (comment)
By changing the file as the solution shows, you will fix the issue on opencv 3.4.1.

@bayesianio I don't know what opencv version that is but you could probably just put the namespace line in an if-statement with a C++ check and it would work.

I have already installed opencv(2.4) via brew with python 2.7
Python 2.7.15 (default)

import cv2
print(cv2.version)
2.4.13.6
exit()
When editing makefile for OPENCV=1 and make:
g++ -DOPENCV pkg-config --cflags opencv -Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas -Ofast -DOPENCV obj/http_stream.o obj/gemm.o obj/utils.o obj/cuda.o obj/convolutional_layer.o obj/list.o obj/image.o obj/activations.o obj/im2col.o obj/col2im.o obj/blas.o obj/crop_layer.o obj/dropout_layer.o obj/maxpool_layer.o obj/softmax_layer.o obj/data.o obj/matrix.o obj/network.o obj/connected_layer.o obj/cost_layer.o obj/parser.o obj/option_list.o obj/darknet.o obj/detection_layer.o obj/captcha.o obj/route_layer.o obj/writing.o obj/box.o obj/nightmare.o obj/normalization_layer.o obj/avgpool_layer.o obj/coco.o obj/dice.o obj/yolo.o obj/detector.o obj/layer.o obj/compare.o obj/classifier.o obj/local_layer.o obj/swag.o obj/shortcut_layer.o obj/activation_layer.o obj/rnn_layer.o obj/gru_layer.o obj/rnn.o obj/rnn_vid.o obj/crnn_layer.o obj/demo.o obj/tag.o obj/cifar.o obj/go.o obj/batchnorm_layer.o obj/art.o obj/region_layer.o obj/reorg_layer.o obj/reorg_old_layer.o obj/super.o obj/voxel.o obj/tree.o obj/yolo_layer.o obj/upsample_layer.o -o darknet -lm -pthread pkg-config --libs opencv
Undefined symbols for architecture x86_64:
"cv::cvarrToMat(void const*, bool, bool, int, cv::AutoBuffer<double, 136ul>)", referenced from:
_send_mjpeg in http_stream.o
_image_data_augmentation in http_stream.o
"cv::VideoCapture::VideoCapture(cv::String const&)", referenced from:
_get_capture_video_stream in http_stream.o
"cv::String::deallocate()", referenced from:
MJPGWriter::write(cv::Mat const&) in http_stream.o
_get_capture_video_stream in http_stream.o
cvflann::anyimpl::big_any_policycv::String::static_delete(void**) in http_stream.o
cvflann::anyimpl::big_any_policycv::String::move(void const*, void**) in http_stream.o
"cv::String::allocate(unsigned long)", referenced from:
MJPGWriter::write(cv::Mat const&) in http_stream.o
_get_capture_video_stream in http_stream.o
"cv::imencode(cv::String const&, cv::_InputArray const&, std::__1::vector<unsigned char, std::__1::allocator >&, std::__1::vector<int, std::__1::allocator > const&)", referenced from:
MJPGWriter::write(cv::Mat const&) in http_stream.o
"_IplImage::_IplImage(cv::Mat const&)", referenced from:
_get_webcam_frame in http_stream.o
_image_data_augmentation in http_stream.o
ld: symbol(s) not found for architecture x86_64
clang: fatal error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [darknet] Error 1
fatal error : ld: symbol(s) not found for architecture x86_64

@jdellithorpe
that's great it works!

I am running into the issue on make opencv work in darknet. Please help.

ubuntu@ubuntu-ThinkPad-X1-Carbon:/darknet$ make
g++ -Iinclude/ -Isrc/ -DOPENCV pkg-config --cflags opencv -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -c ./src/image_opencv.cpp -o obj/image_opencv.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
No package 'opencv' found
./src/image_opencv.cpp:5:10: fatal error: opencv2/opencv.hpp: No such file or directory
#include "opencv2/opencv.hpp"
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:86: recipe for target 'obj/image_opencv.o' failed
make: *** [obj/image_opencv.o] Error 1
ubuntu@ubuntu-ThinkPad-X1-Carbon:
/darknet$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import cv2
cv2.version
'4.0.0'
quit()
ubuntu@ubuntu-ThinkPad-X1-Carbon:~/darknet$

Huge flop on my side. I was compiling OpenCV 2x because i cloned from Github OpenCV. Turned out I have to check out the latest release to be able to compile the c++ version. I recompiled Opencv 3x and then recompiled darknet. Issue resolved. That's how it happened for me at least.

Can You please explain how did you recompile Opencv3 step by step? I can't make the program work as I have the same issue

UPDATE:
it worked for me after I installed the latest version of opencv by this link https://github.com/jayrambhia/Install-OpenCV

Hi All,

I had the same problem. The message fatal error: unknown type name 'namespace' was a giveaway that somehow some C++ code was making it past the preprocessor (namespace is a keyword in C++ but not C, which darknet is written in). Basically this is a bug in OpenCV (I'm using 3.4.1_2 which I installed via Homebrew on my Mac), and I'm not sure when it was introduced or when it was resolved (looks like from what @hubutui commented, however, the bug may not have been in 3.4.0).

In any case, to work around this without playing Russian roulette with OpenCV versions, I made two changes to the OpenCV header files (one change is somewhat dubious, the other is most likely a bug fix).

Change 1: Highly Dubious, May Break Other Builds, Proceed at Own Risk

In /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/core/cvdef.h:
Change:

#else
#include <stdint.h>
namespace cv {
typedef ::int8_t int8_t;
typedef ::uint8_t uint8_t;
typedef ::int16_t int16_t;
typedef ::uint16_t uint16_t;
typedef ::int32_t int32_t;
typedef ::uint32_t uint32_t;
typedef ::int64_t int64_t;
typedef ::uint64_t uint64_t;
}
#endif

To this (just comment it out)

#else
#include <stdint.h>
//namespace cv {
//typedef ::int8_t int8_t;
//typedef ::uint8_t uint8_t;
//typedef ::int16_t int16_t;
//typedef ::uint16_t uint16_t;
//typedef ::int32_t int32_t;
//typedef ::uint32_t uint32_t;
//typedef ::int64_t int64_t;
//typedef ::uint64_t uint64_t;
//}
#endif

Change 2: Not as Dubious, Likely an Actual Bug

In /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/highgui/highgui_c.h
Change

CVAPI(cv::Rect)cvGetWindowImageRect(const char* name);

To this:

CVAPI(CvRect)cvGetWindowImageRect(const char* name);

Hope that works for for anyone else with this version. Otherwise library version Russian roulette may be a better option.

I didn't find any folder named "Cellar" in /usr/local directory... I have Linux mint 18.

By adding opencv.pc to my PKG_CONFIG_PATH I solved this problem.
opencv.pc is in /path/to/your/opencv/lib/pkgconfig
Hope this helps.

I resolved it using following steps:

  1. Installed opencv using sudo apt install libopencv-dev
  2. Opened a new terminal (make sure that you are using version as installed in the 1st step using following command):
    python -c "import cv2; print(cv2.__version__);
    (In my case, bashrc was setting up env vars and another installation of opencv was active and I had to comment out relevant lines in bashrc)
  3. make after setting OPENCV=1 in Makefile

Well not much of use, just got 2 FPS :(
Versions (other versions might also work):
Opencv: 4.2.0
No CUDA/CUDNN in use

For the original issues : Under ubuntu 17.x it builds ok, provided you installed the OpenCV libraries
sudo apt install libopencv-dev

For Cuda ... you may try :
sudo apt install nvidia-cuda-toolkit
and replace nvcc with nvcc -ccbin clang-3.8

Thank you so much ,it worked.

Hello, I just have a question
Is GPU Nvidia gt 840m compatible with yolov4 ?????

Hello I have the following problem

lip@lip-desktop:~/darknet$ make
g++ -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/image_opencv.cpp -o obj/image_opencv.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
No package 'opencv' found
./src/image_opencv.cpp:5:10: fatal error: opencv/opencv2: No such file or directory
 #include "opencv/opencv2"
          ^~~~~~~~~~~~~~~~
compilation terminated.
Makefile:86: recipe for target 'obj/image_opencv.o' failed
make: *** [obj/image_opencv.o] Error 1