Browse Source

img_mill ver 1.0

AGP
Gitea 6 years ago
parent
commit
a5cc92b631
  1. 32
      Projects/img_mill/IM_ClientHandler.cpp
  2. 4
      Projects/img_mill/IM_DataBaseAdapter.cpp
  3. 2
      Projects/img_mill/IM_DataBaseAdapter.h
  4. 1
      Projects/img_mill/IM_DataStructs.h
  5. 183
      Projects/img_mill/IM_DnnWorker.cpp
  6. 7
      Projects/img_mill/IM_DnnWorker.h
  7. 18
      Projects/img_mill/IM_Kernel.cpp
  8. 1
      Projects/img_mill/IM_Kernel.h
  9. 2
      Projects/img_mill/img_mill.vpj
  10. 2
      Projects/img_mill/makefile

32
Projects/img_mill/IM_ClientHandler.cpp

@ -44,7 +44,7 @@ IM_ClientHandler::IM_ClientHandler( int SOCKET ) : networkClientHandler( SOCKE
// Destructor implementation
IM_ClientHandler::~IM_ClientHandler(){
if( m_pPort != NULL ) delete( m_pPort );
//if( m_pMyCurrentJob ) free( (IM_JOB *)m_pMyCurrentJob );
if( m_pMyCurrentJob ) free( (IM_JOB *)m_pMyCurrentJob );
};
bool IM_ClientHandler::getStatus( void ){
@ -61,6 +61,11 @@ bool IM_ClientHandler::handleItem( IM_ITEM_DATA *ptr ){
IM_JOB *tJOB;
tJOB = (IM_JOB*)malloc(sizeof(IM_JOB));
if( tJOB ) {
if( m_pMyCurrentJob ) {
LOG("CLEANNING UNCLEAN JOB!");
//AnyWay Clean Memory that was Used!
free((IM_JOB *)m_pMyCurrentJob);
};
m_pMyCurrentJob = (void*) tJOB;
memset( tJOB, 0 , sizeof(IM_JOB));
tJOB->dbID = ptr->item_id;
@ -124,6 +129,7 @@ bool IM_ClientHandler::packetHandler( protocolPackage *Package ){
};
};
} catch( exception e ){
LOG_ERROR("Some Error in XML Parser!");
};
try{
Json::Value root;
@ -151,6 +157,7 @@ bool IM_ClientHandler::packetHandler( protocolPackage *Package ){
};
};
} catch ( exception e ){
LOG_ERROR("Some Error in JSON Parser!");
};
};
};
@ -164,8 +171,31 @@ void IM_ClientHandler::dispatchEvent( basicEvent *Evt ){
IM_RESULT *tData = (IM_RESULT*) Evt->m_pEvtData;
if( tData!= NULL ) {
if( tData->pJOB == m_pMyCurrentJob ) {
LOG("JOB Finished Sucessfully!");
for( int i=0; i < tData->nBoxCount; i++ ){
m_pDBA->insertItemROI(
tData->pJOB->dbID,
tData->boxes[i].classID,
tData->boxes[i].weight,
tData->boxes[i].x,
tData->boxes[i].y,
tData->boxes[i].width,
tData->boxes[i].height
);
};
//handle finished Job
free( (IM_JOB *)m_pMyCurrentJob );
m_pMyCurrentJob = NULL;
free( (IM_CLASS_BOUNDARY_BOX *)tData->boxes );
free( (IM_RESULT *)Evt->m_pEvtData );
Evt->m_pEvtData = NULL;
m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"OK\"}\n", 16 );
};
} else {
LOG("JOB Finished Without Detection!");
free( (IM_JOB *)m_pMyCurrentJob );
m_pMyCurrentJob = NULL;
m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
};
};
};

4
Projects/img_mill/IM_DataBaseAdapter.cpp

