cv2.videoCapture仅返回“uri中缺少端口”

2024-07-03 05:52:33 发布

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

首先,感谢您的关注。 我尝试使用opencv和ip摄像头进行流式传输,但它只返回“[tcp@00000 1d5fce13580]uri中缺少的端口”。我已经在VLC上进行了测试,也用JavaCV进行了测试,这两种工具都可以使用。我已经检查了MPEG,没问题。有没有人经历过这件事,可以帮忙

我赞成:

  • WIN10
  • OpenCV 4.5.1

代码:

import numpy as np
import cv2


def runCam():
    print(cv2.getBuildInformation())
    video_src = "rtsp://admin:myPWCam@192.168.1.223:554/Streaming/channels/1/"
    cap = cv2.VideoCapture(video_src, cv2.CAP_FFMPEG)

    while True:
        ret, frame = cap.read()
        try:
            cv2.resizeWindow('Stream IP Camera OpenCV', 120300, 800)
            cv2.imshow('Stream IP Camera OpenCV', frame)
        except Exception as ex:
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
            break

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

    cap.release()
    cv2.destroyAllWindows()


runCam()

OPENCV构建信息:https://gist.github.com/nathancn/745f543e3a6cd13d012855759bd0940d

我使用海康威视IP CAM型号DS-2CD2621G0-IS

返回的错误: enter image description here


Tags: importipsrcstreamasvideotemplatecv2
2条回答

如果您的密码包含像!"#$%&'()*+,-./:;<=>?@[\]^_{|}~这样的字符,那么问题可能出在OpenCV库的cv2.VideoCapture()

您可以尝试使用imutils库来解决此问题。这对我很有用

from imutils.video import VideoStream
cap = VideoStream(video_src).start()

因此,您的代码可能如下所示:

import numpy as np
import cv2
from imutils.video import VideoStream

def runCam():
    print(cv2.getBuildInformation())
    video_src = "rtsp://admin:myPWCam@192.168.1.223:554/Streaming/channels/1/"
    cap = VideoStream(video_src).start()

    while True:
        frame = cap.read()
        try:
            cv2.resizeWindow('Stream IP Camera OpenCV', 120300, 800)
            cv2.imshow('Stream IP Camera OpenCV', frame)
        except Exception as ex:
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
            break

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

    cap.release()
    cv2.destroyAllWindows()

runCam()

已解决

问题在于我的密码,由于某种原因,当密码包含“#”时,opencv的视频捕获会变得混乱。当我在javacv和VLC中测试它时,它工作了,我没有用另一个密码测试它,浪费了很多时间:(但现在我发现我更改了密码,它已经工作了。我希望它能帮助那些有同样问题的人意识到特殊的密码字符

相关问题 更多 >