You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __IMAGE_MILL_DLIB_WORKER_HEADER__
  2. #define __IMAGE_MILL_DLIB_WORKER_HEADER__
  3. #include "../../lib/components/base/basicThread.h"
  4. #include "../../lib/components/base/basicMutex.h"
  5. #include "../../lib/components/base/basicSettingsProvider.h"
  6. #include "../../lib/components/base/basicEventHeap.h"
  7. #include "IM_DataStructs.h"
  8. #include "IM_DataBaseAdapter.h"
  9. #define DLIB
  10. #ifdef DLIB
  11. #define GPU
  12. //#include <darknet/darknet.h>
  13. //#include <darknet/yolo_v2_class.hpp>
  14. #undef GPU
  15. #endif
  16. #include <opencv2/opencv.hpp>
  17. #include <opencv2/core.hpp>
  18. #include <opencv2/imgcodecs.hpp>
  19. #include <opencv2/highgui.hpp>
  20. #include <opencv2/dnn/dnn.hpp>
  21. #include <dlib/dnn.h>
  22. #include <dlib/gui_widgets.h>
  23. #include <dlib/clustering.h>
  24. #include <dlib/string.h>
  25. #include <dlib/image_io.h>
  26. #include <dlib/image_processing/frontal_face_detector.h>
  27. #include <dlib/opencv.h>
  28. using namespace cv;
  29. using namespace dlib;
  30. using namespace std;
  31. template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
  32. using residual = add_prev1<block<N,BN,1,tag1<SUBNET>>>;
  33. template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
  34. using residual_down = add_prev2<avg_pool<2,2,2,2,skip1<tag2<block<N,BN,2,tag1<SUBNET>>>>>>;
  35. template <int N, template <typename> class BN, int stride, typename SUBNET>
  36. using block = BN<con<N,3,3,1,1,relu<BN<con<N,3,3,stride,stride,SUBNET>>>>>;
  37. template <int N, typename SUBNET> using ares = relu<residual<block,N,affine,SUBNET>>;
  38. template <int N, typename SUBNET> using ares_down = relu<residual_down<block,N,affine,SUBNET>>;
  39. template <typename SUBNET> using alevel0 = ares_down<256,SUBNET>;
  40. template <typename SUBNET> using alevel1 = ares<256,ares<256,ares_down<256,SUBNET>>>;
  41. template <typename SUBNET> using alevel2 = ares<128,ares<128,ares_down<128,SUBNET>>>;
  42. template <typename SUBNET> using alevel3 = ares<64,ares<64,ares<64,ares_down<64,SUBNET>>>>;
  43. template <typename SUBNET> using alevel4 = ares<32,ares<32,ares<32,SUBNET>>>;
  44. using anet_type = loss_metric<fc_no_bias<128,avg_pool_everything<
  45. alevel0<
  46. alevel1<
  47. alevel2<
  48. alevel3<
  49. alevel4<
  50. max_pool<3,3,2,2,relu<affine<con<32,7,7,2,2,
  51. input_rgb_image_sized<150>
  52. >>>>>>>>>>>>;
  53. // ----------------------------------------------------------------------------------------
  54. std::vector<matrix<rgb_pixel>> jitter_image( const matrix<rgb_pixel>& img );
  55. /**
  56. * TODO: Add class description
  57. *
  58. * @author Gagua
  59. */
  60. class IM_DlibWorker : public basicThread{
  61. private:
  62. frontal_face_detector m_oFaceDetector;
  63. shape_predictor m_oShapePredictor;
  64. anet_type m_oNet;
  65. IM_DataBaseAdapter * m_pDBA;
  66. //matrix<rgb_pixel> m_oInputImg;
  67. //std::vector<matrix<rgb_pixel>> m_vFacesImg;
  68. basicLogCollection * m_pLog;
  69. config_setting_t * m_pSettings;
  70. basicEventHeap * m_pEvtHeap;
  71. cv::Mat m_oWorkingMat;
  72. float m_fDetectionProbability;
  73. bool m_bUseShapePredictor;
  74. bool m_bUseFaceRecognition;
  75. char m_pPicturesBase[1024];
  76. char m_pFacesBase[1024];
  77. char m_pShapePredictorPath[1024];
  78. char m_pNetModelPath[1024];
  79. char m_sChownUser[64];
  80. char m_sChownGroup[64];
  81. basicMutex m_mBufferProtectorMutex;
  82. IM_JOB * m_pActiveJob;
  83. bool m_bDataIsActive;
  84. CSTR m_fSaveFace( matrix<rgb_pixel> &Picture, CSTR md5, uint32_t index );
  85. IM_RESULT * m_fImageAnalyzer( cv::Mat &Picture );
  86. protected:
  87. virtual void m_fThreadProc( void );//getLayerNames()
  88. public:
  89. // Constructor
  90. explicit IM_DlibWorker( config_setting_t * Settings = NULL );
  91. // Destructor
  92. virtual ~IM_DlibWorker();
  93. bool create( void );
  94. bool enqueueJob( IM_JOB *ptr );
  95. };
  96. #endif