about summary refs log tree commit diff
path: root/pkgs/by-name/ba/backgroundremover/package.nix
blob: e6303bb377b7bf09b8c94b5cfae047cc3fdacb7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{ python3
, lib
, runCommand
, fetchFromGitHub
, fetchurl
}:

let
  p = python3.pkgs;
  self = p.buildPythonApplication rec {
    pname = "backgroundremover";
    version = "0.2.6";
    pyproject = true;

    src = fetchFromGitHub {
      owner = "nadermx";
      repo = "backgroundremover";
      rev = "v${version}";
      hash = "sha256-dDOo7NPwvdfV+ae2oMUytCGC+2HF6xUI7dyKk2we23w=";
    };

    models = runCommand "background-remover-models" {} ''
      mkdir $out
      cat ${src}/models/u2a{a,b,c,d} > $out/u2net.pth
      cat ${src}/models/u2ha{a,b,c,d} > $out/u2net_human_seg.pth
      cp ${src}/models/u2netp.pth $out
    '';

    postPatch = ''
      substituteInPlace backgroundremover/bg.py backgroundremover/u2net/detect.py \
        --replace 'os.path.expanduser(os.path.join("~", ".u2net", model_name + ".pth"))' "os.path.join(\"$models\", model_name + \".pth\")"
    '';

    nativeBuildInputs = [ p.setuptools p.wheel ];

    propagatedBuildInputs = [
      p.certifi
      p.charset-normalizer
      p.ffmpeg-python
      p.filelock
      p.filetype
      p.hsh
      p.idna
      p.more-itertools
      p.moviepy
      p.numpy
      p.pillow
      p.pymatting
      p.pysocks
      p.requests
      p.scikit-image
      p.scipy
      p.six
      p.torch
      p.torchvision
      p.tqdm
      p.urllib3
      p.waitress
    ];

    pythonImportsCheck = [ "backgroundremover" ];

    passthru = {
      inherit models;
      tests = {
        image = let
          # random no copyright car image from the internet
          demoImage = fetchurl {
            url = "https://pics.craiyon.com/2023-07-16/38653769ac3b4e068181cb5ab1e542a1.webp";
            hash = "sha256-Kvd06eZdibgDbabVVe0+cNTeS1rDnMXIZZpPlHIlfBo=";
          };
        in runCommand "backgroundremover-image-test.png" {
          buildInputs = [ self ];
        } ''
          export NUMBA_CACHE_DIR=$(mktemp -d)
          backgroundremover -i ${demoImage} -o $out
        '';
      };
    };

    doCheck = false; # no tests

    meta = with lib; {
      mainProgram = "backgroundremover";
      description = "Command line tool to remove background from image and video, made by nadermx to power";
      homepage = "https://BackgroundRemoverAI.com";
      downloadPage = "https://github.com/nadermx/backgroundremover/releases";
      license = licenses.mit;
      maintainers = [ maintainers.lucasew ];
    };
  };
in self