使用python打开并打开摄像头

2024-06-26 10:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将海康威视ip摄像机与python连接,并使用以下代码打开cv:

import numpy as np
import cv2

cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword@172.16.30.248:555/Streaming/channels/2/")

while(True):
     # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',ret)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

当我运行我的代码时,我遇到了这个错误:

^{pr2}$

我用VLCPlayer测试了我的相机,它工作得很好!在

我想问题出在opencv4!在

我怎样才能修好它?tnx很多


Tags: the代码importipnumpyreleaseasnp
1条回答
网友
1楼 · 发布于 2024-06-26 10:20:38

错误是在OpenCV的imshow()函数中传递BOOLEAN值,而不是Mat值:

cv2.imshow('frame',ret)

所以你应该传递帧:

^{pr2}$

cap.read()中的cap.read()返回两个值,一个是Boolean,它显示帧是否被成功读取,第二个值是帧本身。所以您应该检查ret如果它是true,然后显示框架。在

相关问题 更多 >