加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
thread.cpp 11.70 KB
一键复制 编辑 原始数据 按行查看 历史
丁坤 提交于 2020-11-07 14:45 . correct picture path
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
#include "thread.h"
#include <QSemaphore>
#include "camera.h"
int curbuf=1;
bool sendflag=false;
QSemaphore closestate;
QSemaphore closeflag;
//unsigned char *bufferForRGB_1=NULL; //RGB缓存区1
//unsigned char *bufferForRGB_2=NULL; //RGB缓存区2
QSemaphore emptybuff(2);
QSemaphore fullbuff;
SapLocation *m_loc=NULL;
SapAcqDevice *m_acq=NULL;
SapBuffer *m_buffer=NULL;
SapColorConversion *m_conv=NULL;
SapAcqDeviceToBuf *m_trans=NULL;
cv::Mat mat_1;
cv::Mat mat_2;
cv::Mat mat_learn;
QThreadRead::QThreadRead(QObject *parent) : QThread(parent)
{
m_stop=false;
m_mutex=new QMutex;
}
QThreadDeal::QThreadDeal(QObject *parent) : QThread(parent)
{
m_stop=false;
m_mutex=new QMutex;
}
QThreadLearn::QThreadLearn(QObject *parent) : QThread(parent)
{
}
QThreadCorrect::QThreadCorrect(QObject *parent) : QThread(parent)
{
}
ReadForLearn::ReadForLearn(QObject *parent) : QThread(parent)
{
m_stop=false;
m_mutex=new QMutex;
}
QThreadDisplay::QThreadDisplay(QObject *parent) : QThread(parent)
{
}
//-------------------------------------------------------------------------------------------------------------
void onImageDataCallback(SapXferCallbackInfo *pInfo)
{
// Display the last transferred frame
qDebug()<<"grab thread:"<<GetCurrentThreadId();
int width = m_buffer->GetWidth();
int height =m_buffer->GetHeight();
m_conv->Convert();
SapBuffer *conv_buff=m_conv->GetOutputBuffer();
unsigned char pdata_rgb;
void* pDataAddr_rgb=&pdata_rgb;
conv_buff->GetAddress(&pDataAddr_rgb);
cv::Mat img(height,width,CV_8UC3,pDataAddr_rgb);
emptybuff.acquire();
if(curbuf==1)
{
mat_1 =img;
conv_buff->ReleaseAddress(pDataAddr_rgb);
curbuf=2;
fullbuff.release();
}
else
{
mat_2 =img;
conv_buff->ReleaseAddress(pDataAddr_rgb);
curbuf=1;
fullbuff.release();
}
qDebug()<<"-----------------------"<<emptybuff.available()<<";"<<fullbuff.available();
closeflag.release();
}
void onImageForLearnCallback(SapXferCallbackInfo *pInfo)
{
// Display the last transferred frame
qDebug()<<"grab thread:"<<GetCurrentThreadId();
int width = m_buffer->GetWidth();
int height =m_buffer->GetHeight();
m_conv->Convert();
SapBuffer *conv_buff=m_conv->GetOutputBuffer();
unsigned char pdata_rgb;
void* pDataAddr_rgb=&pdata_rgb;
conv_buff->GetAddress(&pDataAddr_rgb);
cv::Mat img(height,width,CV_8UC3,pDataAddr_rgb);
char filepath[1024];
strcpy(filepath,dirpath);
char num[10];
sprintf(num,"%d.bmp",index);
index++;
strcat(filepath,num);
cv::imwrite(filepath,img);
conv_buff->ReleaseAddress(pDataAddr_rgb);
qDebug()<<height;
mat_learn=img;
sendflag=true;
closestate.release();
}
void QThreadRead::run()
{
qDebug()<<QThread::currentThreadId()<<"______read thread run______";
m_stop=false;
int n=emptybuff.available();
if(n<2)
{
emptybuff.release(2-n);
}
if(!openCamera())
{
return ;
}
//getCameraFeature();
m_conv->Enable(TRUE,FALSE);
m_conv->SetAlign(SapColorConversion::AlignRGBG);
m_conv->SetMethod(SapColorConversion::Method7);
m_conv->SetOutputFormat(SapFormatRGB888);
m_trans->SetAutoEmpty(TRUE);
m_trans->Grab();
while (true)
{
closeflag.acquire();
// msleep(450);
// m_trans->Freeze();
// m_trans->Wait(100);
// closeCamera();
// openCamera();
// m_conv->Enable(TRUE,FALSE);
// m_conv->SetAlign(SapColorConversion::AlignRGBG);
// m_conv->SetMethod(SapColorConversion::Method7);
// m_conv->SetOutputFormat(SapFormatRGB888);
// m_trans->Grab();
//1
// m_acq->Close();
// m_acq->Open();
// //2
// msleep(250);
m_trans->Freeze();
m_trans->Wait(100);
m_trans->Grab();
// //3
// m_trans->Disconnect();
// m_trans->Connect();
}
m_trans->Freeze();
m_trans->Wait(5000);
qDebug()<<"______read thread quit______";
closeCamera();
}
void QThreadRead::exitThread()
{
m_mutex->lock();
m_stop = true;
m_mutex->unlock();
}
//---------------------------------------------------------------------------------------------------------------
void QThreadDeal::run()
{
qDebug()<<QThread::currentThreadId()<<"_____deal thread run______";
PyThreadStateLock PyThreadLock;
PyObject* pReturn1=PyObject_CallMethod(PFun,"load",NULL);
if(!pReturn1)
{
qDebug()<<"return failed";
}
int ret = 0;
PyArg_Parse(pReturn1, "i", &ret);//转换返回类型
qDebug() << "load model return:" << ret ;
int n=fullbuff.available();
if(n>0)
{
fullbuff.acquire(n);
}
if(PFun)
{
qDebug()<<"python init success";
}
sleep(1);
while (1)
{
m_mutex->lock();
if(m_stop)
{
break;
}
m_mutex->unlock();
fullbuff.acquire();
if(curbuf==1)
{
emit sendimage(mat_2);
npy_intp Dims[3] = {mat_2.rows,mat_2.cols,mat_2.channels()}; //给定维度信息
qDebug()<<mat_2.rows<<mat_2.cols<<mat_2.channels();
PyObject* pReturn=call_python_file(Dims,mat_2.data);
deal_ret_from_python(pReturn);
emptybuff.release();
qDebug()<<"-----buffer 2 has been freed-----";
}
else
{
emit sendimage(mat_1);
npy_intp Dims[3] = {mat_1.rows,mat_1.cols,mat_1.channels()}; //给定维度信息
qDebug()<<mat_1.rows<<mat_1.cols<<mat_1.channels();
PyObject* pReturn=call_python_file(Dims,mat_1.data);
deal_ret_from_python(pReturn);
emptybuff.release();
qDebug()<<"-----buffer 1 has been freed-----";
}
qDebug()<<"-----------------------"<<emptybuff.available()<<";"<<fullbuff.available();
}
m_mutex->unlock();
qDebug()<<"______deal thread quit______";
}
//PyObject* QThreadDeal::initpythonmodule()
//{
// Py_Initialize();
// if (!Py_IsInitialized())
// {
// qDebug() << "Python init failed";
// }
// import_array();
// PyImport_ImportModule("sys");
// PyRun_SimpleString("import sys"); // 执行 python 中的短语句
// PyRun_SimpleString("sys.path.append('C:/Users/dk/Desktop/classify/')");
// pModule = PyImport_ImportModule("predict");//Python文件名
// if (!pModule)
// {
// qDebug() << "import moudle failed";
// return NULL;
// }
// pDict = PyModule_GetDict(pModule);
// if(!pDict)
// {
// qDebug()<<"import function failed";
// return NULL;
// }
// PyObject* pClass=PyDict_GetItemString(pDict,"Classification");
// if(!pClass)
// {
// qDebug()<<"get class failed";
// return NULL;
// }
// PyObject* pObject=PyInstanceMethod_New(pClass);
// if(!pObject)
// {
// qDebug()<<"get init function failed";
// return NULL;
// }
// PyObject* pInstance=PyObject_CallObject(pObject,NULL);
// if(!pInstance)
// {
// qDebug()<<"instance failed";
// return NULL;
// }
// return pInstance;
//}
PyObject* QThreadDeal::call_python_file(npy_intp* diminfo,unsigned char* buff)
{
import_array(); //此处不调用PyArray_SimpleNewFromData会发送错误
PyObject *PyArray = PyArray_SimpleNewFromData(3, diminfo, NPY_UBYTE, buff);
PyObject *ArgArray = PyTuple_New(1); //构造参数
PyTuple_SetItem(ArgArray, 0, PyArray);
if(!PFun)
{
qDebug()<<"pointer of class is NULL!";
}
PyObject* pReturn=PyObject_CallMethod(PFun,"predict","O",ArgArray);
return pReturn;
}
void QThreadDeal::deal_ret_from_python(PyObject* ret)
{
if(!ret)
{
qDebug()<<"return failed";
}
int res = 0;
PyArg_Parse(ret, "i", &res);//转换返回类型
qDebug() << "res:" << res ;//输出结果
emit sendRes(res);
return ;
}
void QThreadDeal::exitThread()
{
m_mutex->lock();
m_stop = true;
m_mutex->unlock();
}
void ReadForLearn::run()
{
if(!openCameraForLearn())
{
return ;
}
m_conv->Enable(TRUE,FALSE);
m_conv->SetAlign(SapColorConversion::AlignRGBG);
m_conv->SetMethod(SapColorConversion::Method7);
m_conv->SetOutputFormat(SapFormatRGB888);
m_trans->Grab();
while (1)
{
closestate.acquire();
// msleep(450);
// m_trans->Freeze();
// m_trans->Wait(100);
// closeCamera();
// openCameraForLearn();
// m_conv->Enable(TRUE,FALSE);
// m_conv->SetAlign(SapColorConversion::AlignRGBG);
// m_conv->SetMethod(SapColorConversion::Method7);
// m_conv->SetOutputFormat(SapFormatRGB888);
// m_trans->Grab();
m_trans->Freeze();
m_trans->Wait(150);
m_trans->Grab();
}
m_trans->Freeze();
m_trans->Wait(5000);
qDebug()<<"______read thread quit______";
closeCamera();
m_mutex->unlock();
}
void ReadForLearn::exitThread()
{
m_mutex->lock();
m_stop = true;
m_mutex->unlock();
}
void QThreadLearn::run()
{
if(PFun)
{
qDebug()<<"training module init success";
}
train_model();
emit learnCompelete();
exit(0);
}
//PyObject* QThreadLearn::initpythonmodule()
//{
// Py_Initialize();
// if (!Py_IsInitialized())
// {
// qDebug() << "Python init failed";
// }
// PyImport_ImportModule("sys");
// PyRun_SimpleString("import sys"); // 执行 python 中的短语句
// PyRun_SimpleString("sys.path.append('C:/Users/dk/Desktop/classify/')");
// PyObject *pModule;
// pModule = PyImport_ImportModule("training");//Python文件名
// if (!pModule)
// {
// qDebug() << "import moudle failed";
// return NULL;
// }
// PyObject* pDict = PyModule_GetDict(pModule);
// if(!pDict)
// {
// qDebug()<<"import function failed";
// return NULL;
// }
// PyObject* pClass=PyDict_GetItemString(pDict,"Classification");
// if(!pClass)
// {
// qDebug()<<"get class failed";
// return NULL;
// }
// PyObject* pObject=PyInstanceMethod_New(pClass);
// if(!pObject)
// {
// qDebug()<<"get init function failed";
// return NULL;
// }
// PyObject* pInstance=PyObject_CallObject(pObject,NULL);
// if(!pInstance)
// {
// qDebug()<<"instance failed";
// return NULL;
// }
// return pInstance;
//}
void QThreadLearn::train_model()
{
class PyThreadStateLock PyThreadLock;
PyObject *ArgArray = PyTuple_New(1); //构造参数
PyTuple_SetItem(ArgArray, 0, Py_BuildValue("s","C://woodboard//build-woodboard-Desktop_Qt_5_14_1_MSVC2017_64bit-Debug"));
PyObject* pReturn=PyObject_CallMethod(PFun,"fit_pictures","O",ArgArray);
if(!pReturn)
{
qDebug()<<"return failed";
}
int ret = 0;
PyArg_Parse(pReturn, "i", &ret);//转换返回类型
qDebug() << "ret:" << ret ;
qDebug()<<"learn compelete";
emit learnCompelete();
}
void QThreadCorrect::run()
{
if(PFun)
{
qDebug()<<"training module init success";
}
correctWood();
emit correctCompelete();
exit(0);
}
void QThreadCorrect::correctWood()
{
class PyThreadStateLock PyThreadLock;
PyObject* pReturn=PyObject_CallMethod(PFun,"correct",NULL);
if(!pReturn)
{
qDebug()<<"return failed";
}
int ret = 0;
PyArg_Parse(pReturn, "i", &ret);//转换返回类型
qDebug() << "ret:" << ret ;
}
void QThreadDisplay::run()
{
while(1)
{
if(sendflag==true)
{
emit sendimage2(mat_learn);
qDebug()<<"send it";
sendflag=false;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化