diff --git a/app/Http/Controllers/Api/OCRController.php b/app/Http/Controllers/Api/OCRController.php index 6b9926e..f343fc6 100644 --- a/app/Http/Controllers/Api/OCRController.php +++ b/app/Http/Controllers/Api/OCRController.php @@ -175,6 +175,10 @@ class OCRController extends ApiController $data['ocr']['pixelate'] = $result; imagedestroy($imgcreate); + // keras + $cmd = "python3 scripts/keras.py " . base64_encode(file_get_contents($tmpfname)); + $command = escapeshellcmd($cmd); #no special characters it will work + $data['ocr']['keras'] = shell_exec($command); unlink($tmpfname); return $this->respondJson($data); @@ -203,28 +207,4 @@ class OCRController extends ApiController // // Do something with the text from Tesseract // echo $text; - - - // function is_ani($filename) { - // if(!($fh = @fopen($filename, 'rb'))) - // return false; - // $count = 0; - // //an animated gif contains multiple "frames", with each frame having a - // //header made up of: - // // * a static 4-byte sequence (\x00\x21\xF9\x04) - // // * 4 variable bytes - // // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?) - - // // We read through the file til we reach the end of the file, or we've found - // // at least 2 frame headers - // $chunk = false; - // while(!feof($fh) && $count < 2) { - // //add the last 20 characters from the previous string, to make sure the searched pattern is not split. - // $chunk = ($chunk ? substr($chunk, -20) : "") . fread($fh, 1024 * 100); //read 100kb at a time - // $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches); - // } - - // fclose($fh); - // return $count > 1; - // } } diff --git a/scripts/keras.py b/scripts/keras.py new file mode 100644 index 0000000..7f813a0 --- /dev/null +++ b/scripts/keras.py @@ -0,0 +1,17 @@ +import io +import sys +import base64 +import numpy as np +import keras_ocr + +if len(sys.argv) > 1: + # Decode the base64-encoded image + img = base64.b64decode(sys.argv[1]) + img = np.array(bytearray(img), dtype=np.uint8) + + # Use Keras-OCR to recognize text in the image + pipeline = keras_ocr.pipeline.Pipeline() + prediction = pipeline.recognize([img]) + + # Return the recognized text + print prediction[0][0]['text']