|
@@ -0,0 +1,313 @@
|
|
|
+#include <QCoreApplication>
|
|
|
+
|
|
|
+
|
|
|
+#include <QDateTime>
|
|
|
+#include <QMutex>
|
|
|
+
|
|
|
+#include <QWaitCondition>
|
|
|
+
|
|
|
+#include <opencv2/opencv.hpp>
|
|
|
+#include <opencv2/core/core.hpp>
|
|
|
+#include <opencv2/highgui/highgui.hpp>
|
|
|
+#include <string>
|
|
|
+#include <iostream>
|
|
|
+#include <zconf.h>
|
|
|
+#include <csignal>
|
|
|
+#include <thread>
|
|
|
+#include "MvGmslCamera.h"
|
|
|
+#include <fstream>
|
|
|
+#include <chrono>
|
|
|
+
|
|
|
+#include "modulecomm.h"
|
|
|
+#include "xmlparam.h"
|
|
|
+
|
|
|
+#include "rawpic.pb.h"
|
|
|
+
|
|
|
+#ifdef USE_OPENCV4
|
|
|
+#include "opencv2/imgcodecs/legacy/constants_c.h" //OpenCV4 use this line
|
|
|
+#include <opencv2/imgproc/types_c.h> //OpenCV4 use this line
|
|
|
+#endif
|
|
|
+
|
|
|
+#define CAM_MAX 4
|
|
|
+
|
|
|
+using namespace std::chrono;
|
|
|
+
|
|
|
+using std::string;
|
|
|
+sig_atomic_t exitRequested = 0;
|
|
|
+
|
|
|
+void handler(int) {
|
|
|
+ std::cout << "will exit..." << std::endl;
|
|
|
+ exitRequested = true;
|
|
|
+}
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ uint32_t window_num{4};
|
|
|
+ string cam_devname;
|
|
|
+ string cam_fmt;
|
|
|
+ unsigned int cam_pixfmt;
|
|
|
+ unsigned int cam_w{1280};
|
|
|
+ unsigned int cam_h{720};
|
|
|
+ unsigned int fps;
|
|
|
+} sample_context_t;
|
|
|
+
|
|
|
+
|
|
|
+cv::Mat gMat[CAM_MAX];
|
|
|
+QMutex gMutex[CAM_MAX];
|
|
|
+
|
|
|
+QWaitCondition gwc[CAM_MAX];
|
|
|
+
|
|
|
+QMutex gMutexWait[CAM_MAX];
|
|
|
+
|
|
|
+bool gbUpdate[CAM_MAX];
|
|
|
+
|
|
|
+qint64 gTimePic[CAM_MAX];
|
|
|
+
|
|
|
+bool gbRun = true;
|
|
|
+
|
|
|
+void * gThreadComp[CAM_MAX];
|
|
|
+
|
|
|
+std::string gstrmempic[CAM_MAX];
|
|
|
+
|
|
|
+void * gpapic[CAM_MAX];
|
|
|
+
|
|
|
+int gcompress = 1;
|
|
|
+
|
|
|
+int gcompquality = 95;
|
|
|
+
|
|
|
+void ThreadCompress(const int n)
|
|
|
+{
|
|
|
+ int index = 0;
|
|
|
+ std::cout<<" camera n : "<<n<<std::endl;
|
|
|
+
|
|
|
+ int nserbufsize = 20000000;
|
|
|
+
|
|
|
+ char * strser = new char[nserbufsize];
|
|
|
+ if((n<0)||(n>=CAM_MAX))
|
|
|
+ {
|
|
|
+ std::cout<<"camera error. n: "<<n<<std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ while(gbRun)
|
|
|
+ {
|
|
|
+ gMutexWait[n].lock();
|
|
|
+ gwc[n].wait(&gMutexWait[n],100);
|
|
|
+ gMutexWait[n].unlock();
|
|
|
+ if(gbUpdate[n])
|
|
|
+ {
|
|
|
+
|
|
|
+ // std::cout<<"new jpg"<<std::endl;
|
|
|
+ qint64 nPicTime;
|
|
|
+ cv::Mat xMat;
|
|
|
+ gMutex[n].lock();
|
|
|
+ xMat = gMat[n].clone();
|
|
|
+ nPicTime = gTimePic[n];
|
|
|
+ gbUpdate[n] = false;
|
|
|
+ gMutex[n].unlock();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ iv::vision::rawpic pic;
|
|
|
+
|
|
|
+ // qint64 time = QDateTime::currentMSecsSinceEpoch();
|
|
|
+// QTime xg;
|
|
|
+// xg.start();
|
|
|
+ pic.set_time(nPicTime);
|
|
|
+ pic.set_index(index);index++;
|
|
|
+
|
|
|
+
|
|
|
+ pic.set_elemsize(xMat.elemSize());
|
|
|
+ pic.set_width(xMat.cols);
|
|
|
+ pic.set_height(xMat.rows);
|
|
|
+ pic.set_mattype(xMat.type());
|
|
|
+
|
|
|
+// cv::Mat matcompress;
|
|
|
+// cv::resize(mat1,matcompress,cv::Size(640,360));
|
|
|
+
|
|
|
+
|
|
|
+ std::vector<int> param = std::vector<int>(2);
|
|
|
+ param[0] = CV_IMWRITE_JPEG_QUALITY;
|
|
|
+ param[1] = gcompquality; // default(95) 0-100
|
|
|
+ std::vector<unsigned char> buff;
|
|
|
+ if(gcompress == 1)
|
|
|
+ {
|
|
|
+ cv::imencode(".jpg", xMat, buff, param);
|
|
|
+// cv::imencode(".jpg", matcompress, buff, param);
|
|
|
+ pic.set_picdata(buff.data(),buff.size());
|
|
|
+ buff.clear();
|
|
|
+ pic.set_type(2);
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pic.set_picdata(xMat.data,xMat.rows*xMat.cols*xMat.elemSize());
|
|
|
+// pic.set_picdata(matcompress.data,matcompress.rows*matcompress.cols*matcompress.elemSize());
|
|
|
+ pic.set_type(1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// qDebug("buff size is %d",buff.size());
|
|
|
+
|
|
|
+
|
|
|
+ int nSize = pic.ByteSize();
|
|
|
+
|
|
|
+ // char * strser = new char[nSize];
|
|
|
+ bool bser = pic.SerializeToArray(strser,nSize);
|
|
|
+
|
|
|
+ std::cout<<" Time Latency is :"<<(QDateTime::currentMSecsSinceEpoch() - nPicTime)<<std::endl;
|
|
|
+ // iv::modulecomm::ModuleSendMsg(gpa,str.data(),str.length());
|
|
|
+ if(bser)iv::modulecomm::ModuleSendMsg(gpapic[n],strser,nSize);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ std::cout<<"usbcamera "<< "serialize error. "<<std::endl;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // qint64 time2 = QDateTime::currentMSecsSinceEpoch();
|
|
|
+
|
|
|
+ // qint64 ncomptime = time2- time1;
|
|
|
+ // int npicsize = buff.size();
|
|
|
+
|
|
|
+// std::cout<<" compress time is "<<ncomptime<<" size is "<<npicsize<<std::endl;
|
|
|
+// std::cout<<" Thread time is : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ std::cout<<"no new jpg "<<std::endl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int main(int argc, char *argv[])
|
|
|
+{
|
|
|
+ QCoreApplication a(argc, argv);
|
|
|
+
|
|
|
+ unsigned int i;
|
|
|
+ for(i=0;i<CAM_MAX;i++)
|
|
|
+ {
|
|
|
+ gbUpdate[i] = false;
|
|
|
+ gstrmempic[i] = "image0"+ QString::number(i).toStdString();
|
|
|
+ gpapic[i] = iv::modulecomm::RegisterSend(gstrmempic[i].data(),10000000,1);
|
|
|
+ gThreadComp[i] = new std::thread(ThreadCompress,i);
|
|
|
+ }
|
|
|
+
|
|
|
+ sample_context_t ctx;
|
|
|
+ ctx.cam_fmt = " ";
|
|
|
+ ctx.cam_devname = "/dev/video0";
|
|
|
+ ctx.cam_h = 720;
|
|
|
+ ctx.cam_w = 1280;
|
|
|
+ ctx.window_num = 2;
|
|
|
+ ctx.fps = 25;
|
|
|
+
|
|
|
+// if (!parse_cmdline(&ctx, argc, argv)) {
|
|
|
+// return -1;
|
|
|
+// }
|
|
|
+
|
|
|
+ uint imgWidth{640};
|
|
|
+ uint imgHeight{360};
|
|
|
+ string imgFmt = {"ABGR32"};
|
|
|
+// string imgFmt = {"YUYV"};
|
|
|
+ miivii::MvGmslCamera mvcam(ctx.cam_devname, ctx.window_num, ctx.cam_w, ctx.cam_h, ctx.fps, imgWidth, imgHeight,
|
|
|
+ imgFmt);
|
|
|
+
|
|
|
+ bool group_a_key(true), group_b_key(false);
|
|
|
+ std::cout << "Camera type in group A is " << mvcam.GetCameraType(group_a_key) << std::endl;
|
|
|
+ std::cout << "Camera type in group B is " << mvcam.GetCameraType(group_b_key) << std::endl;
|
|
|
+
|
|
|
+ std::string windowName("DisplayCamera ");
|
|
|
+ for (uint32_t i = 0; i < ctx.window_num; i++) {
|
|
|
+ cv::namedWindow(windowName + std::to_string(i), cv::WindowFlags::WINDOW_AUTOSIZE);
|
|
|
+ cv::moveWindow(windowName + std::to_string(i), 200 * i, 200 * i);
|
|
|
+ }
|
|
|
+ cv::Mat outMat[ctx.window_num];
|
|
|
+ uint8_t *outbuf[ctx.window_num];
|
|
|
+ cv::Mat imgbuf[ctx.window_num];
|
|
|
+ cv::Mat omat;
|
|
|
+ signal(SIGINT, &handler);
|
|
|
+ bool quit = false;
|
|
|
+ uint64_t timestamp{};
|
|
|
+ while (!quit) {
|
|
|
+ if (exitRequested) {
|
|
|
+ quit = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ qint64 ncomptime = 0;
|
|
|
+ qint64 npicsize;
|
|
|
+ // qDebug("before is %ld",QDateTime::currentMSecsSinceEpoch());
|
|
|
+ if (mvcam.GetImageCvMat(outMat, timestamp)) {
|
|
|
+ // qDebug(" get is %ld",QDateTime::currentMSecsSinceEpoch());
|
|
|
+
|
|
|
+ for (i = 0; i < ctx.window_num; i++) {
|
|
|
+
|
|
|
+ if (imgFmt == "YUYV") {
|
|
|
+ cv::cvtColor(outMat[i], outMat[i], cv::COLOR_YUV2BGR_YUYV);
|
|
|
+ cv::imshow(windowName + std::to_string(i), outMat[i]);
|
|
|
+ } else if (imgFmt == "ABGR32") {
|
|
|
+ qint64 nPicTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
+ cv::cvtColor(outMat[i], outMat[i], cv::COLOR_RGBA2BGR);
|
|
|
+ cv::imshow(windowName + std::to_string(i), outMat[i]);
|
|
|
+
|
|
|
+ std::cout<<" pic time is : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
|
|
|
+
|
|
|
+
|
|
|
+ gMutex[i].lock();
|
|
|
+ gTimePic[i] = nPicTime;
|
|
|
+ gMat[i] = outMat[i].clone();
|
|
|
+ gbUpdate[i] = true;
|
|
|
+ gMutex[i].unlock();
|
|
|
+ gwc[i].wakeAll();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // qDebug(" end is %ld",QDateTime::currentMSecsSinceEpoch());
|
|
|
+ // std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
|
+ std::cout << "Timestamp from GetImageCvMat: " << timestamp << " comp time: "<<ncomptime<<" size is "<<npicsize<<std::endl;
|
|
|
+ } else {
|
|
|
+ std::cerr << "Can't get image form camera." << std::endl;
|
|
|
+ }
|
|
|
+ if (cv::waitKey(1) == 27) {// Wait for 'esc' key press to exit
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // }
|
|
|
+ /*use raw data to get image*/
|
|
|
+
|
|
|
+// qDebug("before raw is %ld",QDateTime::currentMSecsSinceEpoch());
|
|
|
+// if (mvcam.GetImagePtr(outbuf, timestamp)) {
|
|
|
+// qDebug(" get raw is %ld",QDateTime::currentMSecsSinceEpoch());
|
|
|
+// for (i = 0; i < ctx.window_num; i++) {
|
|
|
+// if (imgFmt == "YUYV") {
|
|
|
+// imgbuf[i] = cv::Mat(imgHeight, imgWidth, CV_8UC2, outbuf[i]);
|
|
|
+// cv::Mat mrgba(imgHeight, imgWidth, CV_8UC3);
|
|
|
+// cv::cvtColor(imgbuf[i], mrgba, cv::COLOR_YUV2BGR_YUYV);
|
|
|
+// cv::imshow(windowName + std::to_string(i), mrgba);
|
|
|
+// } else if (imgFmt == "ABGR32") {
|
|
|
+// imgbuf[i] = cv::Mat(imgHeight, imgWidth, CV_8UC4, outbuf[i]);
|
|
|
+// cv::cvtColor(imgbuf[i], imgbuf[i], cv::COLOR_RGBA2BGR);
|
|
|
+// cv::imshow(windowName + std::to_string(i), imgbuf[i]);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// qDebug(" end raw is %ld",QDateTime::currentMSecsSinceEpoch());
|
|
|
+// // std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
+// std::cout << "Timestamp from GetImagePtr : " << timestamp << std::endl;
|
|
|
+// } else {
|
|
|
+// std::cerr << "Can't get image form camera." << std::endl;
|
|
|
+// }
|
|
|
+
|
|
|
+// if (cv::waitKey(1) == 27) {// Wait for 'esc' key press to exit
|
|
|
+// break;
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ // return a.exec();
|
|
|
+}
|