lol
1{ lib
2, stdenv
3, fetchurl
4, fetchFromGitHub
5, cmake
6, pkg-config
7, unzip
8, zlib
9, pcre
10, hdf5
11, boost
12, gflags
13, protobuf
14, config
15, ocl-icd
16, buildPackages
17
18, enableJPEG ? true
19, libjpeg
20, enablePNG ? true
21, libpng
22, enableTIFF ? true
23, libtiff
24, enableWebP ? true
25, libwebp
26, enableEXR ? !stdenv.isDarwin
27, openexr
28, ilmbase
29, enableJPEG2000 ? true
30, openjpeg
31, enableEigen ? true
32, eigen
33, enableOpenblas ? true
34, openblas
35, enableContrib ? true
36
37, enableCuda ? (config.cudaSupport or false) && stdenv.hostPlatform.isx86_64
38, cudatoolkit
39, nvidia-optical-flow-sdk
40
41, enableUnfree ? false
42, enableIpp ? false
43, enablePython ? false
44, pythonPackages
45, enableGtk2 ? false
46, gtk2
47, enableGtk3 ? false
48, gtk3
49, enableVtk ? false
50, vtk
51, enableFfmpeg ? true
52, ffmpeg
53, enableGStreamer ? true
54, gst_all_1
55, enableTesseract ? false
56, tesseract
57, leptonica
58, enableTbb ? false
59, tbb
60, enableOvis ? false
61, ogre
62, enableGPhoto2 ? false
63, libgphoto2
64, enableDC1394 ? false
65, libdc1394
66, enableDocs ? false
67, doxygen
68, graphviz-nox
69
70, AVFoundation
71, Cocoa
72, VideoDecodeAcceleration
73, CoreMedia
74, MediaToolbox
75, bzip2
76}:
77
78let
79 version = "4.6.0";
80
81 src = fetchFromGitHub {
82 owner = "opencv";
83 repo = "opencv";
84 rev = version;
85 sha256 = "sha256-zPkMc6xEDZU5TlBH3LAzvB17XgocSPeHVMG/U6kfpxg=";
86 };
87
88 contribSrc = fetchFromGitHub {
89 owner = "opencv";
90 repo = "opencv_contrib";
91 rev = version;
92 sha256 = "sha256-hjRqT7V4Sz7t4IEy89F5M+b0x2ObBbqF8GWLKhWFXtE=";
93 };
94
95 # Contrib must be built in order to enable Tesseract support:
96 buildContrib = enableContrib || enableTesseract || enableOvis;
97
98 # See opencv/3rdparty/ippicv/ippicv.cmake
99 ippicv = {
100 src = fetchFromGitHub {
101 owner = "opencv";
102 repo = "opencv_3rdparty";
103 rev = "a56b6ac6f030c312b2dce17430eef13aed9af274";
104 sha256 = "1msbkc3zixx61rcg6a04i1bcfhw1phgsrh93glq1n80hgsk3nbjq";
105 } + "/ippicv";
106 files = let name = platform: "ippicv_2019_${platform}_general_20180723.tgz"; in
107 if stdenv.hostPlatform.system == "x86_64-linux" then
108 { ${name "lnx_intel64"} = "c0bd78adb4156bbf552c1dfe90599607"; }
109 else if stdenv.hostPlatform.system == "i686-linux" then
110 { ${name "lnx_ia32"} = "4f38432c30bfd6423164b7a24bbc98a0"; }
111 else if stdenv.hostPlatform.system == "x86_64-darwin" then
112 { ${name "mac_intel64"} = "fe6b2bb75ae0e3f19ad3ae1a31dfa4a2"; }
113 else
114 throw "ICV is not available for this platform (or not yet supported by this package)";
115 dst = ".cache/ippicv";
116 };
117
118 # See opencv_contrib/modules/xfeatures2d/cmake/download_vgg.cmake
119 vgg = {
120 src = fetchFromGitHub {
121 owner = "opencv";
122 repo = "opencv_3rdparty";
123 rev = "fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d";
124 sha256 = "0r9fam8dplyqqsd3qgpnnfgf9l7lj44di19rxwbm8mxiw0rlcdvy";
125 };
126 files = {
127 "vgg_generated_48.i" = "e8d0dcd54d1bcfdc29203d011a797179";
128 "vgg_generated_64.i" = "7126a5d9a8884ebca5aea5d63d677225";
129 "vgg_generated_80.i" = "7cd47228edec52b6d82f46511af325c5";
130 "vgg_generated_120.i" = "151805e03568c9f490a5e3a872777b75";
131 };
132 dst = ".cache/xfeatures2d/vgg";
133 };
134
135 # See opencv_contrib/modules/xfeatures2d/cmake/download_boostdesc.cmake
136 boostdesc = {
137 src = fetchFromGitHub {
138 owner = "opencv";
139 repo = "opencv_3rdparty";
140 rev = "34e4206aef44d50e6bbcd0ab06354b52e7466d26";
141 sha256 = "13yig1xhvgghvxspxmdidss5lqiikpjr0ddm83jsi0k85j92sn62";
142 };
143 files = {
144 "boostdesc_bgm.i" = "0ea90e7a8f3f7876d450e4149c97c74f";
145 "boostdesc_bgm_bi.i" = "232c966b13651bd0e46a1497b0852191";
146 "boostdesc_bgm_hd.i" = "324426a24fa56ad9c5b8e3e0b3e5303e";
147 "boostdesc_binboost_064.i" = "202e1b3e9fec871b04da31f7f016679f";
148 "boostdesc_binboost_128.i" = "98ea99d399965c03d555cef3ea502a0b";
149 "boostdesc_binboost_256.i" = "e6dcfa9f647779eb1ce446a8d759b6ea";
150 "boostdesc_lbgm.i" = "0ae0675534aa318d9668f2a179c2a052";
151 };
152 dst = ".cache/xfeatures2d/boostdesc";
153 };
154
155 # See opencv_contrib/modules/face/CMakeLists.txt
156 face = {
157 src = fetchFromGitHub {
158 owner = "opencv";
159 repo = "opencv_3rdparty";
160 rev = "8afa57abc8229d611c4937165d20e2a2d9fc5a12";
161 sha256 = "061lsvqdidq9xa2hwrcvwi9ixflr2c2lfpc8drr159g68zi8bp4v";
162 };
163 files = {
164 "face_landmark_model.dat" = "7505c44ca4eb54b4ab1e4777cb96ac05";
165 };
166 dst = ".cache/data";
167 };
168
169 # See opencv/modules/gapi/cmake/DownloadADE.cmake
170 ade = rec {
171 src = fetchurl {
172 url = "https://github.com/opencv/ade/archive/${name}";
173 sha256 = "04n9na2bph706bdxnnqfcbga4cyj8kd9s9ni7qyvnpj5v98jwvlm";
174 };
175 name = "v0.1.1f.zip";
176 md5 = "b624b995ec9c439cbc2e9e6ee940d3a2";
177 dst = ".cache/ade";
178 };
179
180 # See opencv_contrib/modules/wechat_qrcode/CMakeLists.txt
181 wechat_qrcode = {
182 src = fetchFromGitHub {
183 owner = "opencv";
184 repo = "opencv_3rdparty";
185 rev = "a8b69ccc738421293254aec5ddb38bd523503252";
186 sha256 = "sha256-/n6zHwf0Rdc4v9o4rmETzow/HTv+81DnHP+nL56XiTY=";
187 };
188 files = {
189 "detect.caffemodel" = "238e2b2d6f3c18d6c3a30de0c31e23cf";
190 "detect.prototxt" = "6fb4976b32695f9f5c6305c19f12537d";
191 "sr.caffemodel" = "cbfcd60361a73beb8c583eea7e8e6664";
192 "sr.prototxt" = "69db99927a70df953b471daaba03fbef";
193 };
194 dst = ".cache/wechat_qrcode";
195 };
196
197 # See opencv/cmake/OpenCVDownload.cmake
198 installExtraFiles = extra: with lib; ''
199 mkdir -p "${extra.dst}"
200 '' + concatStrings (flip mapAttrsToList extra.files (name: md5: ''
201 ln -s "${extra.src}/${name}" "${extra.dst}/${md5}-${name}"
202 ''));
203 installExtraFile = extra: ''
204 mkdir -p "${extra.dst}"
205 ln -s "${extra.src}" "${extra.dst}/${extra.md5}-${extra.name}"
206 '';
207
208 opencvFlag = name: enabled: "-DWITH_${name}=${printEnabled enabled}";
209
210 printEnabled = enabled: if enabled then "ON" else "OFF";
211in
212
213stdenv.mkDerivation {
214 pname = "opencv";
215 inherit version src;
216
217 postUnpack = lib.optionalString buildContrib ''
218 cp --no-preserve=mode -r "${contribSrc}/modules" "$NIX_BUILD_TOP/source/opencv_contrib"
219 '';
220
221 # Ensures that we use the system OpenEXR rather than the vendored copy of the source included with OpenCV.
222 patches = [
223 ./cmake-don-t-use-OpenCVFindOpenEXR.patch
224 ] ++ lib.optional enableCuda ./cuda_opt_flow.patch;
225
226 # This prevents cmake from using libraries in impure paths (which
227 # causes build failure on non NixOS)
228 postPatch = ''
229 sed -i '/Add these standard paths to the search paths for FIND_LIBRARY/,/^\s*$/{d}' CMakeLists.txt
230 '';
231
232 preConfigure =
233 installExtraFile ade +
234 lib.optionalString enableIpp (installExtraFiles ippicv) + (
235 lib.optionalString buildContrib ''
236 cmakeFlagsArray+=("-DOPENCV_EXTRA_MODULES_PATH=$NIX_BUILD_TOP/source/opencv_contrib")
237
238 ${installExtraFiles vgg}
239 ${installExtraFiles boostdesc}
240 ${installExtraFiles face}
241 ${installExtraFiles wechat_qrcode}
242 ''
243 );
244
245 postConfigure = ''
246 [ -e modules/core/version_string.inc ]
247 echo '"(build info elided)"' > modules/core/version_string.inc
248 '';
249
250 buildInputs = [ zlib pcre boost gflags protobuf ]
251 ++ lib.optional enablePython pythonPackages.python
252 ++ lib.optional (stdenv.buildPlatform == stdenv.hostPlatform) hdf5
253 ++ lib.optional enableGtk2 gtk2
254 ++ lib.optional enableGtk3 gtk3
255 ++ lib.optional enableVtk vtk
256 ++ lib.optional enableJPEG libjpeg
257 ++ lib.optional enablePNG libpng
258 ++ lib.optional enableTIFF libtiff
259 ++ lib.optional enableWebP libwebp
260 ++ lib.optionals enableEXR [ openexr ilmbase ]
261 ++ lib.optional enableJPEG2000 openjpeg
262 ++ lib.optional enableFfmpeg ffmpeg
263 ++ lib.optionals (enableFfmpeg && stdenv.isDarwin)
264 [ VideoDecodeAcceleration bzip2 ]
265 ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
266 ++ lib.optional enableOvis ogre
267 ++ lib.optional enableGPhoto2 libgphoto2
268 ++ lib.optional enableDC1394 libdc1394
269 ++ lib.optional enableEigen eigen
270 ++ lib.optional enableOpenblas openblas
271 # There is seemingly no compile-time flag for Tesseract. It's
272 # simply enabled automatically if contrib is built, and it detects
273 # tesseract & leptonica.
274 ++ lib.optionals enableTesseract [ tesseract leptonica ]
275 ++ lib.optional enableTbb tbb
276 ++ lib.optionals stdenv.isDarwin [ bzip2 AVFoundation Cocoa VideoDecodeAcceleration CoreMedia MediaToolbox ]
277 ++ lib.optionals enableDocs [ doxygen graphviz-nox ];
278
279 propagatedBuildInputs = lib.optional enablePython pythonPackages.numpy
280 ++ lib.optionals enableCuda [ cudatoolkit nvidia-optical-flow-sdk ];
281
282 nativeBuildInputs = [ cmake pkg-config unzip ]
283 ++ lib.optionals enablePython [
284 pythonPackages.pip
285 pythonPackages.wheel
286 pythonPackages.setuptools
287 ];
288
289 NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";
290
291 # Configure can't find the library without this.
292 OpenBLAS_HOME = lib.optionalString enableOpenblas openblas;
293
294 cmakeFlags = [
295 "-DOPENCV_GENERATE_PKGCONFIG=ON"
296 "-DWITH_OPENMP=ON"
297 "-DBUILD_PROTOBUF=OFF"
298 "-DProtobuf_PROTOC_EXECUTABLE=${lib.getExe buildPackages.protobuf}"
299 "-DPROTOBUF_UPDATE_FILES=ON"
300 "-DOPENCV_ENABLE_NONFREE=${printEnabled enableUnfree}"
301 "-DBUILD_TESTS=OFF"
302 "-DBUILD_PERF_TESTS=OFF"
303 "-DBUILD_DOCS=${printEnabled enableDocs}"
304 # "OpenCV disables pkg-config to avoid using of host libraries. Consider using PKG_CONFIG_LIBDIR to specify target SYSROOT"
305 # but we have proper separation of build and host libs :), fixes cross
306 "-DOPENCV_ENABLE_PKG_CONFIG=ON"
307 (opencvFlag "IPP" enableIpp)
308 (opencvFlag "TIFF" enableTIFF)
309 (opencvFlag "WEBP" enableWebP)
310 (opencvFlag "JPEG" enableJPEG)
311 (opencvFlag "PNG" enablePNG)
312 (opencvFlag "OPENEXR" enableEXR)
313 (opencvFlag "OPENJPEG" enableJPEG2000)
314 "-DWITH_JASPER=OFF" # OpenCV falls back to a vendored copy of Jasper when OpenJPEG is disabled
315 (opencvFlag "CUDA" enableCuda)
316 (opencvFlag "CUBLAS" enableCuda)
317 (opencvFlag "TBB" enableTbb)
318 ] ++ lib.optionals enableCuda [
319 "-DCUDA_FAST_MATH=ON"
320 "-DCUDA_HOST_COMPILER=${cudatoolkit.cc}/bin/cc"
321 "-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr"
322 "-DNVIDIA_OPTICAL_FLOW_2_0_HEADERS_PATH=${nvidia-optical-flow-sdk}"
323 ] ++ lib.optionals stdenv.isDarwin [
324 "-DWITH_OPENCL=OFF"
325 "-DWITH_LAPACK=OFF"
326 ] ++ lib.optionals (!stdenv.isDarwin) [
327 "-DOPENCL_LIBRARY=${ocl-icd}/lib/libOpenCL.so"
328 ] ++ lib.optionals enablePython [
329 "-DOPENCV_SKIP_PYTHON_LOADER=ON"
330 ];
331
332 postBuild = lib.optionalString enableDocs ''
333 make doxygen
334 '';
335
336 # By default $out/lib/pkgconfig/opencv4.pc looks something like this:
337 #
338 # prefix=/nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0
339 # exec_prefix=${prefix}
340 # libdir=${exec_prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/lib
341 # includedir_old=${prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/include/opencv4/opencv
342 # includedir_new=${prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/include/opencv4
343 # ...
344 # Libs: -L${exec_prefix}//nix/store/g0wnfyjjh4rikkvp22cpkh41naa43i4i-opencv-4.0.0/lib ...
345 # Note that ${exec_prefix} is set to $out but that $out is also appended to
346 # ${exec_prefix}. This causes linker errors in downstream packages so we strip
347 # of $out after the ${exec_prefix} and ${prefix} prefixes:
348 postInstall = ''
349 sed -i "s|{exec_prefix}/$out|{exec_prefix}|;s|{prefix}/$out|{prefix}|" \
350 "$out/lib/pkgconfig/opencv4.pc"
351 ''
352 # install python distribution information, so other packages can `import opencv`
353 + lib.optionalString enablePython ''
354 pushd $NIX_BUILD_TOP/$sourceRoot/modules/python/package
355 python -m pip wheel --verbose --no-index --no-deps --no-clean --no-build-isolation --wheel-dir dist .
356
357 pushd dist
358 python -m pip install ./*.whl --no-index --no-warn-script-location --prefix="$out" --no-cache
359
360 # the cv2/__init__.py just tries to check provide "nice user feedback" if the installation is bad
361 # however, this also causes infinite recursion when used by other packages
362 rm -r $out/${pythonPackages.python.sitePackages}/cv2
363
364 popd
365 popd
366 '';
367
368 passthru = lib.optionalAttrs enablePython { pythonPath = [ ]; };
369
370 meta = with lib; {
371 description = "Open Computer Vision Library with more than 500 algorithms";
372 homepage = "https://opencv.org/";
373 license = with licenses; if enableUnfree then unfree else bsd3;
374 maintainers = with maintainers; [ mdaiter basvandijk ];
375 platforms = with platforms; linux ++ darwin;
376 };
377}