title: Unity - extracting camera pixel array is incredibly slow
body: <p>I'm using the below lines of code to extract the pixel array from a camera at every frame, saving it as jpg and then running a python process on the jpg. Although it works, it is incredibly slow. The bottleneck seems to be reading the pixels in unity. The python process itself only lasts 0.02 seconds.</p>
  <p>Can anyone suggest relatively easy ways I can speed up this process?</p>
  <p>I've seen <a href="https://medium.com/google-developers/real-time-image-capture-in-unity-458de1364a4c" rel="nofollow noreferrer">this</a> , but it's too high-level for me to understand how to adapt it to my use-case.</p>
  <pre><code>public override void Initialize()
  {
      renderTexture = new RenderTexture(84, 84, 24);
      rawByteData = new byte[84 * 84 * bytesPerPixel];
      texture2D = new Texture2D(84, 84, TextureFormat.RGB24, false);
      rect = new Rect(0, 0, 84, 84);
      cam.targetTexture = renderTexture;
  
  }
  private List&lt;float&gt; run_cmd()
  {
      // Setup a camera, texture and render texture
      cam.targetTexture = renderTexture;
      cam.Render();
      // Read pixels to texture
      RenderTexture.active = renderTexture;
      texture2D.ReadPixels(rect, 0, 0);
      rawByteData = ImageConversion.EncodeToJPG(texture2D);
      // Assign random temporary filename to jpg
      string fileName = &quot;/media/home/tmp/&quot; + Guid.NewGuid().ToString() + &quot;.jpg&quot;;
      File.WriteAllBytes(fileName, rawByteData); // Requires System.IO
  
       // Start Python process
       ProcessStartInfo start = new ProcessStartInfo();
       start.FileName = &quot;/media/home/path/to/python/exe&quot;;
       start.Arguments = string.Format(
          &quot;/media/home/path/to/pythonfile.py photo {0}&quot;, fileName);
       start.UseShellExecute = false;
       start.RedirectStandardOutput = true;
       start.RedirectStandardError = true;
       string stdout;
       using(Process process = Process.Start(start))
       {
           using(StreamReader reader = process.StandardOutput)
           {
               stdout = reader.ReadToEnd();
           }
       }
  
      string[] tokens = stdout.Split(',');
      List&lt;float&gt; result = tokens.Select(x =&gt; float.Parse(x)).ToList();
      System.IO.File.Delete(fileName);
  
      return result;
  
  
  }
  </code></pre>