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()