0 背景与准备
因为要做人脸识别的项目,所以需要配置相对应的环境和库,常用深度学习的数值计算库为tensorflow、pytorch ,常用的人脸识别库为face_recognition ,常用的图像处理库opencv 。
下图为使用库识别后的效果。
因为本地环境为Mac,服务器环境为Linux,为了方便调试和部署,于是在本地和远程linux上都配置了相同的人脸识别环境。
安装这三个常用人脸识别的库都需要安装anaconda,都需要在Anaconda的虚拟环境中进行安装。
Mac安装的方法为,直接到Anaconda官方下载链接,下载安装后即可。
Linux下安装Anaconda的方法为:
wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
bash Anaconda3-5.3.1-Linux-x86_64.sh
source ~/.bashrc
创建Anaconda虚拟环境的方法如下:
conda create --name tensorflow-env python=3.6
source activate tensorflow-env
source deactivate tensorflow-env
使用Pycharm创建Anaconda环境方法如下:
添加安装源:
conda config --add channels https://pypi.tuna.tsinghua.edu.cn/simple
conda config --set show_channel_urls yes
conda config --show channels
vi ~/.condarc
执行,将进入`condarc`这个文件,按下`‘i’`进入`‘Insert’`模式将`‘- defaults’`这一行删除.
1 tensorflow环境【数值计算的开源软件库】
首先创建python版本3.6的Anaconda的虚拟环境。
在命令端执行如下执行,如果是Pycharm,则在Teriminal中执行指令。
conda install tensorflow
如果上面的方法安装不成功,则更换安装源(安装源可自行选择)进行安装:
pip --default-timeout=100 install tensorflow==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
测试代码:
import tensorflow as tf
x=tf.constant(3)
y=tf.constant(2)
z=x+y
sess=tf.Session()
print(sess.run(z))
print(tf.__version__)
安装的是tensorflow2,但是却想使用tensorflow1中的方法,可以使用如下的方法。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
2 pytorch环境【数值计算的开源软件库】
首先创建python版本3.7的Anaconda的虚拟环境。
执行指令如下:
conda install torch
如果上面的方法安装不成功,则更换安装源进行安装:
pip --default-timeout=100 install torch -i https://pypi.tuna.tsinghua.edu.cn/simple
其他常用的图像处理库:
conda install PIL
conda install numpy
测试代码:
import torch
x = torch.rand(5, 3)
print(x)
print(torch.__version__)
3 face_recognition【基于dlib的人脸识别库】
首先创建python版本3.7的Anaconda的虚拟环境。
执行如下指令:
pip --default-timeout=100 install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip --default-timeout=100 install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
4 opencv【图像处理库】
4.1 安装opencv库
Mac的安装opencv的方法为:使用brew进行opencv的安装:
brew install opencv
Linux安装opencv,
可以使用如下指令直接安装,但是安装的版本比较老,不推荐。
yum install opencv opencv-devel opencv-python -y
pkg-config --modversion opencv
这里使用下面👇的方法进行安装,
首先安装cmake3,用于编译opencv源代码,方法如下:
yum install openssl openssl-devel
[root@localhost ~]
cmake version 2.8.12.2
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost cmake]
[root@localhost cmake]
[root@localhost cmake]
[root@localhost cmake]
[root@localhost cmake-3.16.6]
[root@localhost cmake-3.16.6]
[root@localhost cmake-3.16.6]
cmake version 3.16.6
CMake suite maintained and supported by Kitware (kitware.com/cmake).
安装opencv的方法如下:
wget https://github.com/opencv/opencv/archive/4.1.2.zip
wget https://github.com/opencv/opencv_contrib/archive/4.1.2.zip
unzip 4.1.2.zip
cd opencv4.1.2
mkdir build
cd build
cmake -D WITH_TBB=ON -D WITH_EIGEN=ON -D OPENCV_GENERATE_PKGCONFIG=ON ..
cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF ..
cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make install
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
source /etc/profile
pkg-config --libs opencv
pkg-config --libs opencv4
pkg-config opencv --modversion
opencv_version
vi /etc/profile
source activate tensorflow-env
pip install opencv-python
部分安装截图如下:
构建截图:
编译截图: 安装截图 使用python命令端,查看是否安装成功以及安装版本:
测试C++ opencv版本可用,在文件夹中创建如下文件:
qr.cpp 文件(作用:框出图片中二维码的部分):
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/imgproc/types_c.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;Mat src_out;
RNG rng(12345);
Point Center_point(vector<vector<Point> > contours,int i)
{
int centerx=0,centery=0,n=contours[i].size();
centerx = (contours[i][n/4].x + contours[i][n*2/4].x + contours[i][3*n/4].x + contours[i][n-1].x)/4;
centery = (contours[i][n/4].y + contours[i][n*2/4].y + contours[i][3*n/4].y + contours[i][n-1].y)/4;
Point point1=Point(centerx,centery);
return point1;
}
int main( int argc, char** argv[] )
{
src = imread( "1.jpg", 1 );
if(src.empty())
{
fprintf(stderr, "Can not load image %s\n", 1);
return -1;
}
Mat src_all=src.clone();
cvtColor( src, src_gray, CV_BGR2GRAY );
Scalar color = Scalar(1,1,255 );
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
Mat drawing2 = Mat::zeros( src.size(), CV_8UC3 );
Mat src_threshold;
threshold(src_gray, src_threshold, 112, 255, THRESH_BINARY );
vector<vector<Point> > contours,contours2;
vector<Vec4i> hierarchy;
findContours( src_threshold, contours, hierarchy, CV_RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0) );
int c=0,ic=0,k=0,area=0,i=0;
int parentIdx=-1;
for( int i = 0; i< contours.size(); i++ )
{
if (hierarchy[i][2] != -1 && ic==0)
{
parentIdx = i;
ic++;
}
else if (hierarchy[i][2] != -1)
{
ic++;
}
else if(hierarchy[i][2] == -1)
{
ic = 0;
parentIdx = -1;
}
if ( ic >= 2)
{
contours2.push_back(contours[parentIdx]);
drawContours( drawing, contours, parentIdx, CV_RGB(255,0,0) , 1, 8);
ic = 0;
parentIdx = -1;
area = contourArea(contours[i]);
}
}
for(int i=0; i<contours2.size(); i++)
drawContours( drawing2, contours2, i, CV_RGB(255,0,0) , -1, 4, hierarchy[k][2], 0, Point() );
Point point[3];
for(int i=0; i<contours2.size(); i++)
{
point[i] = Center_point( contours2, i );
}
area = contourArea(contours2[0]);
int area_side = cvRound (sqrt (double(area)));
for(int i=0; i<contours2.size(); i++)
{
line(drawing2,point[i%contours2.size()],point[(i+1)%contours2.size()],CV_RGB(100,100,255),area_side/2,8);
}
Mat gray_all,src_treshold_all;
vector<vector<Point> > contours_all;
vector<Vec4i> hierarchy_all;
cvtColor( drawing2, gray_all, CV_BGR2GRAY );
threshold( gray_all, src_treshold_all, 45, 255, THRESH_BINARY );
findContours( src_treshold_all, contours_all, hierarchy_all, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point(0, 0) );
RotatedRect rectPoint = minAreaRect(contours_all[0]);
Point2f fourPoint2f[4];
rectPoint.points(fourPoint2f);
for (int i = 0; i < 4; i++)
{
line(src_all, fourPoint2f[i%4], fourPoint2f[(i + 1)%4], Scalar(20,21,237), 3);
}
imshow( "二维码", src_all );
waitKey(0);
return(0);
}
CMakeLists.txt 文件
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
project(QR)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS})
add_executable(qr qr.cpp)
target_link_libraries(qr ${OpenCV_LIBS})
编译执行:
cmake .
make
./qr 1.jpg
# 执行结果(没有界面的linux)
(二维码:22837): Gtk-WARNING **: 18:02:43.491: cannot open display:
附带卸载opencv的方法:
rm -rf /usr/local/share/licenses/opencv4/
rm -rf /usr/local/include/opencv4/
rm -rf /usr/local/lib64/cmake/opencv4/
rm -rf /usr/local/lib64/libopencv_core.so.4.1.2
rm -rf /usr/local/lib64/libopencv_core.so.4.1
rm -rf /usr/local/lib64/libopencv_core.so
rm -rf /usr/local/bin/setup_vars_opencv4.sh
rm -rf /usr/local/share/opencv4/valgrind.supp
rm -rf /usr/local/bin/setup_vars_opencv4.sh
rm -rf /usr/local/share/opencv4/valgrind.supp
rm -rf /usr/local/share/licenses/opencv4/
删除下面路径下的所有opencv库
cd /usr/local/lib64/
shopt -s extglob
rm -fr !(cmake)
cd /root/opencv-4.1.2/build/
sudo make uninstall
cd ..
sudo rm -r build
4.2 Pycharm配置环境
pycharm配置opencv环境:
import sys
sys.path.append("/usr/local/Cellar/opencv/4.3.0_5/")
- 2 )打开pycharm的设置,点击加号?,在里面搜索
opencv ,然后执行安装。
如果搜不到opencv-py 库,可以点击Manage Repositories ,添加安装源: 或者在pycharm的命令端添加,执行如下指令:
% conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
% conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
% conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
% conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/
conda config --add channels https://pypi.tuna.tsinghua.edu.cn/simple
% conda config --set show_channel_urls yes
5 参考博文
ccw1078的博客
吕振江的博客
bxzsy的博客
陈Cocke
HeyMountain
|