PHP code example of 1338 / nativephp-tflite

1. Go to this page and download the library: Download 1338/nativephp-tflite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

1338 / nativephp-tflite example snippets


use OneThreeThreeEight\NativephpTflite\Tflite;

$info = Tflite::loadModelFromAsset('models/example.tflite');

$base64 = base64_encode(file_get_contents('/local/path/to/model.tflite'));

Tflite::addModel('model.tflite', $base64);
Tflite::loadModelFromFile('model.tflite');

$info = Tflite::modelInfo();

$result = Tflite::run([
    0.1,
    0.2,
    0.3,
]);

$output = $result['data'];

$result = Tflite::run([
    [0.1, 0.2],
    [0.3, 0.4],
]);

$result = Tflite::run($input, outputIndex: 1);

[
    'loaded' => true,
    'model' => 'file:model.tflite',
    'inputs' => [
        [
            'index' => 0,
            'name' => 'serving_default_input',
            'shape' => [1, 224, 224, 3],
            'dataType' => 'FLOAT32',
            'bytes' => 602112,
        ],
    ],
    'outputs' => [
        [
            'index' => 0,
            'name' => 'StatefulPartitionedCall',
            'shape' => [1, 1001],
            'dataType' => 'FLOAT32',
            'bytes' => 4004,
        ],
    ],
]

[
    'outputIndex' => 0,
    'shape' => [1, 1001],
    'dataType' => 'FLOAT32',
    'data' => [0.01, 0.93, 0.06],
]
bash
composer native:plugin:register 1338/nativephp-tflite
php artisan native:plugin:validate
php artisan native:install --force