Python OpenCV Face Detection on OSX
This is a short post mainly for my reference on how to run OpenCV on OSX to detect faces from a Logitech HD Pro Webcam C920. Note, the below assumes Python 3.7 is already installed.
The first step is to install the xcode command line develop tools, ffmpeg, and cmake.
xcode-select –install
brew install ffmpeg
brew install cmake
Next, install numpy using pip.
pip3.7 install numpy
Pull down the OpenCV source from GitHub.
git clone https://github.com/opencv/opencv.git
Finally, build and install from source.
cd opencv
mkdir build
cd build
cmake -DPYTHON_DEFAULT_EXECUTABLE=python3.7 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_opencv_python2=False -DBUILD_opencv_python3=True -DPYTHON3_EXECUTABLE=python3.7 –FFMPEG_INCLUDE_DIR=/usr/local/Cellar/ffmpeg/4.1.3_1/include/ FFMPEG_LIB_DIR=/usr/local/Cellar/ffmpeg/4.1.3_1/lib/ ../
make -j2
sudo make install
cat /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/opencv3.pth
Confirm OpenCV is installed successfully and print the version.
python3.7 -c “import cv2; print(cv2.__version__)”
Finally the code (for reference, GitHub repository is here)
|
|
References
- Face Detection using Haar Cascades
- Flags for video IO - setting the camera resolution
- Getting Started with Videos