python - 使用python连接到海康威视摄像头并打开cv

标签 python opencv image-processing ip-camera hikvision

我想用 python 连接 Hikvision 网络摄像机并使用此代码打开 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()

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

    Traceback (most recent call last):
  File "C:\Users\Amin\Desktop\ip camera in py\csm.py", line 15, in <module>
    cv2.imshow('frame',ret)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__cdecl cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 6 (CV_64F)

我用 VLCPlayer 测试了我的相机,它运行完美!

我认为问题与 opencv4 有关!

我该如何解决?非常感谢

最佳答案

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

cv2.imshow('frame',ret)

所以你应该通过框架:

cv2.imshow('frame',frame)
Python 中的

cap.read() 返回两个值,一个是Boolean,表示是否成功读取帧,另一个是第二个是框架本身。因此,您应该检查 ret 是否为 true,然后显示框架。

关于python - 使用python连接到海康威视摄像头并打开cv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54970830/

相关文章:

python - 当 as_index=False 时,groupby.first、groupby.nth、groupby.head 之间有什么不同

python - Python 中的 OpenCV : "ImportError: DLL load failed"

Python - 在 Mac 上安装 opencv 时遇到问题(几个月前 opencv 运行良好之后)

C++ OpenCV 从点 vector 获取边界框

python - 比较使用 ImageDataGenerator() 和 cv2.imread() 加载的数据

python - 如何将 jpeg 数据加载、标记和馈送到 Tensorflow 中?

python - QStyledItemDelegate 水平截断文本并且不添加水平滚动条

python - Python 中的单元测试装置?

python - 如何使用 requests 捕获发布请求的结果页面?

c++ - 什么算法会混合具有相同场景的多张图像,除了每张图像中不同位置的一个对象?