25 changed files with 895 additions and 118 deletions
-
53Projects/img_mill/IM_ClientHandler.cpp
-
2Projects/img_mill/IM_ClientHandler.h
-
488Projects/img_mill/IM_DLibWorker.cpp
-
118Projects/img_mill/IM_DLibWorker.h
-
38Projects/img_mill/IM_DataBaseAdapter.cpp
-
3Projects/img_mill/IM_DataBaseAdapter.h
-
30Projects/img_mill/IM_DataStructs.cpp
-
11Projects/img_mill/IM_DataStructs.h
-
5Projects/img_mill/IM_DnnWorker.cpp
-
66Projects/img_mill/IM_Kernel.cpp
-
2Projects/img_mill/IM_Kernel.h
-
6Projects/img_mill/img_mill.vpj
-
10Projects/img_mill/makefile
-
52lib/components/base/basicFunctionality.cpp
-
15lib/components/base/basicFunctionality.h
-
2lib/components/base/basicLogCollection.cpp
-
7lib/components/base/basicTransport.cpp
-
2lib/components/base/basicTransport.h
-
2lib/data/mysql/database_class.cpp
-
7lib/data/net/netClientHandler.cpp
-
52lib/data/net/serverPort.cpp
-
1lib/data/net/serverPort.h
-
5lib/data/protocol/protocol.cpp
-
7lib/data/transport/transportSocket.cpp
-
29lib/data/transport/transportSocket.h
@ -0,0 +1,488 @@ |
|||||
|
#include "IM_DLibWorker.h"
|
||||
|
#include <stdio.h>
|
||||
|
#include "../../lib/alg/generic/md5.h"
|
||||
|
#include "../../lib/components/base/basicFunctionality.h"
|
||||
|
|
||||
|
#define LOG( X ) m_pLog->Log( "WDLIB", this, X )
|
||||
|
#ifdef DEBUG
|
||||
|
#define LOG_ERROR( X ) {\
|
||||
|
char _tBfrErrMSG234[256];\ |
||||
|
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ |
||||
|
m_pLog->Log( "WDLIB", this, _LOG_ERROR, _tBfrErrMSG234 );\ |
||||
|
} |
||||
|
#else
|
||||
|
#define LOG_ERROR( X ) m_pLog->Log( "WDLIB", this, _LOG_ERROR, X );
|
||||
|
#endif
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// Constructor implementation
|
||||
|
IM_DlibWorker::IM_DlibWorker( config_setting_t * Settings ) : basicThread(), m_mBufferProtectorMutex(){ |
||||
|
m_pLog = basicLogCollection::getInstance(); |
||||
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DLIB" ); |
||||
|
m_pEvtHeap = basicEventHeap::getInstance(); |
||||
|
m_pDBA = IM_DataBaseAdapter::getInstance(); |
||||
|
|
||||
|
// m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
|
||||
|
// m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
|
||||
|
m_pActiveJob = NULL; |
||||
|
|
||||
|
#ifdef DARKNET
|
||||
|
m_pDarknetDetector = NULL; |
||||
|
#endif
|
||||
|
m_fDetectionProbability = .7f; |
||||
|
m_bUseShapePredictor = false; |
||||
|
m_bUseFaceRecognition = false; |
||||
|
m_bDataIsActive = false; |
||||
|
}; |
||||
|
|
||||
|
// Destructor implementation
|
||||
|
IM_DlibWorker::~IM_DlibWorker(){ |
||||
|
#ifdef DARKNET
|
||||
|
if( m_pDarknetDetector ) delete( m_pDarknetDetector ); |
||||
|
#endif
|
||||
|
}; |
||||
|
|
||||
|
bool IM_DlibWorker::create( void ){ |
||||
|
char tBuff[256]; |
||||
|
if( m_pSettings == NULL ){ |
||||
|
return false; |
||||
|
} else { |
||||
|
config_setting_t * tSetting; |
||||
|
tSetting = config_setting_get_member( m_pSettings, "PICTURES_BASE" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "PICTURES_BASE", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "/srv/vault" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Pictures Base Path! Using default!"); |
||||
|
}; |
||||
|
sprintf( m_pPicturesBase, "%s", config_setting_get_string(tSetting)); |
||||
|
tSetting = config_setting_get_member( m_pSettings, "FACEES_BASE" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "FACEES_BASE", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "/srv/vault/faces" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Face Pictures Base Path! Using default!"); |
||||
|
}; |
||||
|
sprintf( m_pFacesBase, "%s", config_setting_get_string(tSetting)); |
||||
|
tSetting = config_setting_get_member( m_pSettings, "USE_SHAPE_PREDICTOR" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "USE_SHAPE_PREDICTOR", CONFIG_TYPE_BOOL ); |
||||
|
config_setting_set_bool( tSetting, 0 ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Class Map Usage Set! Using default!"); |
||||
|
}; |
||||
|
m_bUseShapePredictor = (bool) config_setting_get_bool(tSetting); |
||||
|
if( m_bUseShapePredictor ) { |
||||
|
tSetting = config_setting_get_member( m_pSettings, "USE_FACE_RECOGNITION" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "USE_FACE_RECOGNITION", CONFIG_TYPE_BOOL ); |
||||
|
config_setting_set_bool( tSetting, 0 ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Class Map Usage Set! Using default!"); |
||||
|
}; |
||||
|
m_bUseFaceRecognition = (bool) config_setting_get_bool(tSetting); |
||||
|
|
||||
|
tSetting = config_setting_get_member( m_pSettings, "SHAPE_PREDICTOR_LANDMARKS" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "SHAPE_PREDICTOR_LANDMARKS", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "data/shape_predictor_5_face_landmarks.dat" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Net Config File! Using default!"); |
||||
|
}; |
||||
|
sprintf( m_pShapePredictorPath, "%s", config_setting_get_string(tSetting)); |
||||
|
if( m_bUseFaceRecognition ) { |
||||
|
tSetting = config_setting_get_member( m_pSettings, "NET_CONFIG_FILE" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "NET_CONFIG_FILE", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "data/dlib_face_recognition_resnet_model_v1.dat" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Net Weights File! Using default!"); |
||||
|
}; |
||||
|
sprintf( m_pNetModelPath, "%s", config_setting_get_string(tSetting)); |
||||
|
}; |
||||
|
} else m_bUseFaceRecognition = false; |
||||
|
|
||||
|
tSetting = config_setting_get_member( m_pSettings, "DETECTION_PROBABILITY" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "DETECTION_PROBABILITY", CONFIG_TYPE_FLOAT ); |
||||
|
config_setting_set_float( tSetting, 0.5f ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Probability Set! Using default!"); |
||||
|
}; |
||||
|
m_fDetectionProbability = config_setting_get_float(tSetting); |
||||
|
|
||||
|
tSetting = config_setting_get_member( m_pSettings, "OUTPUT_CHOWN_USER" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "OUTPUT_CHOWN_USER", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "www-data" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No CHOWN USER! Using default!"); |
||||
|
}; |
||||
|
sprintf( m_sChownUser, "%s", config_setting_get_string(tSetting)); |
||||
|
|
||||
|
tSetting = config_setting_get_member( m_pSettings, "OUTPUT_CHOWN_GROUP" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "OUTPUT_CHOWN_GROUP", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "www-data" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No CHOWN GROUP! Using default!"); |
||||
|
}; |
||||
|
sprintf( m_sChownGroup, "%s", config_setting_get_string(tSetting)); |
||||
|
|
||||
|
|
||||
|
/*
|
||||
|
tSetting = config_setting_get_member( m_pSettings, "USE_CLASS_MAP" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "USE_CLASS_MAP", CONFIG_TYPE_BOOL ); |
||||
|
config_setting_set_bool( tSetting, 0 ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Class Map Usage Set! Using default!"); |
||||
|
}; |
||||
|
m_bUseClassMapping = (bool) config_setting_get_bool(tSetting); |
||||
|
if( m_bUseClassMapping ) { |
||||
|
char tMapFileName[1024]; |
||||
|
FILE *fd; |
||||
|
tSetting = config_setting_get_member( m_pSettings, "CLASS_MAP_FILE" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "CLASS_MAP_FILE", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "data/coco9k.map" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Class Map File Set! Using default!"); |
||||
|
}; |
||||
|
sprintf( tMapFileName, "%s", config_setting_get_string(tSetting) ); |
||||
|
fd = fopen( tMapFileName, "r" ); |
||||
|
if( fd ) { |
||||
|
uint32_t tV; |
||||
|
uint32_t idx = 0; |
||||
|
char tB[128]; |
||||
|
while( !feof(fd) ) { |
||||
|
if( fgets( tB, 128, fd ) ){ |
||||
|
tV = atoi( tB )+1; |
||||
|
m_vClassesMap.push_back( tV ); |
||||
|
idx++; |
||||
|
}; |
||||
|
}; |
||||
|
fclose( fd ); |
||||
|
sprintf( tB, "Loadded %d classes map", idx ); |
||||
|
LOG( tB ); |
||||
|
} else { |
||||
|
m_bUseClassMapping = false; |
||||
|
}; |
||||
|
}; |
||||
|
tSetting = config_setting_get_member( m_pSettings, "NET_TARGET_DEVICE" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "NET_TARGET_DEVICE", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "CPU" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Net Target Device! Using default!"); |
||||
|
}; |
||||
|
if( strstr( config_setting_get_string(tSetting), "CPU" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CPU; |
||||
|
if( strstr( config_setting_get_string(tSetting), "OPENCL_FP16" )!=NULL ){ |
||||
|
m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL_FP16; |
||||
|
} else { |
||||
|
if( strstr( config_setting_get_string(tSetting), "OPENCL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL; |
||||
|
}; |
||||
|
if( strstr( config_setting_get_string(tSetting), "MYRIAD" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_MYRIAD; |
||||
|
if( strstr( config_setting_get_string(tSetting), "VULKAN" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_VULKAN; |
||||
|
if( strstr( config_setting_get_string(tSetting), "FPGA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_FPGA; |
||||
|
if( strstr( config_setting_get_string(tSetting), "CUDA_FP16" )!=NULL ){ |
||||
|
m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA_FP16; |
||||
|
} else { |
||||
|
if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA; |
||||
|
}; |
||||
|
if( strstr( config_setting_get_string(tSetting), "HDDL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_HDDL; |
||||
|
sprintf( tBuff, "Device: %s", config_setting_get_string(tSetting) ); |
||||
|
LOG( tBuff ); |
||||
|
|
||||
|
tSetting = config_setting_get_member( m_pSettings, "NET_BACKEND_ENGINE" ); |
||||
|
if( tSetting == NULL ){ |
||||
|
tSetting = config_setting_add(m_pSettings, "NET_BACKEND_ENGINE", CONFIG_TYPE_STRING ); |
||||
|
config_setting_set_string( tSetting, "DEFAULT" ); |
||||
|
basicSettingsProvider::getInstance()->compromise(); |
||||
|
LOG("No Net Backend Engine! Using default!"); |
||||
|
}; |
||||
|
if( strstr( config_setting_get_string(tSetting), "DEFAULT" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT; |
||||
|
if( strstr( config_setting_get_string(tSetting), "HALIDE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_HALIDE; |
||||
|
if( strstr( config_setting_get_string(tSetting), "INFERENCE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE; |
||||
|
if( strstr( config_setting_get_string(tSetting), "OPENCV" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_OPENCV; |
||||
|
if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM; |
||||
|
if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA; |
||||
|
sprintf( tBuff, "Engine: %s", config_setting_get_string(tSetting) ); |
||||
|
LOG( tBuff ); |
||||
|
*/ |
||||
|
basicSettingsProvider::getInstance()->validate(); |
||||
|
}; |
||||
|
if( m_bUseShapePredictor ) { |
||||
|
if( m_bUseFaceRecognition ) { |
||||
|
sprintf( tBuff, "loading Data from disk... Shape: %s Face: %s", m_pShapePredictorPath, m_pNetModelPath ); |
||||
|
} else { |
||||
|
sprintf( tBuff, "loading Data from disk... Shape Predictor: %s ", m_pShapePredictorPath ); |
||||
|
}; |
||||
|
LOG( tBuff ); |
||||
|
if(!fileExist( m_pShapePredictorPath )) { |
||||
|
LOG_ERROR("No Shape Predictor file exists!"); |
||||
|
return false; |
||||
|
}; |
||||
|
if( m_bUseFaceRecognition ) { |
||||
|
if(!fileExist( m_pNetModelPath )) { |
||||
|
LOG_ERROR("No Net Model file exists!"); |
||||
|
return false; |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
m_oFaceDetector = get_frontal_face_detector(); |
||||
|
|
||||
|
if( m_bUseShapePredictor ) { |
||||
|
|
||||
|
deserialize( m_pShapePredictorPath ) >> m_oShapePredictor; |
||||
|
|
||||
|
if( m_bUseFaceRecognition ) { |
||||
|
|
||||
|
deserialize( m_pNetModelPath ) >> m_oNet; |
||||
|
|
||||
|
}; |
||||
|
}; |
||||
|
|
||||
|
return init(); |
||||
|
}; |
||||
|
|
||||
|
std::vector<matrix<rgb_pixel>> jitter_image( const matrix<rgb_pixel>& img ){ |
||||
|
// All this function does is make 100 copies of img, all slightly jittered by being
|
||||
|
// zoomed, rotated, and translated a little bit differently. They are also randomly
|
||||
|
// mirrored left to right.
|
||||
|
thread_local dlib::rand rnd; |
||||
|
|
||||
|
std::vector<matrix<rgb_pixel>> crops; |
||||
|
for (int i = 0; i < 100; ++i) |
||||
|
crops.push_back(jitter_image(img,rnd)); |
||||
|
|
||||
|
return crops; |
||||
|
}; |
||||
|
|
||||
|
CSTR IM_DlibWorker::m_fSaveFace( matrix<rgb_pixel> &Picture, CSTR md5, uint32_t index ){ |
||||
|
char tPathSrc[1024]; |
||||
|
char tPathDest[1024]; |
||||
|
CSTR nMd5; |
||||
|
CSTR nPath; |
||||
|
sprintf( tPathSrc, "%s/%s_%d.jpg", m_pFacesBase, md5, index ); |
||||
|
LOG("Save Face"); |
||||
|
LOG( tPathSrc ); |
||||
|
save_jpeg( Picture, tPathSrc, 75 ); |
||||
|
nMd5 = MD5File( tPathSrc ); |
||||
|
nPath = buildMd5Path( m_pFacesBase, nMd5, 0755, m_sChownUser, m_sChownGroup ); |
||||
|
sprintf( tPathDest, "%s/%s.jpg", nPath, nMd5 ); |
||||
|
free( nPath ); |
||||
|
//fileCopy( tPathSrc, tPathDest );
|
||||
|
//remove( tPathSrc );
|
||||
|
rename ( tPathSrc, tPathDest ); |
||||
|
chown( tPathDest, m_sChownUser, m_sChownGroup ); |
||||
|
return nMd5; |
||||
|
}; |
||||
|
|
||||
|
IM_RESULT *IM_DlibWorker::m_fImageAnalyzer( cv::Mat &Pic ){ |
||||
|
IM_RESULT * retVal = NULL; |
||||
|
cv_image<bgr_pixel> Picture(Pic); |
||||
|
std::vector<matrix<rgb_pixel>> faces; |
||||
|
std::vector<uint32_t> dbIDs; |
||||
|
uint32_t idx = 0; |
||||
|
CSTR tVAL = NULL; |
||||
|
for( auto face : m_oFaceDetector( Picture ) ){ |
||||
|
//dlib::rectangle face
|
||||
|
if( m_bUseShapePredictor ) { |
||||
|
auto shape = m_oShapePredictor( Picture, face ); |
||||
|
matrix<rgb_pixel> face_chip; |
||||
|
extract_image_chip( Picture, get_face_chip_details( shape, 150, 0.25 ), face_chip ); |
||||
|
tVAL = m_fSaveFace( face_chip, m_pActiveJob->md5, idx ); |
||||
|
faces.push_back( move( face_chip ) ); |
||||
|
} else { |
||||
|
matrix<rgb_pixel> face_chip; |
||||
|
extract_image_chip( Picture, face, face_chip ); |
||||
|
tVAL = m_fSaveFace( face_chip, m_pActiveJob->md5, idx ); |
||||
|
}; |
||||
|
if( tVAL!= NULL ) { |
||||
|
uint32_t tID; |
||||
|
tID = m_pDBA->insertItemFace( m_pActiveJob->dbID, tVAL, face.left(), face.top(), face.width(), face.height() ); |
||||
|
dbIDs.push_back( tID ); |
||||
|
|
||||
|
char tB[64]; |
||||
|
sprintf(tB,"ID:%d", tID); |
||||
|
LOG(tB); |
||||
|
|
||||
|
free( tVAL ); |
||||
|
tVAL = NULL; |
||||
|
}; |
||||
|
idx++; |
||||
|
}; |
||||
|
|
||||
|
if( m_bUseShapePredictor ) { |
||||
|
//if (faces.size() == 0){
|
||||
|
// cout << "No faces found in image!" << endl;
|
||||
|
// return 1;
|
||||
|
//}
|
||||
|
if( m_bUseFaceRecognition ) { |
||||
|
// This call asks the DNN to convert each face image in faces into a 128D vector.
|
||||
|
// In this 128D vector space, images from the same person will be close to each other
|
||||
|
// but vectors from different people will be far apart. So we can use these vectors to
|
||||
|
// identify if a pair of images are from the same person or from different people.
|
||||
|
if( faces.size() ) { |
||||
|
//LOG("Have Detected Faces!");
|
||||
|
//std::vector<matrix<float,0,1>> face_descriptors = m_oNet( faces );
|
||||
|
try{ |
||||
|
for( int idx = 0; idx < faces.size(); idx++ ) { |
||||
|
matrix<float,0,1> face_descriptor = mean( mat( m_oNet( jitter_image( faces[idx] ) ) ) ); |
||||
|
//matrix<float,0,1> face_descriptor = m_oNet( faces[0] );
|
||||
|
//LOG("Classification Done!");
|
||||
|
char strDesc[129]; |
||||
|
for( int i = 0; i < face_descriptor.size(); i++ ){ |
||||
|
m_pDBA->updateItemFace( dbIDs[idx], i, face_descriptor( i, 0 ) ); |
||||
|
if( face_descriptor( i, 0 ) < -0.5) { |
||||
|
strDesc[i]='A'; |
||||
|
} else if( face_descriptor( i, 0 ) < 0 ) { |
||||
|
strDesc[i]='B'; |
||||
|
} else if( face_descriptor( i, 0 ) < 0.5 ) { |
||||
|
strDesc[i]='C'; |
||||
|
} else strDesc[i]='D'; |
||||
|
}; |
||||
|
strDesc[128] = (char)0; |
||||
|
m_pDBA->updateItemFaceDNA( dbIDs[idx], strDesc ); |
||||
|
}; |
||||
|
} catch ( std::exception& e ){ |
||||
|
LOG_ERROR( e.what() ); |
||||
|
}; |
||||
|
} else { |
||||
|
LOG("No Faces Found!"); |
||||
|
}; |
||||
|
|
||||
|
//std::vector<matrix<float,0,1>> face_descriptors = m_oNet( faces );
|
||||
|
|
||||
|
// In particular, one simple thing we can do is face clustering. This next bit of code
|
||||
|
// creates a graph of connected faces and then uses the Chinese whispers graph clustering
|
||||
|
// algorithm to identify how many people there are and which faces belong to whom.
|
||||
|
/*
|
||||
|
std::vector<sample_pair> edges; |
||||
|
for (size_t i = 0; i < face_descriptors.size(); ++i){ |
||||
|
for (size_t j = i; j < face_descriptors.size(); ++j){ |
||||
|
// Faces are connected in the graph if they are close enough. Here we check if
|
||||
|
// the distance between two face descriptors is less than 0.6, which is the
|
||||
|
// decision threshold the network was trained to use. Although you can
|
||||
|
// certainly use any other threshold you find useful.
|
||||
|
if (length(face_descriptors[i]-face_descriptors[j]) < 0.6) |
||||
|
edges.push_back(sample_pair(i,j)); |
||||
|
}; |
||||
|
}; |
||||
|
std::vector<unsigned long> labels; |
||||
|
const auto num_clusters = chinese_whispers(edges, labels); |
||||
|
*/ |
||||
|
// This will correctly indicate that there are 4 people in the image.
|
||||
|
//cout << "number of people found in the image: "<< num_clusters << endl;
|
||||
|
|
||||
|
|
||||
|
// Now let's display the face clustering results on the screen. You will see that it
|
||||
|
// correctly grouped all the faces.
|
||||
|
/*
|
||||
|
std::vector<image_window> win_clusters(num_clusters); |
||||
|
for( size_t cluster_id = 0; cluster_id < num_clusters; ++cluster_id ){ |
||||
|
std::vector<matrix<rgb_pixel>> temp; |
||||
|
for( size_t j = 0; j < labels.size(); ++j ){ |
||||
|
if (cluster_id == labels[j]) temp.push_back(faces[j]); |
||||
|
}; |
||||
|
win_clusters[cluster_id].set_title("face cluster " + cast_to_string(cluster_id)); |
||||
|
win_clusters[cluster_id].set_image(tile_images(temp)); |
||||
|
}; |
||||
|
|
||||
|
// Finally, let's print one of the face descriptors to the screen.
|
||||
|
cout << "face descriptor for one face: " << trans(face_descriptors[0]) << endl; |
||||
|
|
||||
|
// It should also be noted that face recognition accuracy can be improved if jittering
|
||||
|
// is used when creating face descriptors. In particular, to get 99.38% on the LFW
|
||||
|
// benchmark you need to use the jitter_image() routine to compute the descriptors,
|
||||
|
// like so:
|
||||
|
matrix<float,0,1> face_descriptor = mean(mat(net(jitter_image(faces[0])))); |
||||
|
cout << "jittered face descriptor for one face: " << trans(face_descriptor) << endl; |
||||
|
// If you use the model without jittering, as we did when clustering the bald guys, it
|
||||
|
// gets an accuracy of 99.13% on the LFW benchmark. So jittering makes the whole
|
||||
|
// procedure a little more accurate but makes face descriptor calculation slower.
|
||||
|
*/ |
||||
|
}; |
||||
|
}; |
||||
|
return retVal; |
||||
|
}; |
||||
|
|
||||
|
void IM_DlibWorker::m_fThreadProc( void ){ |
||||
|
m_bThreadStatus = TH_WORKING; |
||||
|
basicEvent *EVT_PTR = NULL; |
||||
|
while( m_bThreadStatus < TH_ENDED ) { |
||||
|
while (!m_bDataIsActive && m_bThreadStatus < TH_ENDED ) SLEEP( 20 ); |
||||
|
if( m_mBufferProtectorMutex.try_lock() ){ |
||||
|
if( m_bDataIsActive ){ |
||||
|
EVT_PTR =(basicEvent *) malloc(sizeof(basicEvent)); |
||||
|
if( EVT_PTR ) { |
||||
|
memset( EVT_PTR, 0, sizeof(basicEvent)); |
||||
|
EVT_PTR->m_eClass = EVT_CLASS_IMAGE; |
||||
|
EVT_PTR->m_eType = EVT_READY; |
||||
|
if( IM_RESULT *tData = m_fImageAnalyzer( m_oWorkingMat ) ){ |
||||
|
//generate detection event
|
||||
|
EVT_PTR->m_pEvtData = tData; |
||||
|
} else { |
||||
|
//generate NOT detection event
|
||||
|
EVT_PTR->m_pEvtData = NULL; |
||||
|
}; |
||||
|
}; |
||||
|
m_pActiveJob = NULL; |
||||
|
m_bDataIsActive = false; |
||||
|
}; |
||||
|
m_mBufferProtectorMutex.unlock(); |
||||
|
}; |
||||
|
if( EVT_PTR != NULL ) { |
||||
|
m_pEvtHeap->broadcast( EVT_PTR ); |
||||
|
EVT_PTR = NULL; |
||||
|
}; |
||||
|
}; |
||||
|
}; |
||||
|
|
||||
|
bool IM_DlibWorker::enqueueJob( IM_JOB *ptr ){ |
||||
|
if( ptr ) { |
||||
|
char tPath[1024]; |
||||
|
char p1[3]; |
||||
|
char p2[3]; |
||||
|
char p3[3]; |
||||
|
LOG( ptr->md5 ); |
||||
|
memcpy( p1, ptr->md5 , 2); p1[2]=(char)0; |
||||
|
memcpy( p2, ptr->md5+2, 2); p2[2]=(char)0; |
||||
|
memcpy( p3, ptr->md5+4, 2); p3[2]=(char)0; |
||||
|
switch( ptr->image_type ) { |
||||
|
case IM_ITEM_TYPE_JPEG: sprintf( tPath, "%s/%s/%s/%s/%s.jpg", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break; |
||||
|
case IM_ITEM_TYPE_PNG: sprintf( tPath, "%s/%s/%s/%s/%s.png", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break; |
||||
|
case IM_ITEM_TYPE_GIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break; |
||||
|
case IM_ITEM_TYPE_AGIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break; |
||||
|
case IM_ITEM_TYPE_TIFF: sprintf( tPath, "%s/%s/%s/%s/%s.tiff", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break; |
||||
|
}; |
||||
|
#ifdef DEBUG
|
||||
|
LOG( tPath ); |
||||
|
#endif
|
||||
|
Mat Frame = cv::imread( tPath, IMREAD_COLOR ); |
||||
|
try{ |
||||
|
while( m_bThreadStatus < TH_ENDED ) { |
||||
|
if( m_mBufferProtectorMutex.try_lock() ) { |
||||
|
Frame.copyTo( m_oWorkingMat ); |
||||
|
#ifdef DEBUG
|
||||
|
cv::imwrite("cur-test-img.jpg", m_oWorkingMat ); |
||||
|
//save_jpeg( m_oInputImg, "cur-test-img.jpg", 75 );
|
||||
|
#endif
|
||||
|
m_bDataIsActive = true; |
||||
|
m_pActiveJob = ptr; |
||||
|
m_mBufferProtectorMutex.unlock(); |
||||
|
return true; |
||||
|
} else SLEEP(5); |
||||
|
}; |
||||
|
} catch ( exception e ){ |
||||
|
LOG_ERROR( "Failure during Image Read!" ); |
||||
|
}; |
||||
|
}; |
||||
|
return false; |
||||
|
}; |
||||
|
|
||||
|
#undef LOG
|
||||
|
#undef LOG_ERROR
|
||||
@ -0,0 +1,118 @@ |
|||||
|
#ifndef __IMAGE_MILL_DLIB_WORKER_HEADER__ |
||||
|
#define __IMAGE_MILL_DLIB_WORKER_HEADER__ |
||||
|
|
||||
|
#include "../../lib/components/base/basicThread.h" |
||||
|
#include "../../lib/components/base/basicMutex.h" |
||||
|
#include "../../lib/components/base/basicSettingsProvider.h" |
||||
|
#include "../../lib/components/base/basicEventHeap.h" |
||||
|
|
||||
|
#include "IM_DataStructs.h" |
||||
|
#include "IM_DataBaseAdapter.h" |
||||
|
|
||||
|
#define DLIB |
||||
|
|
||||
|
#ifdef DLIB |
||||
|
#define GPU |
||||
|
//#include <darknet/darknet.h> |
||||
|
//#include <darknet/yolo_v2_class.hpp> |
||||
|
#undef GPU |
||||
|
#endif |
||||
|
|
||||
|
#include <opencv2/opencv.hpp> |
||||
|
#include <opencv2/core.hpp> |
||||
|
#include <opencv2/imgcodecs.hpp> |
||||
|
#include <opencv2/highgui.hpp> |
||||
|
#include <opencv2/dnn/dnn.hpp> |
||||
|
|
||||
|
#include <dlib/dnn.h> |
||||
|
#include <dlib/gui_widgets.h> |
||||
|
#include <dlib/clustering.h> |
||||
|
#include <dlib/string.h> |
||||
|
#include <dlib/image_io.h> |
||||
|
#include <dlib/image_processing/frontal_face_detector.h> |
||||
|
#include <dlib/opencv.h> |
||||
|
|
||||
|
|
||||
|
using namespace cv; |
||||
|
using namespace dlib; |
||||
|
using namespace std; |
||||
|
|
||||
|
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET> |
||||
|
using residual = add_prev1<block<N,BN,1,tag1<SUBNET>>>; |
||||
|
|
||||
|
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET> |
||||
|
using residual_down = add_prev2<avg_pool<2,2,2,2,skip1<tag2<block<N,BN,2,tag1<SUBNET>>>>>>; |
||||
|
|
||||
|
template <int N, template <typename> class BN, int stride, typename SUBNET> |
||||
|
using block = BN<con<N,3,3,1,1,relu<BN<con<N,3,3,stride,stride,SUBNET>>>>>; |
||||
|
|
||||
|
template <int N, typename SUBNET> using ares = relu<residual<block,N,affine,SUBNET>>; |
||||
|
template <int N, typename SUBNET> using ares_down = relu<residual_down<block,N,affine,SUBNET>>; |
||||
|
|
||||
|
template <typename SUBNET> using alevel0 = ares_down<256,SUBNET>; |
||||
|
template <typename SUBNET> using alevel1 = ares<256,ares<256,ares_down<256,SUBNET>>>; |
||||
|
template <typename SUBNET> using alevel2 = ares<128,ares<128,ares_down<128,SUBNET>>>; |
||||
|
template <typename SUBNET> using alevel3 = ares<64,ares<64,ares<64,ares_down<64,SUBNET>>>>; |
||||
|
template <typename SUBNET> using alevel4 = ares<32,ares<32,ares<32,SUBNET>>>; |
||||
|
|
||||
|
using anet_type = loss_metric<fc_no_bias<128,avg_pool_everything< |
||||
|
alevel0< |
||||
|
alevel1< |
||||
|
alevel2< |
||||
|
alevel3< |
||||
|
alevel4< |
||||
|
max_pool<3,3,2,2,relu<affine<con<32,7,7,2,2, |
||||
|
input_rgb_image_sized<150> |
||||
|
>>>>>>>>>>>>; |
||||
|
|
||||
|
// ---------------------------------------------------------------------------------------- |
||||
|
|
||||
|
|
||||
|
std::vector<matrix<rgb_pixel>> jitter_image( const matrix<rgb_pixel>& img ); |
||||
|
|
||||
|
/** |
||||
|
* TODO: Add class description |
||||
|
* |
||||
|
* @author Gagua |
||||
|
*/ |
||||
|
class IM_DlibWorker : public basicThread{ |
||||
|
private: |
||||
|
|
||||
|
frontal_face_detector m_oFaceDetector; |
||||
|
shape_predictor m_oShapePredictor; |
||||
|
anet_type m_oNet; |
||||
|
IM_DataBaseAdapter * m_pDBA; |
||||
|
//matrix<rgb_pixel> m_oInputImg; |
||||
|
//std::vector<matrix<rgb_pixel>> m_vFacesImg; |
||||
|
|
||||
|
basicLogCollection * m_pLog; |
||||
|
config_setting_t * m_pSettings; |
||||
|
basicEventHeap * m_pEvtHeap; |
||||
|
cv::Mat m_oWorkingMat; |
||||
|
float m_fDetectionProbability; |
||||
|
bool m_bUseShapePredictor; |
||||
|
bool m_bUseFaceRecognition; |
||||
|
char m_pPicturesBase[1024]; |
||||
|
char m_pFacesBase[1024]; |
||||
|
char m_pShapePredictorPath[1024]; |
||||
|
char m_pNetModelPath[1024]; |
||||
|
char m_sChownUser[64]; |
||||
|
char m_sChownGroup[64]; |
||||
|
basicMutex m_mBufferProtectorMutex; |
||||
|
IM_JOB * m_pActiveJob; |
||||
|
bool m_bDataIsActive; |
||||
|
CSTR m_fSaveFace( matrix<rgb_pixel> &Picture, CSTR md5, uint32_t index ); |
||||
|
IM_RESULT * m_fImageAnalyzer( cv::Mat &Picture ); |
||||
|
protected: |
||||
|
virtual void m_fThreadProc( void );//getLayerNames() |
||||
|
public: |
||||
|
// Constructor |
||||
|
explicit IM_DlibWorker( config_setting_t * Settings = NULL ); |
||||
|
// Destructor |
||||
|
virtual ~IM_DlibWorker(); |
||||
|
bool create( void ); |
||||
|
bool enqueueJob( IM_JOB *ptr ); |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,30 @@ |
|||||
|
#include "IM_DataStructs.h"
|
||||
|
|
||||
|
CSTR buildMd5Path( CSTR base, CSTR md5, mode_t mode, CSTR user, CSTR group ){ |
||||
|
char tPath[1024]; |
||||
|
char p1[3]; |
||||
|
char p2[3]; |
||||
|
char p3[3]; |
||||
|
if(!fileExist( base )) return NULL; |
||||
|
memcpy( p1, md5 , 2); p1[2]=(char)0; |
||||
|
sprintf( tPath, "%s/%s", base, p1 ); |
||||
|
if(!fileExist( tPath )) { |
||||
|
if( mkdir( tPath, mode )==-1) return NULL; |
||||
|
if( user!= NULL && group!= NULL ) chown( tPath, user, group ); |
||||
|
}; |
||||
|
memcpy( p2, md5+2, 2); p2[2]=(char)0; |
||||
|
sprintf( tPath, "%s/%s/%s", base, p1, p2 ); |
||||
|
if(!fileExist( tPath )){ |
||||
|
if( mkdir( tPath, mode )==-1) return NULL; |
||||
|
if( user!= NULL && group!= NULL ) chown( tPath, user, group ); |
||||
|
}; |
||||
|
memcpy( p3, md5+4, 2); p3[2]=(char)0; |
||||
|
sprintf( tPath, "%s/%s/%s/%s", base, p1, p2, p3 ); |
||||
|
if(!fileExist( tPath )) { |
||||
|
if( mkdir( tPath, mode )==-1) return NULL; |
||||
|
if( user!= NULL && group!= NULL ) chown( tPath, user, group ); |
||||
|
}; |
||||
|
return strdup( tPath ); |
||||
|
}; |
||||
|
|
||||
|
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue