我需要编写一个 Python 脚本来获取相机配置并检查 OSD 中写入的内容。我编写了直接从它们下载 XML 文件的代码,然后对其进行解析以找到库存 ID。
import xml.etree.ElementTree as ET
first_config = """
<VideoOverlay xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
<normalizedScreenSize>
<normalizedScreenWidth>704</normalizedScreenWidth>
<normalizedScreenHeight>576</normalizedScreenHeight>
</normalizedScreenSize>
<attribute>
<transparent>false</transparent>
<flashing>false</flashing>
</attribute>
<TextOverlayList size="4">
<TextOverlay>
<id>1</id>
<enabled>true</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText>Inv: 1234513454321</displayText>
<directAngle/>
</TextOverlay>
</TextOverlayList>
<DateTimeOverlay>
<enabled>true</enabled>
<positionX>552</positionX>
<positionY>560</positionY>
<dateStyle>DD-MM-YYYY</dateStyle>
<timeStyle>24hour</timeStyle>
<displayWeek>false</displayWeek>
</DateTimeOverlay>
<channelNameOverlay>
<enabled>true</enabled>
<positionX>553</positionX>
<positionY>48</positionY>
</channelNameOverlay>
<fontSize>48*48</fontSize>
<frontColorMode>auto</frontColorMode>
<frontColor>000000</frontColor>
<alignment>customize</alignment>
<boundary>1</boundary>
</VideoOverlay>
"""
second_config = """
<VideoOverlay xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
<normalizedScreenSize>
<normalizedScreenWidth>704</normalizedScreenWidth>
<normalizedScreenHeight>576</normalizedScreenHeight>
</normalizedScreenSize>
<attribute>
<transparent>false</transparent>
<flashing>false</flashing>
</attribute>
<fontSize>48*48</fontSize>
<TextOverlayList size="8">
<TextOverlay>
<id>1</id>
<enabled>true</enabled>
<positionX>3</positionX>
<positionY>48</positionY>
<displayText>Inv: 100000000</displayText>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>2</id>
<enabled>true</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText>GEO:0000000000</displayText>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>3</id>
<enabled>true</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText>Serial:000000000</displayText>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>4</id>
<enabled>true</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText>MODEL:0000000000000</displayText>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>5</id>
<enabled>false</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText/>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>6</id>
<enabled>false</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText/>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>7</id>
<enabled>false</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText/>
<isPersistentText>true</isPersistentText>
</TextOverlay>
<TextOverlay>
<id>8</id>
<enabled>false</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<displayText/>
<isPersistentText>true</isPersistentText>
</TextOverlay>
</TextOverlayList>
<DateTimeOverlay>
<enabled>true</enabled>
<positionX>0</positionX>
<positionY>576</positionY>
<dateStyle>DD-MM-YYYY</dateStyle>
<timeStyle>24hour</timeStyle>
<displayWeek>false</displayWeek>
</DateTimeOverlay>
<channelNameOverlay xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
<enabled>true</enabled>
<positionX>581</positionX>
<positionY>48</positionY>
</channelNameOverlay>
<frontColorMode>auto</frontColorMode>
<frontColor>000000</frontColor>
<alignment>customize</alignment>
<boundary>1</boundary>
<upDownboundary>0</upDownboundary>
<leftRightboundary>0</leftRightboundary>
</VideoOverlay>
"""
xml_string = first_config
root = ET.fromstring(xml_string)
print(root.tag)
osd_list_size = root[2].attrib['size']
print(osd_list_size) #size of the list of OSD lines
for child in root[2]:
print(child[4].text)
xml_string = second_config
root_b = ET.fromstring(xml_string)
print(root_b.tag)
osd_list_size = root_b[2].attrib['size']
print(osd_list_size) #size of the list of OSD lines
for child in root_b[2]:
print(child[4].text)
有什么想法为什么会这样以及我该如何解决它?
Traceback (most recent call last):
File "C:\Users\S26-tech-Dmt\PycharmProjects\pythonProject1\Test.py", line 159, in <module>
osd_list_size = root_b[2].attrib['size']
~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'size'
不幸的是,每当我尝试解析 secondary_config 时,它都会返回空的根元素,其中没有任何内容,因为它表示属性“size”不存在。知道可能是什么问题以及如何解决它吗?
TextOverlayList
元素在两个 XML 文件中的位置不同,因此您不应访问它
按位置。使用 find()
按名称搜索元素:
import xml.etree.ElementTree as ET
def parse_config(xml_string):
root = ET.fromstring(xml_string)
print(root.tag)
uri = 'http://www.hikvision.com/ver20/XMLSchema'
text_overlay_list = root.find('{%s}TextOverlayList' % uri)
osd_list_size = text_overlay_list.attrib['size']
print(osd_list_size) #size of the list of OSD lines
for child in text_overlay_list:
display_text = child.find('{%s}displayText' % uri)
print(display_text.text)
第二个文件调用时的输出:
{http://www.hikvision.com/ver20/XMLSchema}VideoOverlay
8
Inv: 100000000
GEO:0000000000
Serial:000000000
MODEL:0000000000000
None
None
None
None