Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Camera::stream() {
- /* This function is meant to run on a separate thread and fill up the buffer independantly of
- main stream thread */
- //cv::setNumThreads(100);
- /* Rules for these slightly changed! */
- Mat pre; // Grab initial undoctored frame
- //pre = Mat::zeros(size, CV_8UC1);
- Mat frame; // Final modified frame
- frame = Mat::zeros(size, CV_8UC1);
- if (!pre.isContinuous()) pre = pre.clone();
- ipCam.open(streamUrl, CAP_FFMPEG);
- while (ipCam.isOpened() && capture) {
- // If camera is opened wel need to capture and process the frame
- try {
- auto start = std::chrono::system_clock::now();
- ipCam >> pre;
- if (pre.empty()) {
- /* Check for blank frame, return error if there is a blank frame*/
- cerr << id << ": ERROR! blank frame grabbedn";
- for (FrameListener* i : clients) {
- i->onNotification(1); // Notify clients about this shit
- }
- break;
- }
- else {
- // Only continue if frame not empty
- if (pre.cols != size.width && pre.rows != size.height) {
- resize(pre, frame, size);
- pre.release();
- }
- else {
- frame = pre;
- }
- dPacket* pack = new dPacket{id,&frame};
- for (auto i : clients) {
- i->onPNewFrame(pack);
- }
- frame.release();
- delete pack;
- }
- }
- catch (int e) {
- cout << endl << "-----Exception during capture process! CODE " << e << endl;
- }
- // End camera manipulations
- }
- cout << "Camera timed out, or connection is closed..." << endl;
- if (tryResetConnection) {
- cout << "Reconnection flag is set, retrying after 3 seconds..." << endl;
- for (FrameListener* i : clients) {
- i->onNotification(-1); // Notify clients about this shit
- }
- this_thread::sleep_for(chrono::milliseconds(3000));
- stream();
- }
- return true;
- }
- void GLWidget::onPNewFrame(dPacket* inPack) {
- lastFlag = 0;
- if (bufferEnabled) {
- buffer.push(QPixmap::fromImage(toQImageFromPMat(inPack->frame)));
- }
- else {
- if (playing) {
- /* Only process if this widget is playing */
- frameProcessing = true;
- lastImage.convertFromImage(toQImageFromPMat(inPack->frame));
- frameProcessing = false;
- }
- }
- if (lastFlag != -1 && !lastImage.isNull()) {
- connecting = false;
- }
- else {
- connecting = true;
- }
- }
- QImage GLWidget::toQImageFromPMat(cv::Mat* mat) {
- return QImage(mat->data, mat->cols, mat->rows, QImage::Format_RGB888).rgbSwapped();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement