SHARE
TWEET

Untitled

a guest Jun 18th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool Camera::stream() {
  2.     /* This function is meant to run on a separate thread and fill up the buffer independantly of
  3.     main stream thread */
  4.     //cv::setNumThreads(100);
  5.     /* Rules for these slightly changed! */
  6.     Mat pre;  // Grab initial undoctored frame
  7.     //pre = Mat::zeros(size, CV_8UC1);
  8.     Mat frame; // Final modified frame
  9.     frame = Mat::zeros(size, CV_8UC1);
  10.     if (!pre.isContinuous()) pre = pre.clone();
  11.  
  12.     ipCam.open(streamUrl, CAP_FFMPEG);
  13.  
  14.  
  15.     while (ipCam.isOpened() && capture) {
  16.         // If camera is opened wel need to capture and process the frame
  17.         try {
  18.             auto start = std::chrono::system_clock::now();
  19.  
  20.             ipCam >> pre;
  21.  
  22.             if (pre.empty()) {
  23.                 /* Check for blank frame, return error if there is a blank frame*/
  24.                 cerr << id << ": ERROR! blank frame grabbedn";
  25.                 for (FrameListener* i : clients) {
  26.                     i->onNotification(1); // Notify clients about this shit
  27.                 }
  28.                 break;
  29.             }
  30.  
  31.             else {
  32.                 // Only continue if frame not empty
  33.  
  34.                 if (pre.cols != size.width && pre.rows != size.height) {
  35.                     resize(pre, frame, size);
  36.                     pre.release();
  37.                 }
  38.                 else {
  39.                     frame = pre;
  40.                 }
  41.  
  42.                 dPacket* pack = new dPacket{id,&frame};
  43.                 for (auto i : clients) {
  44.                     i->onPNewFrame(pack);
  45.                 }
  46.                 frame.release();
  47.                 delete pack;
  48.             }
  49.         }
  50.  
  51.         catch (int e) {
  52.             cout << endl << "-----Exception during capture process! CODE " << e << endl;
  53.         }
  54.         // End camera manipulations
  55.     }
  56.  
  57.     cout << "Camera timed out, or connection is closed..." << endl;
  58.     if (tryResetConnection) {
  59.         cout << "Reconnection flag is set, retrying after 3 seconds..." << endl;
  60.         for (FrameListener* i : clients) {
  61.             i->onNotification(-1); // Notify clients about this shit
  62.         }
  63.         this_thread::sleep_for(chrono::milliseconds(3000));
  64.         stream();
  65.     }
  66.  
  67.     return true;
  68. }
  69.      
  70. void GLWidget::onPNewFrame(dPacket* inPack) {
  71.     lastFlag = 0;
  72.  
  73.     if (bufferEnabled) {
  74.         buffer.push(QPixmap::fromImage(toQImageFromPMat(inPack->frame)));
  75.     }
  76.     else {
  77.         if (playing) {
  78.             /* Only process if this widget is playing */
  79.             frameProcessing = true;
  80.             lastImage.convertFromImage(toQImageFromPMat(inPack->frame));
  81.             frameProcessing = false;
  82.         }
  83.     }
  84.  
  85.     if (lastFlag != -1 && !lastImage.isNull()) {
  86.         connecting = false;
  87.     }
  88.     else {
  89.         connecting = true;
  90.     }
  91. }
  92.      
  93. QImage GLWidget::toQImageFromPMat(cv::Mat* mat) {
  94.  
  95.  
  96.  
  97.     return QImage(mat->data, mat->cols, mat->rows, QImage::Format_RGB888).rgbSwapped();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top