gpt4 book ai didi

c# - Hololens TCP 套接字 - Hololens 服务器的 Python 客户端

转载 作者:行者123 更新时间:2023-11-30 17:29:15 29 4
gpt4 key购买 nike

经过几个星期的挫折,我终于能够将字符串从 Python 客户端发送到 Hololens 服务器。代码在下面并且运行完美。但是,我想知道是否有人对套接字有经验可以帮助我修改此代码以将 openCV 网络摄像头帧(因此基本上只是发送图像)从 Python 发送到 Hololens。我一直在尝试,但 C# 脚本中似乎没有接收到图像数据。

非常感谢。

Hololens 服务器代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if !UNITY_EDITOR
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
#endif

//Able to act as a reciever
public class UniversalSampleTutorial : MonoBehaviour
{
public String _input = "Waiting";

#if !UNITY_EDITOR
StreamSocket socket;
StreamSocketListener listener;
String port;
String message;
#endif

// Use this for initialization
void Start()
{
#if !UNITY_EDITOR
listener = new StreamSocketListener();
port = "9090";
listener.ConnectionReceived += Listener_ConnectionReceived;
listener.Control.KeepAlive = false;

Listener_Start();
#endif
}

#if !UNITY_EDITOR
private async void Listener_Start()
{
Debug.Log("Listener started");
try
{
await listener.BindServiceNameAsync(port);
}
catch (Exception e)
{
Debug.Log("Error: " + e.Message);
}

Debug.Log("Listening");
}

private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
Debug.Log("Connection received");

try
{
while (true) {

using (var dw = new DataWriter(args.Socket.OutputStream))
{
dw.WriteString("Hello There");
await dw.StoreAsync();
dw.DetachStream();
}

using (var dr = new DataReader(args.Socket.InputStream))
{
dr.InputStreamOptions = InputStreamOptions.Partial;
await dr.LoadAsync(12);
var input = dr.ReadString(12);
Debug.Log("received: " + input);
_input = input;

}
}
}
catch (Exception e)
{
Debug.Log("disconnected!!!!!!!! " + e);
}

}

#endif

void Update()
{
this.GetComponent<TextMesh>().text = _input;
}
}

Python 客户端(需要从这里发送图片而不是字符串)

import asyncio

#THIS CODE WORKS SENDING STRING MESSAGE TO HOLOLENS

async def tcp_echo_client(message, loop):
reader, writer = await asyncio.open_connection('192.168.1.110', 9090, loop=loop)

print('Send: %r' % message)
writer.write(message.encode())

data = await reader.read(100)
print('Received: %r' % data.decode())

print('Close the socket')
writer.close()


message = 'hello there frend'
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(message, loop))
loop.close()

编辑:这是我一直在处理的图像代码。它还不起作用,我不确定我是否正确接收字节数组: https://gist.github.com/jryebread/3961e890375fcc8a64c8ac3d279ec9fa

最佳答案

我已经知道如何发送图像了。我最终以 base64 编码的字符串形式从 python 客户端发送日期,并在 hololens 服务器脚本上解码该字符串。

https://gist.github.com/jryebread/2bdf148313f40781f1f36d38ada85d47

关于c# - Hololens TCP 套接字 - Hololens 服务器的 Python 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411832/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com