Skip to content

Instantly share code, notes, and snippets.

@a-voronov
Last active August 27, 2021 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-voronov/72644da2b3e32cfcc241994d6059f772 to your computer and use it in GitHub Desktop.
Save a-voronov/72644da2b3e32cfcc241994d6059f772 to your computer and use it in GitHub Desktop.
OpenCV Courses - Jupyter + Python + dlib on Big Sur M1 x86_64
appnope==0.1.2
argon2-cffi==20.1.0
attrs==21.2.0
backcall==0.2.0
bleach==4.0.0
cffi==1.14.6
cycler==0.10.0
debugpy==1.4.1
decorator==5.0.9
defusedxml==0.7.1
entrypoints==0.3
imageio==2.9.0
imutils==0.5.4
ipykernel==6.2.0
ipython==7.26.0
ipython-genutils==0.2.0
ipywidgets==7.6.3
jedi==0.18.0
Jinja2==3.0.1
joblib==1.0.1
jsonschema==3.2.0
jupyter==1.0.0
jupyter-client==7.0.1
jupyter-console==6.4.0
jupyter-core==4.7.1
jupyterlab-pygments==0.1.2
jupyterlab-widgets==1.0.0
kiwisolver==1.3.1
MarkupSafe==2.0.1
matplotlib==3.4.3
matplotlib-inline==0.1.2
mistune==0.8.4
nbclient==0.5.4
nbconvert==6.1.0
nbformat==5.1.3
nest-asyncio==1.5.1
networkx==2.6.2
notebook==6.4.3
numpy==1.21.2
opencv-contrib-python==4.5.3.56
packaging==21.0
pandocfilters==1.4.3
parso==0.8.2
pexpect==4.8.0
pickleshare==0.7.5
Pillow==8.3.1
pip==21.2.4
prometheus-client==0.11.0
prompt-toolkit==3.0.20
ptyprocess==0.7.0
pycparser==2.20
Pygments==2.10.0
pyobjc-core==7.3
pyobjc-framework-Cocoa==7.3
pyparsing==2.4.7
pyrsistent==0.18.0
python-dateutil==2.8.2
PyWavelets==1.1.1
pyzmq==22.2.1
qtconsole==5.1.1
QtPy==1.10.0
scikit-image==0.18.2
scikit-learn==0.24.2
scipy==1.7.1
Send2Trash==1.8.0
setuptools==57.2.0.post0
six==1.16.0
terminado==0.11.1
testpath==0.5.0
threadpoolctl==2.2.0
tifffile==2021.8.8
tornado==6.1
traitlets==5.0.5
wcwidth==0.2.5
webencodings==0.5.1
wheel==0.37.0
widgetsnbextension==3.5.1
with import <nixpkgs> { localSystem = "x86_64-darwin"; };
let
pythonPackages = python38Packages;
# dlib fails to import on arm64 python, temporarily using x86_64: https://github.com/davisking/dlib/issues/2268
# original one doesn't work either: https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/libraries/dlib/default.nix
# hence compiling it ourselves with correct build flags to work on M1
dlib = python38Packages.buildPythonPackage rec {
pname = "dlib";
version = "19.22";
src = fetchFromGitHub {
owner = "davisking";
repo = "dlib";
rev ="v${version}";
sha256 = "sha256-cEbw01w4KgSG3JTvTE/qruo7i4/L++m02HW+0VNmSSQ=";
};
setupPyBuildFlags = [
# for some reason, with blas and laplack flags on,
# I'm getting `** On entry to DGESVD parameter number 13 had an illegal value` error
# however I'm keeping openblas as dependency, because even with the use-blas set to NO,
# I'm getting `No BLAS library found so using dlib's built in BLAS` without it.
"--set DLIB_USE_BLAS=NO"
"--set DLIB_USE_LAPACK=NO"
"--set DLIB_NO_GUI_SUPPORT=ON"
"--set BUILD_SHARED_LIBS=ON"
"--set USE_AVX_INSTRUCTIONS=no"
"--set USE_SSE4_INSTRUCTIONS=yes"
"--set USE_DLIB_USE_CUDA=0"
];
dontUseCmakeConfigure = true;
doCheck = false;
propagatedBuildInputs = [ pythonPackages.setuptools ];
buildInputs = [ libpng libjpeg openblas ];
nativeBuildInputs = [ cmake pkg-config ];
};
in pkgs.mkShell rec {
name = "opencvCoursesPythonEnv";
venvDir = "./.venv";
buildInputs = [
# A Python interpreter including the 'venv' module is required to bootstrap the environment.
pythonPackages.python
# This execute some shell code to initialize a venv in $venvDir before
# dropping into the shell
pythonPackages.venvShellHook
];
packages = [
dlib
];
# Run this command, only after creating the virtual environment
postVenvCreation = ''
unset SOURCE_DATE_EPOCH
python -m pip install --upgrade pip
python -m pip install -r requirements-x86_64.txt
# Fixing iPython Kernel + Jupyter on Apple M1 Big Sur ...
# https://github.com/minrk/appnope/issues/12#issuecomment-741625144
ipythonConfDir=~/.ipython/profile_default
ipythonConf=~/.ipython/profile_default/ipython_kernel_config.py
ipythonFix="c.Kernel._darwin_app_nap = False"
mkdir -p $ipythonConfDir
if ! grep -q "$ipythonFix" "$ipythonConf"; then
echo "$ipythonFix" >> $ipythonConf
fi
unset ipythonConfDir
unset ipythonConf
unset ipythonFix
'';
# For the first-time use, run these commands if there's no requirements-x86_64.txt
# python -m pip install pyobjc-framework-Cocoa==7.3
# python -m pip install wheel numpy scipy matplotlib scikit-image scikit-learn ipython opencv-contrib-python jupyter setuptools dlib imutils
#
# Freeze requirements using format from list to avoid nix paths https://stackoverflow.com/a/62886215/1376429
# pip list --format=freeze > requirements-x86_64.txt
# Now we can execute any commands within the virtual environment.
# This is optional and can be left out to run pip manually.
postShellHook = ''
# allow pip to install wheels
unset SOURCE_DATE_EPOCH
'';
}
# There's a known issue on macOS Big Sur caused by send2trash package when running `jupyter notebook`:
# > AttributeError: dlsym(RTLD_DEFAULT, GetMacOSStatusCommentString): symbol not found
#
# These are the current hotfixes:
# https://github.com/arsenetar/send2trash/issues/51#issuecomment-858111428
# https://github.com/jupyter/notebook/pull/6016#issuecomment-858164229
# TL;DR: make sure to include `pyobjc-framework-Cocoa==7.3 send2trash==1.7.1` into requirements.txt
# Also, consider trying jupyterWith from tweag (but it has same issue):
# https://github.com/tweag/jupyterWith/issues/144
# dlib fails to import on arm64 python, temporarily using x86_64: https://github.com/davisking/dlib/issues/2268
@a-voronov
Copy link
Author

Looks like there're issues when running Jupyter with Python on Big Sur and Apple M1 macs 😅

Here's my nix-shell to launch courses.opencv.org on my new mac.
It's also compatible with VS Code, just make sure to select ./.venv/bin/python as Python and Jupyter server Interpreters (cmd + shift + P).

@a-voronov
Copy link
Author

Added dlib - that was hell of a ride 😮‍💨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment