This is how I do it with Git v2.25.0, and it was also tested with v2.26.2. This trick doesn't work with v2.30.1.
TLDR
git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv
# Requires Git 2.25.x to 2.26.2
git sparse-checkout set data/haarcascades
You can use Docker to avoid installing a specific version of Git:
git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv
# Requires Git 2.25.x to 2.26.2
docker run --rm -it -v $PWD/:/code/ --workdir=/code/ alpine/git:v2.26.2 sparse-checkout set data/haarcascades
Full solution
# Bare minimum clone of OpenCV
git clone --no-checkout --filter=tree:0 https://github.com/opencv/
Output:
opencv
...
Resolving deltas: 100% (529/529), done.
# Downloaded only ~7.3MB , takes ~3 seconds
# du = disk usage, -s = summary, -h = human-readable
And:
du -sh opencv
Output:
7.3M opencv/
And:
# Set target directory
cd opencv
git sparse-checkout set data/haarcascades
Output:
...
Updating files: 100% (17/17), done.
# Takes ~10 seconds, depending on your specifications
And:
# View downloaded files
du -sh data/haarcascades/
Output:
9.4M data/haarcascades/
And:
ls data/haarcascades/
Output:
haarcascade_eye.xml haarcascade_frontalface_alt2.xml haarcascade_licence_plate_rus_16stages.xml haarcascade_smile.xml
haarcascade_eye_tree_eyeglasses.xml haarcascade_frontalface_alt_tree.xml haarcascade_lowerbody.xml haarcascade_upperbody.xml
haarcascade_frontalcatface.xml haarcascade_frontalface_default.xml haarcascade_profileface.xml
haarcascade_frontalcatface_extended.xml haarcascade_fullbody.xml haarcascade_righteye_2splits.xml
haarcascade_frontalface_alt.xml haarcascade_lefteye_2splits.xml haarcascade_russian_plate_number.xml
References