|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# dlib wheel installer for macOS |
| 4 | +# downloads and installs the correct dlib wheel for your Mac |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +REPO_URL="https://github.com/comethrusws/Dlib_linux_python_3.x" |
| 9 | +VERSION="19.24" |
| 10 | + |
| 11 | +# colors |
| 12 | +RED='\033[0;31m' |
| 13 | +GREEN='\033[0;32m' |
| 14 | +YELLOW='\033[1;33m' |
| 15 | +BLUE='\033[0;34m' |
| 16 | +NC='\033[0m' |
| 17 | + |
| 18 | +# output helpers |
| 19 | +info() { echo -e "${BLUE}[info]${NC} $1"; } |
| 20 | +success() { echo -e "${GREEN}[success]${NC} $1"; } |
| 21 | +warn() { echo -e "${YELLOW}[warning]${NC} $1"; } |
| 22 | +error() { echo -e "${RED}[error]${NC} $1"; } |
| 23 | + |
| 24 | +check_macos() { |
| 25 | + if [[ "$(uname)" != "Darwin" ]]; then |
| 26 | + error "this script is for macOS only" |
| 27 | + info "for linux, use: scripts/install_wheel.sh" |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +detect_arch() { |
| 33 | + case $(uname -m) in |
| 34 | + x86_64) echo "x86_64" ;; |
| 35 | + arm64) echo "arm64" ;; |
| 36 | + *) error "unsupported architecture: $(uname -m)"; exit 1 ;; |
| 37 | + esac |
| 38 | +} |
| 39 | + |
| 40 | +detect_python() { |
| 41 | + if command -v python3 &>/dev/null; then |
| 42 | + python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" |
| 43 | + elif command -v python &>/dev/null; then |
| 44 | + python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" |
| 45 | + else |
| 46 | + error "python 3 not found"; exit 1 |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +check_pip() { |
| 51 | + if ! command -v pip &>/dev/null && ! command -v pip3 &>/dev/null; then |
| 52 | + error "pip not found"; exit 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +check_homebrew() { |
| 57 | + if ! command -v brew &>/dev/null; then |
| 58 | + warn "homebrew not found - you may need to install it for build dependencies" |
| 59 | + info "visit: https://brew.sh" |
| 60 | + return 1 |
| 61 | + fi |
| 62 | + return 0 |
| 63 | +} |
| 64 | + |
| 65 | +install_dependencies() { |
| 66 | + info "checking build dependencies..." |
| 67 | + if check_homebrew; then |
| 68 | + info "homebrew detected" |
| 69 | + |
| 70 | + missing_deps=() |
| 71 | + for dep in cmake boost; do |
| 72 | + if ! brew list $dep &>/dev/null; then |
| 73 | + missing_deps+=($dep) |
| 74 | + fi |
| 75 | + done |
| 76 | + |
| 77 | + if [ ${#missing_deps[@]} -gt 0 ]; then |
| 78 | + warn "recommended dependencies not installed: ${missing_deps[*]}" |
| 79 | + echo "install with: brew install ${missing_deps[*]}" |
| 80 | + else |
| 81 | + success "all dependencies installed" |
| 82 | + fi |
| 83 | + fi |
| 84 | +} |
| 85 | + |
| 86 | +install_wheel() { |
| 87 | + local arch=$1 |
| 88 | + local pyver=$2 |
| 89 | + local tag="cp${pyver//.}" |
| 90 | + |
| 91 | + if [ "$arch" = "x86_64" ]; then |
| 92 | + platform="macosx_10_9_x86_64" |
| 93 | + else |
| 94 | + platform="macosx_11_0_arm64" |
| 95 | + fi |
| 96 | + |
| 97 | + local wheel="dlib-${VERSION}-${tag}-${tag}-${platform}.whl" |
| 98 | + |
| 99 | + local url="$REPO_URL/releases/download/v${VERSION}/${wheel}" |
| 100 | + if curl -s --head "$url" | head -n1 | grep -q "200 OK"; then |
| 101 | + info "downloading wheel from releases" |
| 102 | + curl -L -o "$wheel" "$url" |
| 103 | + else |
| 104 | + url="$REPO_URL/raw/main/$wheel" |
| 105 | + if curl -s --head "$url" | head -n1 | grep -q "200 OK"; then |
| 106 | + info "downloading wheel from repo root" |
| 107 | + curl -L -o "$wheel" "$url" |
| 108 | + else |
| 109 | + error "wheel not found for python $pyver on macOS $arch" |
| 110 | + info "you can build from source with: pip install dlib" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + fi |
| 114 | + |
| 115 | + info "installing wheel..." |
| 116 | + if command -v pip3 &>/dev/null; then |
| 117 | + pip3 install "$wheel" |
| 118 | + else |
| 119 | + pip install "$wheel" |
| 120 | + fi |
| 121 | + |
| 122 | + rm -f "$wheel" |
| 123 | + success "dlib installed" |
| 124 | +} |
| 125 | + |
| 126 | +verify_install() { |
| 127 | + info "verifying installation..." |
| 128 | + if python3 -c "import dlib; dlib.get_frontal_face_detector()" &>/dev/null || python -c "import dlib; dlib.get_frontal_face_detector()" &>/dev/null; then |
| 129 | + success "installation verified" |
| 130 | + else |
| 131 | + warn "verification failed" |
| 132 | + fi |
| 133 | +} |
| 134 | + |
| 135 | +main() { |
| 136 | + info "dlib wheel installer for macOS" |
| 137 | + |
| 138 | + check_macos |
| 139 | + |
| 140 | + local arch=$(detect_arch) |
| 141 | + local pyver=$(detect_python) |
| 142 | + info "architecture: $arch" |
| 143 | + info "python version: $pyver" |
| 144 | + |
| 145 | + if [ "$arch" = "x86_64" ]; then |
| 146 | + info "detected: Intel Mac" |
| 147 | + else |
| 148 | + info "detected: Apple Silicon Mac" |
| 149 | + fi |
| 150 | + |
| 151 | + check_pip |
| 152 | + install_dependencies |
| 153 | + install_wheel "$arch" "$pyver" |
| 154 | + verify_install |
| 155 | + |
| 156 | + success "installation complete" |
| 157 | +} |
| 158 | + |
| 159 | +main "$@" |
0 commit comments