Python连接海康摄像头并获取摄像头信息

安装海康摄像头插件 hikvisionapi

pip install hikvisionapi

使用海康插件连接摄像头,并且获取设备信息

from hikvisionapi import Client
client = Client('http://192.168.0.1', login='admin', password='password')
response = client.System.deviceInfo(method='get')  # 获取摄像头信息
if response['DeviceInfo']['deviceDescription'] == 'IPCamera':
	channel_name = client.System.Video.inputs.channels(method='get')
	channel_name = channel_name['VideoInputChannelList']['VideoInputChannel']['name']  # 获取通道名称

延伸一下,增加判断摄像头是否在线,在线时才获取设备信息。


from hikvisionapi import Client
import socket

# 判断IP是否在线
def is_ip_reachable(ip, port=80, timeout=1):
	"""
	尝试连接到指定的IP地址和端口,以判断该IP是否可达。
	:param ip: 要检查的IP地址
	:param port: 要连接的端口号,默认为80
	:param timeout: 连接超时时间,默认为1秒
	:return: 如果IP可达,返回True;否则返回False
	"""
	try:
		# 创建一个socket对象
		with socket.create_connection((ip, port), timeout):
			return True
	except (socket.error, ConnectionRefusedError, socket.timeout):
		# 如果连接失败,则认为IP不可达
		return False


# 获取设备信息
def get_shannel(ip):
	# 创建一个Client对象,传入设备的IP地址、用户名和密码
	client = Client('http://' + ip, login='admin', password='password')
	response = client.System.deviceInfo(method='get')  # 获取摄像头信息
	# 判断设备类型是否为IPCamera
	if response['DeviceInfo']['deviceDescription'] == 'IPCamera':
		channel_name = client.System.Video.inputs.channels(method='get')
		channel_name = channel_name['VideoInputChannelList']['VideoInputChannel']['name']
		channal_data = {
			'ip'    : ip,
			'name'  : channel_name,
			'type'  : response['DeviceInfo']['model'],
			'mac'   : response['DeviceInfo']['macAddress'],
			'number': response['DeviceInfo']['serialNumber'],
		}
		return channal_data
	else:
		print("不是摄像机")


channel_list = []
# 循环获取所有摄像头信息
for i in range(1, 254):
	ip = f"192.168.1.{i}"
	if is_ip_reachable(ip):
		data = get_shannel(ip)
		if data != False:
			channel_list.append(data)
	else:
		print(f"{ip} 不可达。")
print(channel_list)

作者:笑看人生163

物联沃分享整理
物联沃-IOTWORD物联网 » Python连接海康摄像头并获取摄像头信息

发表回复