Mask Detection Snap for Ubuntu Core with Sox audio

Hi I am trying to build a snap which is using OpenCV and SoX to alert if masks are not worn properly. The code works fine on Ubuntu Desktop (not as a snap) and now I am trying to snap it for Ubuntu Core as this will be a Kiosk.

The vision part is mostly working now (after some headache) but it seems that the audio part which I thought will be simple is an even bigger headache. My basic premise is to use sox and play a simple wav file upon detection. I added the sox library to stage-packages and if I shell into the snap (snapcraft --shell-after --use-lxd) I can see the sox library and dependencies installed. However, when I ldd /root/prime/usr/bin/sox the library libsox3 is not found. (0x00007fffa8add000) => not found => /lib/x86_64-linux-gnu/ (0x00007f15ebd9b000) => /lib/x86_64-linux-gnu/ (0x00007f15ebb7c000) => /lib/x86_64-linux-gnu/ (0x00007f15eb78b000)
/lib64/ (0x00007f15ec139000)

I added a bunch of the dependencies to stage packages as well but that does not seem to help. However in the shell-after if I directly do sudo apt install libsox3 then it installs it and now ldd /root/prime/usr/bin/sox finds it. I tried to see if there is any weird installation triggers in libsox3 but did not see anything telling.

the final result of all of this is that when I actually deploy the snap on Core (as a daemon) I get video but when trying to play a sound I get

2021-05-09T06:50:04Z camsnap.camsnap[5157]: play FAIL formats: no handler for given file type alsa' 2021-05-09T06:50:15Z camsnap.camsnap[5157]: play FAIL formats: no handler for detected file type vorbis’

I get two messages as I tried both .wav and .ogg file. From the sox documentation it seems that libsox-fmt-base and libsox-fmt-alsa are key to be able to handle wav files. There is also libsox-fmt-all which I also tried including but did not help. My assumption so far is that there is something funky going on with the sox dependency libraries. Please suggest how can I further troubleshoot to solve this issue.

Finally I am also assuming that I can directly plug :alsa in my snap and don’t need pulseaudio for this but I still experimented with pulse nevertheless, however did not see any improvement.

Please see the majority of my snapcraft.yaml below. Really appreciate some advice on this as I have been digging through the web and trying all sorts of stuff but have not been able to solve this.

name: camsnap
base: core20
version: '1'  
summary: example camera X11 kiosk
description: example camera X11 kiosk
confinement: devmode
grade: devel

  PYTHONPATH: $SNAP/usr/lib/python3/dist-packages
  AUDIODEV: hw:0,0

    bind: $SNAP/usr/share/X11
    symlink: $SNAP/usr/bin/xkbcomp
    bind: $SNAP/usr/share/icons
    bind: $SNAP/usr/share/fonts
    bind: $SNAP/etc/fonts
    bind: $SNAP/usr/share/alsa

    daemon: simple
    restart-condition: always
      - env-setup
    command: usr/local/bin/x11_kiosk_launch $SNAP/run_maskdetector

      - opengl         # For Mir
      - wayland        # For Mir
      - network-bind   # For Mir (to serve X11)
      - camera		# for camera
      - home		# home directory
      - pulseaudio
      - alsa		

    plugin: python
    source: .
      - python3
      - mesa-utils
      - libice6
      - libsm6
      - libx11-6         
      - libxext6
      - libxrender1
      - libpsm-infinipath1
      - python3-opencv
      - alsa-base           #not sure if I need this or not
      - libgsm1              # seems to be required for libsox3
      - libltdl7	              # seems to be required for libsox3
      - libmagic-mgc    # seems to be required for libsox3
      - libmagic1	      # seems to be required for libsox3
      - libsox-fmt-base
      - libsox-fmt-alsa
      - libsox3
      - sox
#      - libpulse0     #for pulse audio

      - mir-kiosk-x11

    override-build: |
      snapcraftctl build
      ln -sf ../libpsm1/  $SNAPCRAFT_PART_INSTALL/usr/lib/x86_64-linux-gnu/
1 Like

if you paste a snapcraft.yaml please add three backticks ``` above and below the pasted file, that way the indendation stays intact … yaml is really hard to read without this … (you can edit the post, add the backticks and re-paste the snapcraft.yaml)

1 Like

Thank you Ogra for the direction and apologies for not knowing the etiquette (first snap for me and first post in the forum). I have updated the original post. Thank you so much for your advice on how to fix this problem.

I was able to resolve this issue by adding the following to my snapcraft.yaml

    bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/sox