Skip to content

Instantly share code, notes, and snippets.

@jryebread
Created July 20, 2018 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jryebread/3961e890375fcc8a64c8ac3d279ec9fa to your computer and use it in GitHub Desktop.
Save jryebread/3961e890375fcc8a64c8ac3d279ec9fa to your computer and use it in GitHub Desktop.
import os
os.environ['PYTHONASYNCIODEBUG'] = '1'
import asyncio
import logging
import cv2
@asyncio.coroutine
def fixed_open_connection(host=None, port=None, *,
loop=None, limit=65536, **kwds):
if loop is None:
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader(limit=limit, loop=loop)
protocol = asyncio.StreamReaderProtocol(reader, loop=loop)
transport, _ = yield from loop.create_connection(lambda: protocol, host, port, **kwds)
###### Following line added to fix buffering issues:
transport.set_write_buffer_limits(0)
######
writer = asyncio.StreamWriter(transport, protocol, reader, loop)
return reader, writer
async def tcp_echo_client(data, loop):
reader, writer = await fixed_open_connection('192.168.1.110', 8080, loop=loop)
print('Sending data of size: %r' % str(len(data)))
writer.write(data)
await writer.drain()
data = await reader.read(100)
print('Received: %r' % data.decode())
print(type(data))
print('Close the socket')
writer.write_eof()
writer.close()
#start here
cap = cv2.VideoCapture(0)
cap.set(3, 1) #MIN AND MAX RESOLUTION
cap.set(4, 1)
width = cap.get(3)
height = cap.get(4)
print(width)
print(height)
if cap.isOpened():
ret, frame = cap.read()
cv2.imwrite("frame.jpg", frame)
with open('frame.jpg','rb') as f:
data=f.read()
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(data, loop))
loop.close()
using UnityEngine;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
//using HUX.Interaction;
//using HUX.Receivers;
using UnityEngine.UI;
//<JEM>Ignore unity editor and run this code in the hololens instead</JEM>
#if !UNITY_EDITOR
using System.Threading;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Networking.Connectivity;
using Windows.Storage.Streams;
#endif
//Able to act as a reciever
public class ImageReciever : MonoBehaviour
{
Stream streamIn;
Renderer rend;
Texture2D texture;
int counter = 0;
byte[] byArray;
#if !UNITY_EDITOR
StreamSocket socket;
StreamSocketListener listener;
String port;
String message;
#endif
// Use this for initialization
void Start()
{
#if !UNITY_EDITOR
rend = this.GetComponent<Renderer>();
listener = new StreamSocketListener();
port = "8080";
listener.ConnectionReceived += _receiver_socket_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 _receiver_socket_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
try
{
while (true)
{
using (var dw = new DataWriter(args.Socket.OutputStream))
{
dw.WriteString("Hello There from C#");
await dw.StoreAsync();
dw.DetachStream();
}
using (var reader = new DataReader(args.Socket.InputStream))
{
reader.InputStreamOptions = InputStreamOptions.Partial;
uint numFileBytes = await reader.LoadAsync(reader.UnconsumedBufferLength);
byArray = new byte[numFileBytes];
reader.ReadBytes(byArray);
}
}
} catch (Exception e)
{
Debug.Log("disconnected!!!!!!!! " + e);
}
}
#endif
//Do nothing every frame
void Update()
{
File.WriteAllBytes("Image.txt", byArray);
texture.LoadImage(byArray);
this.GetComponent<Renderer>().material.mainTexture = texture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment