카테고리 없음

SharedMemory

파란김치 2024. 2. 23. 18:45
    var img = Cv2.ImRead("C:\\Users\\DW-PC\\Desktop\\123.png", ImreadModes.Unchanged);
    var shmName = "test";
    using (var mmf = MemoryMappedFile.CreateNew(shmName, img.Width * img.Height * img.Channels()))
    {
        using (var accessor = mmf.CreateViewAccessor(0, img.Width * img.Height * img.Channels()))
        {
            img.Reshape(1).GetArray(out byte[] array);
            // Copy the pixel data directly into the byte array
            accessor.WriteArray(0, array, 0, array.Length);
        }
    }
#fastapi
from multiprocessing import shared_memory
from fastapi import FastAPI, HTTPException
import numpy as np
import cv2

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/test")
async def test(width :int , height : int, depth :int, shm_name):
    existing_shm = shared_memory.SharedMemory(name=shm_name)
    # print(len(existing_shm.buf))
    # for i in range(0, len(existing_shm.buf)):
    #     print(existing_shm.buf[i])
    c = np.ndarray((height, width, 3), dtype=np.uint8, buffer=existing_shm.buf)
    cv2.imwrite('test.png', c)