dlib note

This commit is contained in:
light 2018-02-25 01:24:12 +08:00
parent 4385b63c3b
commit 6360db8133
15 changed files with 613 additions and 5 deletions

36
dlib/install.md Normal file
View File

@ -0,0 +1,36 @@
# dlib安装
## 安装编译源码
``` bash
git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build . --config Release
# 安装python包
cd ..
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
```
## 编译程序
- use g++
``` bash
g++ -std=c++11 -O3 -I.. dlib_package_root/dlib/dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
```
- use cmake
> nano CMakeLists.txt
``` bash
cmake_minimum_required(VERSION 2.8)
project(test)
add_subdirectory(/home/....../dlib/dlib dlib_build)
add_executable(main main.cpp)
target_link_libraries(face_detector dlib::dlib)
```

View File

@ -0,0 +1,110 @@
# dlib系列笔记0开发环境
> ubuntu 16.04 python3.5 opencv3.4.0
## opencv3.4.0 安装编译
``` bash
unzip opencv-3.4.0.zip
cd opencv
sudo apt install cmake
sudo apt install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
mkdir my_build
cd my_build
## cmake 中会下载安装一个大文件,网速会很慢
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
# add this
"
/usr/local/lib
"
sudo ldconfig
sudo gedit /etc/bash.bashrc
# add this
"
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
"
source /etc/bash.bashrc
sudo updatedb
# test
cd opencv***/smaples/cpp/example_cmake
cmake .
make
./opencv_example
# and you will see yourself
# compete
```
## dlib 安装编译
``` bash
git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build . --config Release
# 安装python包
cd ..
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
```
## 编译opencv程序
- use g++
``` bash
g++ main.cpp `pkg-config opencv --cflags --libs`
```
- use cmake
> nano CMakeLists.txt
``` bash
cmake_minimum_required(VERSION 2.8)
project(test)
find_package(OpenCV REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
```
## 编译 dlib 程序
- use g++
``` bash
g++ -std=c++11 -O3 -I.. dlib_package_root/dlib/dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
```
- use cmake
> nano CMakeLists.txt
``` bash
cmake_minimum_required(VERSION 2.8)
project(test)
add_subdirectory(/home/....../dlib/dlib dlib_build)
add_executable(main main.cpp)
target_link_libraries(face_detector dlib::dlib)
```

View File

@ -0,0 +1,60 @@
# dlib系列笔记1项目简介
## 要完成的目标或功能
- 人脸检测
- 人脸特征提取
- 人脸识别
- 用摄像头实时完成以上三种功能
- 用python、cpp完成以上三种功能
- 移植到树莓派上
## 项目文件结构
> 项目地址: https://github.com/lightjiang/FaceRecognize.git
``` bash
.
├── cpp_src # cpp 源码
│ ├── CMakeLists.txt
│ ├── face_detector.cpp
│ ├── mybase.hpp
│ └── video_tracker.cpp
├── data # 图片数据
│ ├── auto_download_img
│ ├── crowd
│ ├── frontal
│ ├── profile
│ └── star
├── model # 模型 下载链接见 http://dlib.net/files/
│ ├── dlib_face_recognition_resnet_model_v1.dat
│ ├── mmod_human_face_detector.dat
│ ├── shape_predictor_5_face_landmarks.dat
│ └── shape_predictor_68_face_landmarks.dat
├── results # 存储人脸识别数据结果
│ ├── known # 人脸图片
│ │ ├── fee26871-0f30-11e8-93de-a0c589189417.jpg
| | .....
│ │ └── fef129db-0da9-11e8-93de-a0c589189417.jpg
│ ├── known_faces # 存人脸数据,如名称,来源,图片保存路径
│ ├── known_vectors # 存人脸特征
│ └── vedio
├── script
│ ├── download_baidu_img.py
│ ├── download_star_img.py
│ ├── __init__.py
│ ├── reform_star_data.py
│ └── rename_img_in_dir.py
├── base.py
├── face_alignment.py
├── face_clustering.py
├── face_detector.py
├── face_features.py
├── face_recognition.py
├── face_recognize_in_vedio.py
├── settings.py
├── uming.ttc
├── utils.py
├── README.md
└── vedio_base.py
```

View File

@ -0,0 +1,12 @@
# dlib系列笔记2获取数据
源码参考
``` bash
├── script
│ ├── download_baidu_img.py
│ ├── download_star_img.py
```
分别用两个脚本获取百度相关主题词图片和明星图库图片
注:检查文件保存路径等,先少量获取数据测试在完整运行

