通过 UDP 将图像发送到虚幻引擎并在那里加载

问题描述 投票:0回答:1

我想通过 UDP 将图像从 Python 脚本发送到 Unreal 并将其加载到那里,并在下一步中创建一个纹理。我目前正在努力接收图像。

这是我用 Python 编写的简单 udp 脚本:

import cv2, imutils, socket
import numpy as np
import time
import base64

BUFF_SIZE = 65536
server_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,BUFF_SIZE)
host_name = socket.gethostname()
host_ip = '127.0.0.1'#  socket.gethostbyname(host_name)
print(host_ip)
port = 54001
socket_address = (host_ip,port)
server_socket.bind(socket_address)
print('Listening at:',socket_address)

vid = cv2.VideoCapture(0) #  replace 'rocket.mp4' with 0 for webcam
fps,st,frames_to_count,cnt = (0,0,20,0)

while True:
    msg,client_addr = server_socket.recvfrom(BUFF_SIZE)
    print('GOT connection from ',client_addr)
    WIDTH=400
    while(vid.isOpened()):
        _,frame = vid.read()
        frame = imutils.resize(frame,width=WIDTH)
        encoded,buffer = cv2.imencode('.jpg',frame,[cv2.IMWRITE_JPEG_QUALITY,80])
        #message = base64.b64encode(buffer)
        server_socket.sendto(bytes(buffer),client_addr)
        frame = cv2.putText(frame,'FPS: '+str(fps),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.imshow('TRANSMITTING VIDEO',frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            server_socket.close()
            break
        if cnt == frames_to_count:
            try:
                fps = round(frames_to_count/(time.time()-st))
                st=time.time()
                cnt=0
            except:
                pass
        cnt+=1

所以我将其作为 base64 数组发送。我如何在虚幻中加载它?我创建了一个简单的 udp .cpp 和 .h 文件,其中包含启动 UDP 套接字的所有内容:

void Audp_module::BeginPlay()
{
    Super::BeginPlay();
    SocketSubsystem = nullptr;
    if (SocketSubsystem == nullptr) SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);

    SendSize = 2 * 1024 * 1024;
    BufferSize = 2 * 1024 * 1024;
    FIPv4Address iPAdress;
    FIPv4Address::Parse("127.0.0.1",iPAdress);
    LocalEndpoint = FIPv4Endpoint(iPAdress, LocalPort);

    FIPv4Address::Parse(IP, RemoteAddress);
    RemoteEndpoint = FIPv4Endpoint(RemoteAddress, RemotePort);

    Socket = nullptr;

    if (SocketSubsystem != nullptr)
    {
        if (Socket == nullptr)
        {
            Socket = FUdpSocketBuilder(SocketDescription)
                .AsNonBlocking()
                .AsReusable()
                .BoundToEndpoint(LocalEndpoint)
                .WithReceiveBufferSize(SendSize)
                .WithReceiveBufferSize(BufferSize)
                .WithBroadcast();
        }
    }
}

void Audp_module::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    Super::EndPlay(EndPlayReason);
    SocketSubsystem->DestroySocket(Socket);
    Socket = nullptr;
    SocketSubsystem = nullptr;
}

void Audp_module::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    FString t = "Hello";
    sendMessage(t); // Send Message Test
    Listen(); // Listen for messages

    
}

void Audp_module::Listen()
{
    TSharedRef<FInternetAddr> targetAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
    uint32 Size;
    if(Socket != nullptr)
    {
        while (Socket->HasPendingData(Size))
        {
            uint8* Recv = new uint8[Size];
            int32 BytesRead = 0;

            ReceivedData.SetNumUninitialized(FMath::Min(Size, 65507u));
            Recv = ReceivedData.GetData();
            Socket->RecvFrom(ReceivedData.GetData(), ReceivedData.Num(), BytesRead, *targetAddr);
            
            char ansiiData[1024];
            memcpy(ansiiData, ReceivedData.GetData(), BytesRead);
            ansiiData[BytesRead] = 0;
            
            FString data = ANSI_TO_TCHAR(ansiiData);
            //FString data = FString::FromInt(BytesRead);
            const auto encoded = FBase64::Encode(*data);
            GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, encoded);
            UE_LOG(LogTemp, Verbose, TEXT("Received: %s"), *encoded);
            //FString data = ANSI_TO_TCHAR(ansiiData);
            GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Message by UDP: " + data);
        }
    }
    else
    {
        Socket = FUdpSocketBuilder(SocketDescription)
                .AsNonBlocking()
                .AsReusable()
                .BoundToEndpoint(LocalEndpoint)
                .WithReceiveBufferSize(SendSize)
                .WithReceiveBufferSize(BufferSize)
                .WithBroadcast();
    }
    
}

ansiiData 之后的部分不起作用,我猜我必须以不同的方式将接收到的数据转换为 Base64?我的计划是做类似here

所描述的事情

我怎样才能实现它?

python c++ udp unreal-engine4 unreal-engine5
1个回答
0
投票

有可能ansiiData太小了?也许调整它的大小以适合而不是硬编码。

char ansiiData[BytesRead + 1];
memcpy(ansiiData, ReceivedData.GetData(), BytesRead);
ansiiData[BytesRead] = 0;
© www.soinside.com 2019 - 2024. All rights reserved.