ivpicsave.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "ivpicsave.h"
  2. #include <QDateTime>
  3. #include <opencv2/opencv.hpp>
  4. #include <opencv2/core.hpp>
  5. #ifdef NVIDIA_AGX
  6. #include <opencv2/imgcodecs/legacy/constants_c.h> //OpenCV4 use this line
  7. #include <opencv2/imgproc/types_c.h> //OpenCV4 use this line
  8. #endif
  9. ivpicsave::ivpicsave(QString strdir,QString strsuffix,std::string strvin)
  10. {
  11. mstrdir = strdir;
  12. mstrsuffix = strsuffix;
  13. mstrvin = strvin;
  14. mnWriteIndex = 0;
  15. }
  16. void ivpicsave::SetPic(iv::vision::rawpic pic)
  17. {
  18. if((mbGetPicSize == true ) && (mbSave == false))
  19. {
  20. return;
  21. }
  22. mMutex.lock();
  23. mrawpic.CopyFrom(pic);
  24. mnWriteIndex++;
  25. mMutex.unlock();
  26. }
  27. void ivpicsave::run()
  28. {
  29. int nReadIndex = 0;
  30. std::vector<iv::vision::rawpic> xvectorpic;
  31. bool bgetfps = false;
  32. double ffps = 30;
  33. while(!QThread::isInterruptionRequested())
  34. {
  35. if((mbSave == false)&&(bgetfps))
  36. {
  37. msleep(100);
  38. continue;
  39. }
  40. bool bNew = false;
  41. iv::vision::rawpic pic;
  42. if(mnWriteIndex != nReadIndex)
  43. {
  44. mMutex.lock();
  45. pic.CopyFrom(mrawpic);
  46. mMutex.unlock();
  47. if(bgetfps == false)
  48. {
  49. xvectorpic.push_back(pic);
  50. if(xvectorpic.size()>=10)
  51. {
  52. std::vector<qint64> xtimediff;
  53. int j;
  54. for(j=1;j<xvectorpic.size();j++)
  55. {
  56. qint64 diff = xvectorpic[j].time() - xvectorpic[j-1].time();
  57. xtimediff.push_back(diff);
  58. }
  59. qint64 totaldiff = 0;
  60. for(j=0;j<xtimediff.size();j++)
  61. {
  62. totaldiff = totaldiff + xtimediff[j];
  63. }
  64. qint64 avgdiff = totaldiff/xtimediff.size();
  65. ffps = avgdiff;
  66. if(ffps<1.0)ffps =1.0;
  67. if(ffps>1000.0)ffps = 1000.0;
  68. ffps = 1000.0/ffps;
  69. mMutexSave.lock();
  70. mfps = ffps;
  71. mnpicheight = xvectorpic[0].height();
  72. mnpicwidth = xvectorpic[0].width();
  73. mMutexSave.unlock();
  74. mbGetPicSize = true;
  75. bgetfps = true;
  76. xvectorpic.clear();
  77. xtimediff.clear();
  78. }
  79. }
  80. nReadIndex++;
  81. bNew = true;
  82. }
  83. if(mbSave == false)
  84. {
  85. msleep(1);
  86. continue;
  87. }
  88. if(bNew)
  89. {
  90. cv::Mat mat(pic.height(),pic.width(),pic.mattype());
  91. if(mbSave)
  92. {
  93. if(pic.type() == 1)
  94. memcpy(mat.data,pic.picdata().data(),mat.rows*mat.cols*mat.elemSize());
  95. else
  96. {
  97. std::vector<unsigned char> buff(pic.picdata().data(),pic.picdata().data()+pic.picdata().size());
  98. mat = cv::imdecode(buff,CV_LOAD_IMAGE_COLOR);
  99. }
  100. int font = cv::FONT_HERSHEY_DUPLEX;
  101. QDateTime xrecvtime = QDateTime::fromMSecsSinceEpoch(pic.time());
  102. // char strtext[256];
  103. // snprintf(strtext,"%s",xrecvtime.toString("yyyy-MM-dd hh:mm:ss:zzz").toLatin1().data());
  104. std::string strtext = xrecvtime.toString("yyyy-MM-dd hh:mm:ss:zzz").toStdString();
  105. double fratio = pic.width()/640;
  106. int fontsize = 0.5*fratio;
  107. if(fontsize<1)fontsize = 1;
  108. cv::putText(mat,strtext,cv::Point(pic.width() - 230*fratio,30*fratio),font,0.5*fratio,cv::Scalar(0,255,0),fontsize);
  109. cv::putText(mat,mstrvin,cv::Point(10,30*fratio),font,0.5*fratio,cv::Scalar(0,255,0),fontsize);
  110. }
  111. mMutexSave.lock();
  112. if(mbSave)
  113. {
  114. mWriter<<mat;
  115. }
  116. mMutexSave.unlock();
  117. }
  118. msleep(10);
  119. }
  120. }
  121. void ivpicsave::startsave()
  122. {
  123. std::string strfilename;
  124. strfilename = mstrdir.toStdString() + QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss-zzz").toStdString() +"-" + mstrsuffix.toStdString() +".avi";
  125. mMutexSave.lock();
  126. mWriter.open(strfilename,cv::VideoWriter::fourcc('M','P','4','2'),10.0,cv::Size(mnpicwidth,mnpicheight),1);
  127. if(mWriter.isOpened())
  128. {
  129. mbSave = true;
  130. }
  131. mMutexSave.unlock();
  132. }
  133. void ivpicsave::stopsave()
  134. {
  135. if(mbSave)
  136. {
  137. mMutexSave.lock();
  138. mbSave = false;
  139. mWriter.release();
  140. mMutexSave.unlock();
  141. }
  142. }