Hololens TCP套接字-Hololens服务器的Python客户端 [英] Hololens TCP Sockets - Python Client to Hololens Server

查看:173
本文介绍了Hololens TCP套接字-Hololens服务器的Python客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几周的挫败,我终于能够从Python客户端向Hololens服务器发送字符串.该代码在下面,可以完美运行.但是,我想知道是否有套接字方面的经验可以帮助我修改此代码,以将openCV网络摄像头框架(因此基本上只是发送图像)从Python发送到Hololens.我一直在尝试,但是C#脚本似乎未接收到图像数据.

After a couple weeks of frustration I was finally able to send a string from a Python client to a Hololens server. The code is below and runs perfectly. However, I was wondering if someone experienced with sockets could help me modify this code to send an openCV webcam frame (so basically just send an image) from Python to the Hololens. I've been trying but the image data does not seem to be received in the C# script.

非常感谢您.

Hololens服务器代码:

Hololens Server Code:

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客户端(需要从此处发送图像而不是字符串)

Python Client (need to send image from here instead of string)

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

Here is the image code I have been working on. It doesn't work yet, I'm unsure if I'm receiving the byte array correctly: https://gist.github.com/jryebread/3961e890375fcc8a64c8ac3d279ec9fa

推荐答案

我已经弄清楚了如何发送图像.我最终从python客户端以字符串base64发送日期,并在hololens服务器脚本上对该字符串进行了编码和解码.

I have figured out how to send the image. I ended up sending the date from the python client as a string base64 encoded and decoded the string on the hololens server script.

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

这篇关于Hololens TCP套接字-Hololens服务器的Python客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