@ -194,10 +194,10 @@ IM_ITEM_DATA *IM_DataBaseAdapter::getItemDataByMd5( CSTR md5 ){
return tVal;
};
void IM_DataBaseAdapter::updateItemROI( uint32_t itemID, uint32_t classID, uint32_t x, uint32_t y, uint32_t w, uint32_t h ){
void IM_DataBaseAdapter::insertItemROI( uint32_t itemID, uint32_t classID, float weight, uint32_t x, uint32_t y, uint32_t w, uint32_t h ){
if( m_pConnectorClass ) {
char tQBuf[CTEXT];
sprintf(tQBuf, "INSERT INTO `item_rois` SET `item_id`=%d, `class_id`=%d, `x`=%d, `y`=%d, `width`=%d, `height`=%d", itemID, classID, x, y, w, h );
sprintf(tQBuf, "INSERT INTO `item_rois` SET `item_id`=%d, `class_id`=%d, `probability`=%.5f, `x`=%d, `y`=%d, `width`=%d, `height`=%d", itemID, classID, weight, x, y, w, h );
#ifdef DEBUG
LOG(tQBuf);
#endif

2
Projects/img_mill/IM_DataBaseAdapter.h

@ -25,7 +25,7 @@ class IM_DataBaseAdapter {
IM_ITEM_DATA * getItemDataById( uint32_t itemID );
IM_ITEM_DATA * getItemDataByMd5( CSTR md5 );
void updateItemROI( uint32_t itemID, uint32_t classID, uint32_t x, uint32_t y, uint32_t w, uint32_t h );
void insertItemROI( uint32_t itemID, uint32_t classID, float weight, uint32_t x, uint32_t y, uint32_t w, uint32_t h );
protected:
// Constructor - protected so users cannot call it.

1
Projects/img_mill/IM_DataStructs.h

@ -30,6 +30,7 @@ typedef struct S_IM_JOB{
typedef struct S_IM_CLASS_BOUNDARY_BOX{
uint32_t classID;
float weight;
uint32_t x;
uint32_t y;
uint32_t width;

183
Projects/img_mill/IM_DnnWorker.cpp

@ -29,7 +29,11 @@ IM_DnnWorker::IM_DnnWorker( config_setting_t * Settings ) : basicThread(), m_mBu
};
// Destructor implementation
IM_DnnWorker::~IM_DnnWorker(){};
IM_DnnWorker::~IM_DnnWorker(){
#ifdef DARKNET
if( m_pDarknetDetector ) delete( m_pDarknetDetector );
#endif
};
bool IM_DnnWorker::create( void ){
char tBuff[256];
@ -61,6 +65,52 @@ bool IM_DnnWorker::create( void ){
LOG("No Net Weights File! Using default!");
};
sprintf( m_pWeightsPath, "%s", config_setting_get_string(tSetting));
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, "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 );
@ -137,21 +187,23 @@ bool IM_DnnWorker::create( void ){
return init();
};
bool IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
IM_RESULT *IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
Size inpSize;
bool swapRB = true; //BGR->RGB
double scale = 1.0;///255.0;
Scalar mean;
IM_RESULT * retVal = NULL;
inpSize.width = Picture.cols;
inpSize.height = Picture.rows;
#ifndef DARKNET
try {
LOG("Create a 4D blob from a frame");
//cv::resize( m_oWorkingMat, m_oWorkingMat, cv::Size(416, 416));
Mat blob = cv::dnn::blobFromImage( Picture, scale, Size(416, 416), mean, swapRB, false, CV_8U );
if( blob.empty() ) {
LOG_ERROR("Failure Creating Blob!");
return false;
return retVal;
};
// construct a blob from the input frame and then perform a forward
// pass of the YOLO object detector, giving us our bounding boxes
@ -179,56 +231,115 @@ bool IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
LOG_ERROR("Catched Exception");
LOG_ERROR( e.what() );
};
return false;
#else
std::shared_ptr<image_t> img = m_pDarknetDetector->mat_to_image_resize( Picture );
std::vector<bbox_t> result_vec;
//result_vec = detector.detect_resized( * img, inpSize.width, inpSize.height, thresh, true);
result_vec = m_pDarknetDetector->detect_resized( *img, inpSize.width, inpSize.height, m_fDetectionProbability, true);
if( result_vec.size() ) {
retVal = (IM_RESULT *)malloc(sizeof(IM_RESULT));
if( retVal ) {
retVal->pJOB = m_pActiveJob;
retVal->nBoxCount = result_vec.size();
retVal->boxes = (IM_CLASS_BOUNDARY_BOX *) calloc( retVal->nBoxCount,sizeof(IM_CLASS_BOUNDARY_BOX));
if( retVal->boxes ) {
for( int i=0; i< result_vec.size(); i++ ) {
bbox_t * b = &(result_vec.at(i));
retVal->boxes[i].weight = b->prob;
retVal->boxes[i].x = b->x;
retVal->boxes[i].y = b->y;
retVal->boxes[i].width = b->w;
retVal->boxes[i].height = b->h;
if( m_bUseClassMapping ){
retVal->boxes[i].classID = m_vClassesMap[b->obj_id];
} else {
retVal->boxes[i].classID = b->obj_id;
};
#ifdef DEBUG
char msg[256];
sprintf( msg, "[%d] Class: %d Prob: %.2f X:%d Y:%d W:%d H:%d", i, retVal->boxes[i].classID, retVal->boxes[i].weight, b->x, b->y, b->w, b->h );
LOG( msg );
#endif
};
} else {
free(retVal);
retVal = NULL;
};
};
};
#endif
return retVal;
};
void IM_DnnWorker::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 ){
LOG( "Pushing Picure to DNN" );
m_fImageAnalyzer( m_oWorkingMat );
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();
};
// Create Finish EVT;
//basicEvent *tEvt;
//tEvt = createVAEvent(true);// EVT_CLASS_VIDEO, EVT_ALARM, "Motion Detected");
//if (tEvt!=NULL)m_pEventHeap->broadcast(tEvt);^M
if( EVT_PTR != NULL ) {
m_pEvtHeap->broadcast( EVT_PTR );
EVT_PTR = NULL;
};
};
};
bool IM_DnnWorker::enqueueJob( IM_JOB *ptr ){
if( ptr ) {
if( m_mBufferProtectorMutex.try_lock() ) {
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->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;
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->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 );
if( !Frame.empty() ){
while( m_bThreadStatus < TH_ENDED ) {
if( m_mBufferProtectorMutex.try_lock() ) {
Frame.copyTo( m_oWorkingMat );
#ifdef DEBUG
cv::imwrite("cur-test-img.jpg", m_oWorkingMat );
#endif
m_bDataIsActive = true;
m_pActiveJob = ptr;
m_mBufferProtectorMutex.unlock();
return true;
};
SLEEP(5);
};
LOG( tPath );
Mat Frame = cv::imread( tPath, IMREAD_COLOR );
if( !Frame.empty() ){
Frame.copyTo( m_oWorkingMat );
cv::resize( m_oWorkingMat, m_oWorkingMat, cv::Size(416, 416));
cv::imwrite("cur-test-img.jpg", m_oWorkingMat );
m_bDataIsActive = true;
m_mBufferProtectorMutex.unlock();
} else LOG_ERROR( "Failure during Image Read!" );
return true;
} else {
LOG_ERROR( "Failure during Image Read!" );
};
};
return false;

7
Projects/img_mill/IM_DnnWorker.h

@ -7,7 +7,7 @@
#include "../../lib/components/base/basicEventHeap.h"
#include "IM_DataStructs.h"
//#define DARKNET
#define DARKNET
#ifdef DARKNET
#define GPU
@ -40,6 +40,9 @@ class IM_DnnWorker : public basicThread {
cv::dnn::Target m_eTargetDevice;
cv::dnn::Net m_net;
cv::Mat m_oWorkingMat;
float m_fDetectionProbability;
bool m_bUseClassMapping;
std::vector<uint32_t> m_vClassesMap;
basicEventHeap * m_pEvtHeap;
char m_pPicturesBase[1024];
char m_pConfigPath[1024];
@ -47,7 +50,7 @@ class IM_DnnWorker : public basicThread {
basicMutex m_mBufferProtectorMutex;
IM_JOB * m_pActiveJob;
bool m_bDataIsActive;
virtual bool m_fImageAnalyzer( Mat &Picture );
IM_RESULT * m_fImageAnalyzer( Mat &Picture );
protected:
virtual void m_fThreadProc( void );//getLayerNames()
public:

18
Projects/img_mill/IM_Kernel.cpp

@ -25,6 +25,7 @@ IM_Kernel *IM_Kernel::getInstance(){
inline IM_Kernel::IM_Kernel( config_setting_t *Settings ) : basicEngine(), basicThread(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
m_pEvtHeap = basicEventHeap::getInstance();
m_bExitCause = 0;
m_pWorker = NULL;
m_pDBA = NULL;
@ -80,7 +81,22 @@ void IM_Kernel::m_fThreadProc( void ){
if( m_qJOBS.size() ) {
if( m_pWorker ){
m_pActiveJob = m_qJOBS.front();
if( m_pWorker->enqueueJob( m_pActiveJob ) ) m_qJOBS.pop();
m_qJOBS.pop();
if( !m_pWorker->enqueueJob( m_pActiveJob ) ) {
#ifdef DEBUG
LOG("Not Inserted Job Must generate EVT");
#endif
basicEvent *tEVT;
tEVT = (basicEvent *)malloc(sizeof(basicEvent));
if( tEVT ) {
memset(tEVT,0,sizeof(basicEvent));
tEVT->m_eClass = EVT_CLASS_IMAGE;
tEVT->m_eType = EVT_READY;
m_pEvtHeap->broadcast( tEVT );
} else {
LOG_ERROR( "Failure to Emmit EVENT!");
};
};
} else LOG_ERROR( "DNN Worker Not Created!" );
};
SLEEP( DEFAULT_THREAD_SLEEP_TIME );

1
Projects/img_mill/IM_Kernel.h

@ -19,6 +19,7 @@ class IM_Kernel : public basicEngine, public basicThread {
private:
basicLogCollection * m_pLog;
config_setting_t * m_pSettings;
basicEventHeap * m_pEvtHeap;
IM_DataBaseAdapter * m_pDBA;
uint8_t m_bExitCause;
IM_DnnWorker * m_pWorker;

2
Projects/img_mill/img_mill.vpj

@ -13,7 +13,7 @@
Version="1"
OutputFile="%bdimg_mill"
CompilerConfigName="Latest Version"
Defines='"/D_OPENCV_" "/DDEBUG"'>
Defines='"/D_OPENCV_" "/DDEBUG" "/DOPENCV"'>
<Menu>
<Target
Name="Compile"

2
Projects/img_mill/makefile

@ -67,7 +67,7 @@ ALL_OBJ=$(OUTDIR)/basicDualThread.o $(OUTDIR)/basicEngine.o \
libavresample libavutil libswresample libswscale libconfig++ libcurl \
opencv4` -lGL -lGLU -lc -lm -lz -ldarknet
COMPILE=g++ -c -x c++ "-D_OPENCV_" "-DDEBUG" -g -ggdb `pkg-config --cflags opencv4` -o "$(OUTDIR)/$(*F).o" $(CFG_INC) $<
COMPILE=g++ -c -x c++ "-D_OPENCV_" "-DDEBUG" "-DOPENCV" -g -ggdb `pkg-config --cflags opencv4` -o "$(OUTDIR)/$(*F).o" $(CFG_INC) $<
LINK=g++ -g -ggdb -o "$(OUTFILE)" $(ALL_OBJ)
# Pattern rules

Loading…
Cancel
Save