View File

@ -0,0 +1,192 @@
# dlib系列笔记3人脸检测
## 效果图
![img1](https://raw.githubusercontent.com/lightjiang/FaceRecognize/master/results/others/1.png)
## 源码
截取重要类的实现片段
具体源码参考 https://github.com/lightjiang/FaceRecognize.git
- base.py
``` python
class Base(object):
base_path = BASEPATH
def __init__(self):
self.img = None
self.img_path = ''
def load_img(self, path, relative=True, flags=cv2.IMREAD_COLOR):
# 自动从web加载图片
if path.startswith('http'):
path = self.download_web_img(path)
img_path = path
if relative:
path = os.path.join(self.base_path, path)
if os.path.exists(path):
res = cv2.imread(path, flags=flags)
# 转换图片通道, opencv默认bgr格式dlib需要rgb格式
self.img = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
self.img_path = img_path
return self.img
else:
raise FileNotFoundError(path)
def download_web_img(self, url):
path = 'data/auto_download_img/%s.jpg' % uuid.uuid1()
# 复制的百度图片链接有时会下载异常
request.urlretrieve(url, path)
print('download complete')
return path
def save_img(self, img, path, relative=True):
if relative:
path = os.path.join(self.base_path, path)
cv2.imwrite(path, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
def add_marks(self, pos, color=None):
# 在self.img 上添加标记
if isinstance(pos, tuple):
pos = [pos]
elif isinstance(pos, list):
pos = pos
else:
raise AttributeError
if not color:
color = (0, 255, 0)
for p in pos:
cv2.circle(self.img, p, 2, color, 1)
def add_faces(self, faces, show_score=True, color=None, add_text=None):
# 在self.img 上添加脸部识别框
if isinstance(faces, dict):
faces = [faces]
elif isinstance(faces, list):
faces = faces
else:
raise AttributeError
for face in faces:
rect = face['position']
if not color:
color = (255, 0, 0)
cv2.rectangle(self.img, (rect.left(), rect.top()), (rect.right(), rect.bottom()), color, 3)
if show_score and 'score' in face:
score = face['score']
width = rect.right() - rect.left()
cv2.putText(self.img, str(round(score, 3)), (rect.left() + 10, rect.bottom() - 10), cv2.FONT_HERSHEY_SIMPLEX, width/120,
(255, 255, 255), 1)
if add_text:
width = rect.right() - rect.left()
cv2.putText(self.img, str(add_text), (rect.left() + 10, rect.bottom() - 10),
cv2.FONT_HERSHEY_SIMPLEX, width / 120,
(255, 255, 255), 1)
```
- face_detector.py
``` python
class FaceDetector(Base):
"""
based on HOG
"""
def __init__(self):
self.face_detector = dlib.get_frontal_face_detector()
super().__init__()
def detect_face(self, log_status=True):
if self.img is None:
raise AttributeError('please load img before detect')
t = time.time()
dets, scores, idx = self.face_detector.run(self.img, 1, -1)
res = []
for index, face in enumerate(dets):
temp = {
'id': str(uuid.uuid1()),
'position': face,
'score': scores[index],
'src': self.img_path
}
if scores[index] > 0:
res.append(temp)
if log_status:
print("Detecting faces takes: {}s\nNumber of faces detected: {}".format(time.time() - t, len(res)))
return res
def detect_faces_from_imgs(self, imgs: list):
t = time.time()
res = []
for img_path in imgs:
self.load_img(img_path)
res += self.detect_face(log_status=False)
self.img = None
print("Detecting faces takes: {}s\nNumber of faces detected: {}".format(time.time() - t, len(res)))
return res
```
- cpp_src/face_detector.cpp
``` cpp
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/opencv.h>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include "mybase.hpp"
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
try
{
if (argc == 1)
{
cout << "Give some image files as arguments to this program." << endl;
return 0;
}
frontal_face_detector detector = get_frontal_face_detector();
// Loop over all the images provided on the command line.
for (int i = 1; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
cv::Mat image;
image = cv::imread(argv[i], cv::IMREAD_COLOR);
dlib::cv_image<rgb_pixel> img(image);
std::vector<rectangle> dets = detector(img);
cout << "Number of faces detected: " << dets.size() << dets[0].left() <<dets[1] << image.size() << endl<< image;
for (auto temp_point: dets){
cout << temp_point<< temp_point.left() <<endl;
mark_face(image, temp_point);
// cv::rectangle(image, cv::Point(temp_point.left(), temp_point.top()), cv::Point(temp_point.right(), temp_point.bottom()),cv::Scalar(255,0,0),1,1,0);
}
// cv::rectangle(image, cv::Point(dets[0][0][0], dets[0][0][1]));
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow("Display window", image); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
}
}
catch (exception &e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
```

View File

@ -0,0 +1 @@
# dlib系列笔记6人脸识别

View File

@ -0,0 +1 @@
# dlib系列笔记7实时人脸识别和人脸追踪

View File

@ -32,8 +32,3 @@ sudo systemctl daemon-reload
sudo systemctl restart docker
```
``` bash
docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.2
```

8
mongodb/install.md Normal file
View File

@ -0,0 +1,8 @@
# mongodb 安装使用
## docker
``` bash
docker pull mongo
docker run -p 27017:27017 -v /data/db:/data/db -d mongo:3.2
```

19
opencv/compile.md Normal file
View File

@ -0,0 +1,19 @@
# 编译opencv程序
## use g++
``` bash
g++ main.cpp `pkg-config opencv --cflags --libs`
```
## use cmake
> nano CMakeLists.txt
``` bash
cmake_minimum_required(VERSION 2.8)
project(test)
find_package(OpenCV REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
```

119
opencv/install.md Normal file
View File

@ -0,0 +1,119 @@
# opencv 环境部署指南
> http://blog.csdn.net/cocoaqin/article/details/78163171
> http://blog.csdn.net/eds95/article/details/78870194
## ubuntu
``` bash
unzip opencv-3.4.0.zip
cd opencv
sudo apt install cmake
sudo apt install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
mkdir my_build
cd my_build
## cmake 中会下载安装一个大文件,网速会很慢
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
# add this
"
/usr/local/lib
"
sudo ldconfig
sudo gedit /etc/bash.bashrc
# add this
"
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
"
source /etc/bash.bashrc
sudo updatedb
# test
cd opencv***/smaples/cpp/example_cmake
cmake .
make
./opencv_example
# and you will see yourself
# compete
```
## raspberry
``` bash
sudo apt update
sudo apt upgrade
sudo rpi-update
sudo reboot
# 图像io库
sudo apt install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
# 视频处理库
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt install libxvidcore-dev libx264-dev
# 安装gtk 用于opencv gui显示
sudo apt install libgtk2.0-dev
sudo apt install libatlas-base-dev gfortran
# 安装python 环境
sudo apt-get install python2.7-dev python3-dev
sudo apt-get install build-essential libssl-dev libevent-dev libjpeg-dev libxml2-dev libxslt-dev
unzip ....
cd opencv
mkdir build
cd build
# warning change your path
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/program/opencv_contrib-3.3.0/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..
# 修改swap空间大小
vi /etc/dphys-swapfile
# 找到CONF_SWAPSIZE=100改为1024
# set size to absolute value, leaving empty (default) then uses computed value
# you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=1024
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
make -j4
sudo make install
sudo ldconfig
xhost +
export DISPLAY=:0.0
```

55
opencv/make_a_video.md Normal file
View File

@ -0,0 +1,55 @@
# 利用opencv录制视频
``` cpp
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
VideoCapture capture(0);//如果是笔记本0打开的是自带的摄像头1 打开外接的相机
Mat img;
VideoWriter vw; //新建一个多媒体文件
namedWindow( "cam" );
int fps = capture.get(CAP_PROP_FPS ); //获取摄像头的帧率
cout << capture.get( CAP_PROP_FRAME_WIDTH )<<capture.get( CAP_PROP_FRAME_HEIGHT )<<fps<<endl;
if(fps <= 0 )fps = 25;
//设置视频的格式
vw.open( "out.avi", VideoWriter::fourcc( 'x', '2', '6', '4' ), fps, Size( capture.get( CAP_PROP_FRAME_WIDTH ), capture.get( CAP_PROP_FRAME_HEIGHT ) ) );
if(!capture.isOpened( )) //判断摄像头是否打开
{
cout << "open video faild";
return 0;
}
cout << "open video success" << endl;
if(!vw.isOpened()) //判断视频文件是否创建
{
cout << "open vw faild"<< endl;
}
cout << "open vw success" << endl;
while(1)
{
capture.read( img); //读取视频帧
if(img.empty( ))
break;
imshow("cam", img ); //显示视频帧
vw.write( img ); //将视频帧写入文件
if(waitKey( 10 ) == 'q') //q键退出录制
break;
}
return 0;
}
```