0

I'm trying to read my webcam with OpenCV in Qt, it's IP looks like http://10.147.17.208:8963/. But it keeps getting Error:

Assertion failed (!_src.empty()) in cvtColor.

I tried use Py OpenCV to read it, it works. Here's the code in Qt, it dosen't work

void MainWindow::on_cameraButton_toggled(bool checked)
{
    if(checked){
        cap.open("http://10.147.17.208:8963");
        timer->start(100);
        qDebug() << "Timer started";
    }else{
        timer->stop();
        cap.release();
        ui->camView->clear();
        qDebug() << "Timer stopped";
    }
}

void MainWindow::displayFrame()
{
    qDebug() << "Time out!";
    cap.read(frame);
    if(!cap.isOpened()) qDebug() << "Cap is not opened";
    cout << frame;
    QImage qim = MainWindow::convertMat2QImage(frame);
    ui->camView->setPixmap(QPixmap::fromImage(qim));
    qDebug() << "Displayed one frame!";
}

Here's the code in Python OpenCV, it works

import cv2
cap = cv2.VideoCapture('http://10.147.17.208:8963')
while True:
    ret, frame = cap.read()
    frame = cv2.flip(
            frame,
            1  # 1:水平镜像,-1:垂直镜像
        )
    cv2.imshow('frame', frame)
    # 这一步必须有,否则图像无法显示
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# 当一切完成时,释放捕获
cap.release()
cv2.destroyAllWindows()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
fuso
  • 1
  • 2
    don't suggest that then @JesperJuhl -- clearly some Mat is empty at some point. why are you not checking for that, or testing `if not ret: break`? why do you not abort when determining `!cap.isOpened()`? please take the [tour] and review [ask] – Christoph Rackwitz Aug 06 '22 at 14:33
  • Does this answer your question? [OpenCV VideoCapture and (-215:Assertion failed) !\_src.empty() in function 'cvtColor'](https://stackoverflow.com/questions/64526024/opencv-videocapture-and-215assertion-failed-src-empty-in-function-cvtco) – Christoph Rackwitz Aug 06 '22 at 14:33

0 Answers0