Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

876
Views
Unity + OpenCV + Python (integreation)

there is a need to combine Unity3d and OpenCV (preferably with NumPy, since functions will also be used from there, which means +Python is also needed). The main idea is to pass images from camera from Unity to OpenCV for processing and then sending actions back to Unity.

I've tried many variants, made by taking picture bytes stream from RenderTexture (Unity) and sending it to python server on websockets. Everything works, and the transfer speed is acceptable, but during the "photographing" of the texture it takes a lot of resources and the main thread of Unity hangs... so I get 5 fps, which is not good. I want to rewrite it the right way: either integrate openCV into unity, or pull texture from video memory (but that's horrible C++ and openGL, I want to get python, because I need numPy).

I tried streaming with FMETP STREAM (it has not started front end, which means it works, but image is not displayed in browser), then put in IronPython openCV and use them in C# (I can not install opencv in any way), tried to put modules for C#: OpenCVSharp (seems to be ok with this package), NumSharp (lacks some methods to be used in the project), and socket image transfer method (works, but awfully long and lags). .. in each variant tried and studied almost everything thoroughly.

Actually, I wanted to ask for advice, which integration is better to choose, how to do it better?

Code I use to send image bytes to python server: https://pastebin.com/pRyutRcj

    WebSocket ws;

    void Start () {

        ws = WebSocketFactory.CreateInstance("ws://localhost:12345");

        // ...

        // Connect to the server
        ws.Connect();

        InvokeRepeating("Capture", 1f, 0.2f);  //1s delay, repeat every 1s

    }

    public void Capture()
    {
        RenderTexture activeRenderTexture = RenderTexture.active;
        RenderTexture.active = Camera.targetTexture;

        Camera.Render();

        Texture2D image = new Texture2D(Camera.targetTexture.width, Camera.targetTexture.height);
        image.ReadPixels(new Rect(0, 0, Camera.targetTexture.width, Camera.targetTexture.height), 0, 0);
        image.Apply();
        RenderTexture.active = activeRenderTexture;

        byte[] bytes = image.EncodeToPNG();
        Destroy(image);


        ws.Send(bytes);
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your original solution is running everything in your main thread on unity. Thus, it will lag as expected. And you are encoding as PNG, the data will be huge.

In FMETP STREAM, they used multi-threading encoder, which should at least solve your performance issue. That's why it can run smoothly on low-end device and VR headsets.

The output byte[] are chunks of jpeg, instead of one image. It was designed for UDP packet size limit.

If you try to decode FMETP STREAM's image on your own, you have to combine those byte[] into one complete jpeg. It won't be too difficult referring to their decoder in C#.

PS: For technical support, please contact the developer directly.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!