当前位置: 代码迷 >> 综合 >> dlib物体检测开发
  详细解决方案

dlib物体检测开发

热度:52   发布时间:2024-03-09 20:59:02.0

一、dlib 动态库编译

github 下载dlib源码,进入源码目录创建 dlib-master

vim dlib/CMakeLists.txt 修改部分内容

if (DLIB_ISO_CPP_ONLY)

option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} OFF)

option(DLIB_LINK_WITH_SQLITE3 ${DLIB_LINK_WITH_SQLITE3_STR} OFF)

option(DLIB_USE_BLAS ${DLIB_USE_BLAS_STR} OFF)

option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} OFF)

option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} OFF)

option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} OFF)

option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} OFF)

#option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} OFF)

option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} OFF)

else()

option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} ON)

option(DLIB_LINK_WITH_SQLITE3 ${DLIB_LINK_WITH_SQLITE3_STR} ON)

option(DLIB_USE_BLAS ${DLIB_USE_BLAS_STR} OFF)

option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} OFF)

option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} OFF)

option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} ON)

option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} ON)

#option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} ON)

option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} OFF)

endif()

mkdir build

cd build

cmake -DBUILD_SHARED_LIBS=ON -DDLIB_USE_LAPACK=1 ..

cmake --build . --config Release

编译完成后在当前目录生成dlib文件夹,里面包含动态库和config.h

为防止报错需要报config.h覆盖dlib源码目录的config.h

二、dlib 模型训练

1、训练图片处理

dib-master/tools/imglab可处理图片,详细查看README.txt

2、模型训练

dlib-master/python_examples/train_object_detecotr.py例子可参考

三、dlib C++接口调用

1、单个模型调用

typedef scan_fhog_pyramid<pyramid_down<6>> image_scanner_type;
object_detector<image_scanner_type> detector; ifstream fin("./detector.svm", ios::binary);
deserialize(detector, fin);QImage image_gray = img_rgb.convertToFormat(QImage::Format_Grayscale8);
cv::Mat mat_img = cv::Mat(image_gray.height(), image_gray.width(), CV_8UC1,       (void*)image_gray.constBits(), image_gray.bytesPerLine());dlib::cv_image<unsigned char> dlib_img(mat_img);
const std::vector<rectangle> rects = detector(dilb_img);

2、多个模型同时调用

typedef scan_fhog_pyramid<pyramid_down<6>> image_scanner_type;
std::vector<object_detector<image_scanner_type>> detectors; auto add_detector = [&](QString file_name){object_detector<image_scanner_type> detector;ifstream fin(file_name.toStdString(), ios::binary);deserialize(detector, fin);detectors.push_back(detector)
}
add_detector("./detector1.svm");
add_detector("./detector2.svm");QImage image_gray = img_rgb.convertToFormat(QImage::Format_Grayscale8);
cv::Mat mat_img = cv::Mat(image_gray.height(), image_gray.width(), CV_8UC1,       (void*)image_gray.constBits(), image_gray.bytesPerLine());dlib::cv_image<unsigned char> dlib_img(mat_img);
const std::vector<rectangle> rects = evaluate_detectors(detectors, dlib_img);