当前位置: 首页 > 知识库问答 >
问题:

在python中获取到海康威视IP摄像机的帧

冯野
2023-03-14

我有下一台海康威视IP摄像机http://www.Hikvision.com/es/products_accessries_161_i11969.html我在本地网络中配置了它,并且与浏览器连接,我可以看到摄像机工作得很好。我曾在不同的帖子中寻找像这样的镜头,但我做不到。这里我放了我用来尝试它的代码和响应。我想获得一个框架,并保存在我的去库存后处理。

import cv2
import requests
import numpy as np
import urllib
import hikvision.api
import requests
import shutil

#Donwload from: https://github.com/fbradyirl/hikvision/archive/master.zip

hik_camera = hikvision.api.CreateDevice('192.168.1.64', username='admin', password=contrasenia)
"""
INFO:hikvision.api:Initialising new hikvision camera client
INFO:hikvision.api:motion_url: http://192.168.1.64/MotionDetection/1
INFO:hikvision.api:ElementTree.register_namespace: http://www.hikvision.com/ver10/XMLSchema
INFO:hikvision.api:Going to probe device to test connection
INFO:hikvision.api:url: http://192.168.1.64/System/deviceInfo
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.1.64
DEBUG:requests.packages.urllib3.connectionpool:"GET /System/deviceInfo HTTP/1.1" 200 696
DEBUG:hikvision.api:response: <Response [200]>
DEBUG:hikvision.api:status_code 200
DEBUG:hikvision.api:element_to_query: .//{http://www.hikvision.com/ver10/XMLSchema}firmwareVersion result: <Element '{http://www.hikvision.com/ver10/XMLSchema}firmwareVersion' at 0x7fb090b76cc8>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.1.64
DEBUG:requests.packages.urllib3.connectionpool:"GET /MotionDetection/1 HTTP/1.1" 200 407
DEBUG:hikvision.api:Response: <?xml version="1.0" encoding="UTF-8"?>
<MotionDetection version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
<id>1</id>
<enabled>false</enabled>
<regionType>grid</regionType>
<Grid>
<rowGranularity>18</rowGranularity>
<columnGranularity>22</columnGranularity>
</Grid>
<MotionDetectionRegionList>
<sensitivityLevel>0</sensitivityLevel>
</MotionDetectionRegionList>
</MotionDetection>

INFO:hikvision.api:Current motion detection state? enabled: false
WARNING:hikvision.api:sensitivityLevel is 0.
INFO:hikvision.api:sensitivityLevel now set to 1
INFO:hikvision.api:Connected OK!
INFO:hikvision.api:Camera firmaward version: V5.4.3
INFO:hikvision.api:Motion Detection enabled: False
"""

#The method:
"""
hik_camera.__class__(                    hik_camera.__setattr__(
hik_camera.__delattr__(                  hik_camera.__sizeof__(
hik_camera.__dict__                      hik_camera.__str__(
hik_camera.__dir__(                      hik_camera.__subclasshook__(
hik_camera.__doc__                       hik_camera.__weakref__
hik_camera.__eq__(                       hik_camera._base
hik_camera.__format__(                   hik_camera._host
hik_camera.__ge__(                       hik_camera._password
hik_camera.__getattribute__(             hik_camera._sensitivity_level
hik_camera.__gt__(                       hik_camera._username
hik_camera.__hash__(                     hik_camera._xml_namespace
hik_camera.__init__(                     hik_camera.disable_motion_detection(
hik_camera.__le__(                       hik_camera.enable_motion_detection(
hik_camera.__lt__(                       hik_camera.get_about(
hik_camera.__module__                    hik_camera.get_version(
hik_camera.__ne__(                       hik_camera.is_motion_detection_enabled(
hik_camera.__new__(                      hik_camera.motion_url
hik_camera.__reduce__(                   hik_camera.put_motion_detection_xml(
hik_camera.__reduce_ex__(                hik_camera.xml_motion_detection_off
hik_camera.__repr__(                     hik_camera.xml_motion_detection_on
"""
hik_camera
#<hikvision.api.CreateDevice object at 0x7fb09183ab38>

