亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Python Linux 中使用內存映射文件

如何在 Python Linux 中使用內存映射文件

翻過高山走不出你 2023-06-20 13:52:21
正如我在 Python 文檔中看到的那樣,Linux 中的 Python 可以完全支持內存映射文件。然而,當我試圖將這個想法應用到我的應用程序中時。我無法運行示例。我的應用程序是將幀從 Python 文件(客戶端)發送到另一個 Python 文件(服務器)??蛻舸aimport mmapimport timeimport osimport cv2 as cvprint("Opening camera...")cap = cv.VideoCapture('/home/hunglv/Downloads/IMG_8442.MOV')mm = Nonetry:? ? while True:? ? ? ? ret, img = cap.read()? ? ? ? if not ret:? ? ? ? ? ? break? ? ? ? if mm is None:? ? ? ? ? ? mm = mmap.mmap(-1,img.size,mmap.MAP_SHARED, mmap.PROT_WRITE)? ? ? ? # write image? ? ? ? start = time.time()? ? ? ? buf = img.tobytes()? ? ? ? mm.seek(0)? ? ? ? mm.write(buf)? ? ? ? mm.flush()??? ? ? ? stop = time.time()? ? ? ? print("Writing Duration:", (stop - start) * 1000, "ms")except KeyboardInterrupt:? ? passprint("Closing resources")cap.release()mm.close()服務器代碼import mmapimport timeimport osimport cv2 as cvimport numpy as npshape = (1080, 1920, 3)n = np.prod(shape)mm = mmap.mmap(-1, n)while True:? ? # read image? ? print (mm)? ? start = time.perf_counter()? ? mm.seek(0)? ? buf = mm.read(12)? ? img = np.frombuffer(buf, dtype=np.uint8).reshape(shape)? ? stop = time.perf_counter()? ? print("Reading Duration:", (stop - start) * 1000, "ms")? ? cv.imshow("img", img)? ? key = cv.waitKey(1) & 0xFF? ? key = chr(key)? ? if key.lower() == "q":? ? ? ? breakcv.destroyAllWindows()mm.close()在服務器端,我將內存索引設置為 0,并嘗試從內存中讀取字節。但是,服務器似乎無法正確讀取客戶端的數據。[更新] 我試圖在服務器端讀出前 12 個字節。該值是恒定的,不再改變。b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'此外,隨機幀的前 12 個字節是b'\xf5\xff\xff\xf0\xfa\xfe\xdf\xe9\xed\xd2\xdc\xe0'
查看完整描述

1 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

首先,我找到了可能有效但它使用的示例tagName(客戶端和服務器相同),這意味著它僅適用于 Window:

python-mmap-ipc


接下來我找到了適用于 Linux 的代碼:

使用 mmap 在進程之間共享 Python 數據。

它在磁盤上創建真實文件,將其大小調整為圖像大小,然后使用它的fdinmmap()


我使用網絡攝像頭進行測試。

服務器

import mmap

import time

import os

import cv2


print("Opening camera...")


cap = cv2.VideoCapture(0)

#print(cap.get(cv.CAP_PROP_FRAME_WIDTH))? # 640

#print(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) # 480


shape = (480, 640, 3)

n = (480*640*3)


fd = os.open('/tmp/mmaptest', os.O_CREAT | os.O_TRUNC | os.O_RDWR)

#os.write(fd, b'\x00' * n)? # resize file

os.truncate(fd, n)? # resize file


mm = None

try:

? ? while True:

? ? ? ? ret, img = cap.read()

? ? ? ??

? ? ? ? if not ret:

? ? ? ? ? ? break

? ? ? ??

? ? ? ? if mm is None:

? ? ? ? ? ? mm = mmap.mmap(fd, n, mmap.MAP_SHARED, mmap.PROT_WRITE)? # it has to be only for writing


? ? ? ? # write image

? ? ? ? start = time.perf_counter()

? ? ? ??

? ? ? ? buf = img.tobytes()

? ? ? ? mm.seek(0)

? ? ? ? mm.write(buf)

? ? ? ? mm.flush()

? ? ? ??

? ? ? ? stop = time.perf_counter()


? ? ? ? print("Writing Duration:", (stop - start) * 1000, "ms")

except KeyboardInterrupt:

? ? pass


print("Closing resources")

cap.release()

mm.close()

客戶


import mmap

import time

import os

import cv2

import numpy as np


shape = (480, 640, 3)

n = (480*640*3)


fd = os.open('/tmp/mmaptest', os.O_RDONLY)


mm = mmap.mmap(fd, n, mmap.MAP_SHARED, mmap.PROT_READ)? # it has to be only for reading


while True:

? ? # read image

? ? start = time.perf_counter()

? ??

? ? mm.seek(0)

? ? buf = mm.read(n)

? ? img = np.frombuffer(buf, dtype=np.uint8).reshape(shape)

? ??

? ? stop = time.perf_counter()


? ? print("Reading Duration:", (stop - start) * 1000, "ms")


? ? cv2.imshow("img", img)

? ? key = cv2.waitKey(1) & 0xFF

? ? key = chr(key)

? ? if key.lower() == "q":

? ? ? ? break

? ??

cv2.destroyAllWindows()

mm.close()

順便說一句:可能mmap()與-1(不在磁盤上創建文件)可以與線程(或分叉)一起工作,因為它們共享相同的內存。


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號