好记性不如烂笔头,放到网上才安全
1. 首先安装cmake
1.1 去cmake官网下载最新版的软件,然后上传到centos
1.2 解压安装包
tar zxvf cmake-3.17.0-rc2.tar.gz
1.3 编译安装
cd cmake-3.17.0-rc2
./bootstrap
make && make install
1.4 输入命令测验
hash -r
cmake --version
2 安装opencv
2.1 从github中下载OpenCV + OpenCV_Contrib库(注意版本对应),也可以通过opencv官网下载(建议使用):
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git
git clone https://github.com/Itseez/opencv_contrib.git
然后分别解压缩
unzip opencv_contrib-4.2.0.zip
unzip opencv-4.2.0.zip
2.2 开始编译安装
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=/dgm/ml/opencv_contrib-4.2.0/modules ..
make #此阶段很慢
make install
3. 安装python工具包 opencv-python
pip3 install opencv-python
测试:
python3
import cv2
print(cv2.__version__)
4. 测试头像
原图片,编写代码(也可保存为py文件)并执行
#!/usr/bin/python3
import cv2
# loading the test image
image = cv2.imread("dongguangming.jpg")
# converting to grayscale
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# initialize the face recognizer (default face haar cascade)
#face_cascade = cv2.CascadeClassifier("cascades/haarcascade_fontalface_default.xml")
face_cascade=cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
face_cascade.load('/dgm/ml/opencv-4.2.0/data/haarcascades/haarcascade_frontalface_default.xml')
# detect all the faces in the image
faces = face_cascade.detectMultiScale(image_gray, 1.3, 5)
# print the number of faces detected
print(f"{len(faces)} faces detected in the image.")
# for every face, draw a blue rectangle
for x, y, width, height in faces:
cv2.rectangle(image, (x, y), (x + width, y + height), color=(255, 0, 0), thickness=2)
# save the image with rectangles
cv2.imwrite("dongguangming_face_detected.jpg", image)
若保存的文件名为face_detection.py, 那么 python3 face_detection.py
,执行结果
又如》
等等 ,案例太多。
安装时出现的问题与解答(question and answer):
1. 安装cmake出现问题
是升级openssl引起的,网上搜索解决
2. 出现c++: internal compiler error: Killed (program cc1plus)问题时,请参考内存不足
3. 出现
~/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory现象时,请先下载https://files.cnblogs.com/files/arxive/boostdesc_bgm.i%2Cvgg_generated_48.i%E7%AD%89.rar,解压后上传到对应的目录下
4 。当出现类似于XXX.hpp 没有那个文件或者目录的问题时,
请
cd /dgm/ml/opencv-4.2.0/modules/features2d/test #目录依实际而定
cp -f test_detectors_regression.impl.hpp /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
cp -f test_detectors_regression.hpp /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
cp -f test_detectors_regression.cpp /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
cp -f test_descriptors_* /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
cp -f test_detectors_invariance* /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
cp -f test_utils.cpp /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
cp -f test_invariance_utils.hpp /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test
然后分别编辑文件
vi /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test/test_features2d.cpp
vi /dgm/ml/opencv_contrib-4.2.0/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp
去掉features2d/test/,只剩下文件名
告一段落,收工!!!