用这个代码我不能获得一个帧。

camera = requests.get('http://192.168.1.64', auth=('admin', contrasenia), stream=True)
#INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.1.64
#DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 480
chunk = camera.iter_content(chunk_size=1024)
chunk
#<generator object iter_slices at 0x7fb090b7a150>
bytes += bytes()
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#TypeError: unsupported operand type(s) for +=: 'type' and 'bytes'
stream = urllib.request.urlopen("http://192.168.1.64")
#<http.client.HTTPResponse object at 0x7fb090b82320>
out_cv2 = cv2.VideoCapture('http://192.168.1.64')
#<VideoCapture 0x7fb090b6dc10>
cv2.imshow(out_cv2)
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#TypeError: Required argument 'mat' (pos 2) not found
ret, frame = out_cv2.read()
cv2.imshow('out', frame)
#OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /io/opencv/modules/highgui/src/window.cpp, line 304
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#cv2.error: /io/opencv/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow
ret
#False
frame
#
from requests.auth import HTTPBasicAuth
requ = requests.get('http://192.168.1.64', auth=HTTPBasicAuth('admin', contrasenia), stream=True)
out_file = open('img.png', 'wb')
shutil.copyfileobj(requ.raw, out_file)
#The file is empty

这段代码来自其他有类似问题的帖子,但我不能用它来获得我的对象。我能做什么?

共有1个答案

姬欣怡
2023-03-14

转到您的相机设置(网络配置),并检查de rtsp端口,在我的情况下是555。

我有一个海康威视DS-2CD2D14WD,在我的情况下,我必须使用以下:

rtsp:/yourusername:yourpassword@172.16.30.248:555/streaming/channels/2/

示例:

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()
 类似资料:
  • 我在从远程IP摄像头中抓取帧时遇到了问题。我的雇主希望在C#.NET(用于Windows)中完成,如果可能的话,使用轻量级的解决方案,即不使用任何庞大的框架。 该设备型号为DS-2CD2632F-I,它目前连接到我的局域网,相机的网络界面工作非常好。 我已经试用了几个流行的框架,例如AForge、EmguCV、OzekiSDK和directshow.net,但它们似乎都不起作用。特别是OzekiS

  • null 我如何向摄像机发送重置请求?或者有没有我们可以与HTTP get post方法一起使用的命令列表?有海康威视CGI的名单吗?和Onvif命令来重置相机? 任何帮助都是感激的

  • 技术支持工程师-医疗方向-杭州 时间线: 9.04:测评 9.06:笔试 9.21:一面 1、一面(钉钉)(第一个面试) 1.01 自我介绍 1.02 问项目 1.03 项目中的主要工作 1.04 最有成就感的事情,详细说下 1.05 当班长的体会 1.06 对这个岗位的了解 1.07 课余爱好 1.08 长期坚持的爱好 1.09 为什么选这个城市 1.10 反问

  • 刚刚面的海康威视。找的是Java开发。一进是线下面的。然后他开始也不问,我技术啊,他就问我一些七七八八的学校做什么,你怎么自学的?你觉得你可算了多少嗯什么你最精通的什么框架?笑死了,我说我就会一个是公布的,其他不是很熟练。他说他这边要求必须精通三个以上的框架。真**笑死我了。然后说什么妮蔻,你这才刷至少得刷200道题啊?然后就是说你技术上不行。然后问我练了多少家公司了,怎么有没有考虑,为什么没有通

  • 我有一个海康威视IP摄像机内置的人计数应用程序。我正在尝试构建一个桌面/Web应用程序,它读取通过的人数并对其进行处理。我只想知道我怎样才能知道这些人的数量,不管用什么编程语言。相机型号为海康威视IDS-2CD6412FWD/C。任何回答都很重要。谢谢你。

  • 项目遇到的难点 项目有哪些功能 vue中的指令 computed使用场景 js深浅拷贝 路由跳转方法 如果让你设计一个任务面板,你会实现哪些功能 被突然来的电话吓到了 我和面试官都在咳 有些忘了,感觉没戏答的不怎么好