nixpkgs/pkgs/development/python-modules/invisible-watermark/default.nix
Luflosi 04120a3965
python3Packages.invisible-watermark: add tests
After the 0.2.0 version, there are even more possible cases to consider.
I found it too annoying to do all the testing manually.
Add some tests to make it easy to test everything automatically.

The test python3Packages.invisible-watermark.tests.withOnnx-rivaGan would fail in the nix sandbox on aarch64-linux:
```
Error in cpuinfo: failed to parse the list of possible processors in /sys/devices/system/cpu/possible
Error in cpuinfo: failed to parse the list of present processors in /sys/devices/system/cpu/present
Error in cpuinfo: failed to parse both lists of possible and present processors
terminate called after throwing an instance of 'onnxruntime::OnnxRuntimeException'
  what():  /build/source/include/onnxruntime/core/common/logging/logging.h:294 static const onnxruntime::logging::Logger& onnxruntime::logging::LoggingManager::DefaultLogger() Attempt to use DefaultLogger but none has been registered.

/build/.attr-0l2nkwhif96f51f4amnlf414lhl4rv9vh8iffyp431v6s28gsr90: line 9:     5 Aborted                 (core dumped) invisible-watermark --verbose --action encode --type bytes --method 'rivaGan' --watermark 'asdf' --output output.png '/nix/store/srl698a32n9d2pmyf5zqfk65gjzq3mhp-source/test_vectors/original.jpg'
Exit code of invisible-watermark was 134 while 0 was expected.
```
so I have disabled that test. I believe https://github.com/microsoft/onnxruntime/issues/10038 describes the same issue.
2023-10-27 16:56:36 +02:00

77 lines
2.3 KiB
Nix

{ lib
, stdenv
, buildPythonPackage
, pythonOlder
, fetchFromGitHub
, opencv4
, torch
, onnx
, onnxruntime
, pillow
, pywavelets
, numpy
, callPackage
, withOnnx ? false # Enables the rivaGan en- and decoding method
}:
buildPythonPackage rec {
pname = "invisible-watermark";
version = "0.2.0";
format = "setuptools";
disabled = pythonOlder "3.6";
src = fetchFromGitHub {
owner = "ShieldMnt";
repo = "invisible-watermark";
rev = "e58e451cff7e092457cd915e445b1a20b64a7c8f"; # No git tag, see https://github.com/ShieldMnt/invisible-watermark/issues/22
hash = "sha256-6SjVpKFtiiLLU7tZ3hBQr0KT/YEQyywJj0e21/dJRzk=";
};
propagatedBuildInputs = [
opencv4
torch
pillow
pywavelets
numpy
] ++ lib.optionals withOnnx [
onnx
onnxruntime
];
postPatch = ''
substituteInPlace setup.py \
--replace 'opencv-python>=4.1.0.25' 'opencv'
substituteInPlace imwatermark/rivaGan.py --replace \
'You can install it with pip: `pip install onnxruntime`.' \
'You can install it with an override: `python3Packages.invisible-watermark.override { withOnnx = true; };`.'
'';
passthru.tests = let
image = "${src}/test_vectors/original.jpg";
methods = [ "dwtDct" "dwtDctSvd" "rivaGan" ];
testCases = builtins.concatMap (method: [
{ method = method; withOnnx = true; }
{ method = method; withOnnx = false; }
]) methods;
createTest = { method, withOnnx }: let
testName = "${if withOnnx then "withOnnx" else "withoutOnnx"}-${method}";
# This test fails in the sandbox on aarch64-linux, see https://github.com/microsoft/onnxruntime/issues/10038
skipTest = stdenv.isLinux && stdenv.isAarch64 && withOnnx && method == "rivaGan";
in lib.optionalAttrs (!skipTest) {
"${testName}" = callPackage ./tests/cli.nix { inherit image method testName withOnnx; };
};
allTests = builtins.map createTest testCases;
in (lib.attrsets.mergeAttrsList allTests) // {
python = callPackage ./tests/python { inherit image; };
};
pythonImportsCheck = [ "imwatermark" ];
meta = with lib; {
description = "A library for creating and decoding invisible image watermarks";
homepage = "https://github.com/ShieldMnt/invisible-watermark";
license = licenses.mit;
maintainers = with maintainers; [ Luflosi ];
};
}