title: Tesseract with PHP performance
body: <p>I have implemented an API with Laminas and Mezzio (former Zend Expressive). There I have a handler which uses the thiagoalessio\TesseractOCR library (<a href="https://github.com/thiagoalessio/tesseract-ocr-for-php" rel="nofollow noreferrer">https://github.com/thiagoalessio/tesseract-ocr-for-php</a>) to call Tesseract from PHP.</p>
  <p>On my development environment everything works fine. Getting an image text by calling the API takes 2-6 seconds.</p>
  <p>Now first I have deployed the API to a Google Cloud VM and now I have deployed it to a Raspberry Pi 4 4GB RAM model. Both are very slow! A request response takes 25-30 seconds. Tesseract doesn't seem to be the problem. If I call it from the CLI it is super fast. But simple API calls aren't slow either! It seems that the combination of Lamina / Mezzio with Tesseract is super slow. I really do nothing but extracting the text from the image and sending it back as a JSON response.</p>
  <p>I am running php 7.3 on a apache2 server. The Pi is in my local network connected via LAN. I am testing the API calls with Postman.</p>
  <p>What can I do to increase performance? Is it the hardware?</p>
  <p>This is my handler code</p>
  <pre><code>&lt;?php
  
  declare(strict_types=1);
  
  namespace App\Handler;
  
  use Laminas\Diactoros\Response\JsonResponse;
  use Psr\Http\Message\ResponseInterface;
  use Psr\Http\Message\ServerRequestInterface;
  use Psr\Http\Server\RequestHandlerInterface;
  use thiagoalessio\TesseractOCR\TesseractOCR;
  
  class OcrHandler implements RequestHandlerInterface
  {
      public function handle(ServerRequestInterface $request) : ResponseInterface
      {
          $measure = [];
          $start = microtime(true);
  
          $body = $request-&gt;getBody();
  
          $result = '';
  
          if(!empty($body-&gt;getContents())) {
              $measure['body_parse'] = microtime(true) - $start;
              $start = microtime(true);    
  
              $guid = $this-&gt;GUID();
              $imagePath = sprintf('%s/data/%s', getcwd(), $guid);
  
              file_put_contents($imagePath, $body-&gt;getContents());
              
              $measure['image_write'] = microtime(true) - $start;
              $start = microtime(true);
  
              $tesseractOcr = new TesseractOCR($imagePath);
              $tesseractOcr-&gt;withoutTempFiles();
              $result = $tesseractOcr-&gt;lang('deu')-&gt;run();
              
              $measure['image_parsing'] = microtime(true) - $start;
              $start = microtime(true);
  
              unlink($imagePath);
  
              $measure['image_delete'] = microtime(true) - $start;
          }
  
          return new JsonResponse(['result' =&gt; $result, 'measure' =&gt; $measure]);
      }
  
      private function GUID()
      {
          if (function_exists('com_create_guid') === true)
              return trim(com_create_guid(), '{}');
      
          return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
      }
  }
  </code></pre>
  <p><strong>Edit</strong></p>
  <p>Ok so I've added time measurement and found the bottleneck. It is indeed the &quot;image_parsing&quot;, the execution of Tesseract. Which for me, is strange, because as I said, on the CLI it's super fast. Here it takes most of the response time (27,9 sec)!</p>
  <pre><code>{
      &quot;result&quot;: &quot;...&quot;,
      &quot;measure&quot;: {
          &quot;body_parse&quot;: 0.0018658638000488281,
          &quot;image_write&quot;: 0.0020492076873779297,
          &quot;image_parsing&quot;: 27.909277200698853,
          &quot;image_delete&quot;: 0.0005030632019042969
      }
  }
  </code></pre>
  <p>Why is it so fast on the CLI but so slow when I call it from PHP? Is there any possible performance improvement?</p>