第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Python Linux 中使用內(nèi)存映射文件

如何在 Python Linux 中使用內(nèi)存映射文件

翻過高山走不出你 2023-06-20 13:52:21
正如我在 Python 文檔中看到的那樣,Linux 中的 Python 可以完全支持內(nèi)存映射文件。然而,當(dāng)我試圖將這個(gè)想法應(yīng)用到我的應(yīng)用程序中時(shí)。我無法運(yùn)行示例。我的應(yīng)用程序是將幀從 Python 文件(客戶端)發(fā)送到另一個(gè) Python 文件(服務(wù)器)。客戶代碼import 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()服務(wù)器代碼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()在服務(wù)器端,我將內(nèi)存索引設(shè)置為 0,并嘗試從內(nèi)存中讀取字節(jié)。但是,服務(wù)器似乎無法正確讀取客戶端的數(shù)據(jù)。[更新] 我試圖在服務(wù)器端讀出前 12 個(gè)字節(jié)。該值是恒定的,不再改變。b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'此外,隨機(jī)幀的前 12 個(gè)字節(jié)是b'\xf5\xff\xff\xf0\xfa\xfe\xdf\xe9\xed\xd2\xdc\xe0'
查看完整描述

1 回答

?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊

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

python-mmap-ipc


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

使用 mmap 在進(jìn)程之間共享 Python 數(shù)據(jù)。

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


我使用網(wǎng)絡(luò)攝像頭進(jìn)行測試。

服務(wù)器

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(不在磁盤上創(chuàng)建文件)可以與線程(或分叉)一起工作,因?yàn)樗鼈児蚕硐嗤膬?nèi)存。


查看完整回答
反對 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)