清理文件,记录一下共享内存的代码。后面再解析理解。
server.pyimport mmap
import contextlib
import time
import zeep
import numpy as np
import cv2
import time
from onvif import ONVIFCamera
import requests
from requests.auth import HTTPDigestAuth
with open("test.dat", "w") as f:
f.write('x00' * 1024)
def zeep_pythonvalue(self, xmlvalue):
return xmlvalue
# with open('test.dat', 'r+') as f:
# with contextlib.closing(mmap.mmap(f.fileno(), 1024, access=mmap.ACCESS_WRITE)) as m:
# for i in range(1, 10001):
# m.seek(0)
# s = ("msg " + str(i)).encode()
# # s=s.rjust(1024, 'x00').encode()
# print(s)
# m.write(s)
# m.flush()
# time.sleep(1)
class onvifCamera(object):
def __init__(self, ip: str, username: str, password: str):
self.ip = ip
self.username = username
self.password = password
zeep.xsd.simple.AnySimpleType.pythonvalue = zeep_pythonvalue
self.init_ipc()
def init_ipc(self):
try:
self.mycam = ONVIFCamera(self.ip, 80, self.username, self.password)
self.media = self.mycam.create_media_service() # 创建媒体服务
self.media_profile = self.media.GetProfiles()[0] # 获取配置信息
# self.ptz = self.mycam.create_ptz_service() # 创建控制台服务
# print(self.media_profile)
return True
except Exception as e:
print(e)
return False
def Snapshot(self):
"""
截图
:return:
"""
res = self.media.GetSnapshotUri({'ProfileToken': self.media_profile.token})
response = requests.get(res.Uri, auth=HTTPDigestAuth(self.username, self.password))
print(len(response.content))
return response.content
# with open(self.save_path, 'wb') as f: # 保存截图
# f.write(response.content)
camera0 = onvifCamera('192.168.8.153','admin','tensorsight666')
with open('test.dat', 'w+') as f:
with contextlib.closing(mmap.mmap(f.fileno(), 1024*1024, access=mmap.ACCESS_WRITE)) as m:
while True:
m.seek(0)
# s = ("msg " + str(i)).encode()
image_buff = camera0.Snapshot()
s=image_buff
# s=s.rjust(1024, 'x00').encode()
# print(s)
image = cv2.imdecode(np.fromstring(s, np.uint8), 1)
cv2.imshow('cv0', image)
cv2.waitKey(1)
m.write(s)
m.flush()
time.sleep(1)
# image_buff =camera0.Snapshot()
client.py
# -*- coding:utf-8 -*-
# SodrSnne's PYTHON
# @Time : 2021/2/20 17:29
# @Email : jisashandianmao@163.com
# @File : client.py
# @Software: PyCharm
_author_ = 'SodrSnne'
import mmap
import contextlib
import time
import mmap
import contextlib
import time
import numpy as np
import cv2
while True:
with open(r'E:Business_projectsonvifsctrlcamrera192.168.8.153data.dat', 'r') as f:
with contextlib.closing(mmap.mmap(f.fileno(), 1024*312, access=mmap.ACCESS_READ)) as m:
s = m.read(1024*1000)#.replace('x00', '')
image = cv2.imdecode(np.frombuffer(s, np.uint8), 1)
cv2.imshow('cv', image)
cv2.waitKey(1)
# print(s)
time.sleep(1)



