at 25.11-pre 3.4 kB view raw
1import ./make-test-python.nix ( 2 { pkgs, ... }: 3 /* 4 SANE NixOS test 5 =============== 6 SANE is intrisically tied to hardware, so testing it is not straightforward. 7 However: 8 - a fake webcam can be created with v4l2loopback 9 - sane has a backend (v4l) to use a webcam as a scanner 10 This test creates a webcam /dev/video0, streams a still image with some text 11 through this webcam, uses SANE to scan from the webcam, and uses OCR to check 12 that the expected text was scanned. 13 */ 14 let 15 text = "66263666188646651519653683416"; 16 fontsConf = pkgs.makeFontsConf { 17 fontDirectories = [ 18 pkgs.dejavu_fonts.minimal 19 ]; 20 }; 21 # an image with black on white text spelling "${text}" 22 # for some reason, the test fails if it's jpg instead of png 23 # the font is quite large to make OCR easier 24 image = 25 pkgs.runCommand "image.png" 26 { 27 # only imagemagickBig can render text 28 nativeBuildInputs = [ pkgs.imagemagickBig ]; 29 FONTCONFIG_FILE = fontsConf; 30 } 31 '' 32 magick -pointsize 100 label:${text} $out 33 ''; 34 in 35 { 36 name = "sane"; 37 nodes.machine = 38 { pkgs, config, ... }: 39 { 40 boot = { 41 # create /dev/video0 as a fake webcam whose content is filled by ffmpeg 42 extraModprobeConfig = '' 43 options v4l2loopback devices=1 max_buffers=2 exclusive_caps=1 card_label=VirtualCam 44 ''; 45 kernelModules = [ "v4l2loopback" ]; 46 extraModulePackages = [ config.boot.kernelPackages.v4l2loopback ]; 47 }; 48 systemd.services.fake-webcam = { 49 wantedBy = [ "multi-user.target" ]; 50 description = "fill /dev/video0 with ${image}"; 51 /* 52 HACK: /dev/video0 is a v4l2 only device, it misses one single v4l1 53 ioctl, VIDIOCSPICT. But sane only supports v4l1, so it will log that this 54 ioctl failed, and assume that the pixel format is Y8 (gray). So we tell 55 ffmpeg to produce this pixel format. 56 */ 57 serviceConfig.ExecStart = [ 58 "${pkgs.ffmpeg}/bin/ffmpeg -framerate 30 -re -stream_loop -1 -i ${image} -f v4l2 -pix_fmt gray /dev/video0" 59 ]; 60 }; 61 hardware.sane.enable = true; 62 system.extraDependencies = [ image ]; 63 environment.systemPackages = [ 64 pkgs.fswebcam 65 pkgs.tesseract 66 pkgs.v4l-utils 67 ]; 68 environment.variables.SANE_DEBUG_V4L = "128"; 69 }; 70 testScript = '' 71 start_all() 72 machine.wait_for_unit("fake-webcam.service") 73 74 # the device only appears when ffmpeg starts producing frames 75 machine.wait_until_succeeds("scanimage -L | grep /dev/video0") 76 77 machine.succeed("scanimage -L >&2") 78 79 with subtest("debugging: /dev/video0 works"): 80 machine.succeed("v4l2-ctl --all >&2") 81 machine.succeed("fswebcam --no-banner /tmp/webcam.jpg") 82 machine.copy_from_vm("/tmp/webcam.jpg", "webcam") 83 84 # scan with the webcam 85 machine.succeed("scanimage -o /tmp/scan.png >&2") 86 machine.copy_from_vm("/tmp/scan.png", "scan") 87 88 # the image should contain "${text}" 89 output = machine.succeed("tesseract /tmp/scan.png -") 90 print(output) 91 assert "${text}" in output, f"expected text ${text} was not found, OCR found {output!r}" 92 ''; 93 } 94)