栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 共享内存

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 共享内存

清理文件,记录一下共享内存的代码。后面再解析理解。

server.py
import 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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1030674.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号