1338/nativephp-tflite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

1338/nativephp-tflite

Composer 安装命令:

composer require 1338/nativephp-tflite

包简介

NativePHP plugin for TensorFlow Lite model storage, metadata, and inference

README 文档

README

Packagist Version

NativePHP plugin for loading TensorFlow Lite models and running on-device inference from PHP or JavaScript.

This project is intentionally scoped as a small TFLite bridge, not a wakeword engine. Android and iOS do not provide reliable custom wakeword support in the way this project originally explored, so the useful surface is model storage, model metadata, and direct inference.

Status

  • Android: implemented for single-input models.
  • iOS: bridge stubs only; not implemented yet.
  • Supported tensor types: FLOAT32, INT32, UINT8, and INT8.
  • Current limitation: one input tensor per model. Multiple outputs are supported by choosing an outputIndex.
  • Build validation: installed from Packagist into a clean NativePHP Mobile 3.3 app and compiled with ./gradlew assembleRelease.

Requirements

  • NativePHP Mobile 3 app.
  • PHP 8.3+.
  • Android min SDK 33 or higher.
  • Android project generated by NativePHP.
  • TensorFlow Lite Android dependency from nativephp.json.

Install

Require and register the plugin from your NativePHP mobile app:

composer require 1338/nativephp-tflite
php artisan native:plugin:register 1338/nativephp-tflite
php artisan native:plugin:validate
php artisan native:install --force

PHP Usage

Load a model bundled in Android assets:

use OneThreeThreeEight\NativephpTflite\Tflite;

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

Store a model in app storage:

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

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

Inspect the loaded model:

$info = Tflite::modelInfo();

Run inference:

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

$output = $result['data'];

The input array is flattened before being passed to TensorFlow Lite, so nested arrays are allowed:

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

For models with multiple outputs, choose the output tensor:

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

JavaScript Usage

import {
  loadModelFromAsset,
  loadModelFromFile,
  modelInfo,
  run,
} from '/_native/plugins/tflite/tflite.js';

await loadModelFromAsset('models/example.tflite');

const info = await modelInfo();
const result = await run([0.1, 0.2, 0.3]);

API

Tflite::loadModelFromAsset(string $asset): ?array

Loads a .tflite model from Android assets and returns model metadata.

Tflite::addModel(string $name, string $base64Data): ?array

Stores a base64-encoded model in app-private storage under tflite_models/.

Tflite::listModels(): array

Returns stored model files with name, size, and lastModified.

Tflite::deleteModel(string $name): bool

Deletes a stored model file.

Tflite::loadModelFromFile(string $filename): ?array

Loads a model previously stored with addModel() or copyAssetToStorage().

Tflite::copyAssetToStorage(string $assetName, string $targetName): ?array

Copies a bundled asset model into app-private model storage.

Tflite::modelInfo(): ?array

Returns metadata for the currently loaded model:

[
    '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,
        ],
    ],
]

Tflite::run(array $input, int $outputIndex = 0): ?array

Runs inference against the loaded model.

Return shape:

[
    'outputIndex' => 0,
    'shape' => [1, 1001],
    'dataType' => 'FLOAT32',
    'data' => [0.01, 0.93, 0.06],
]

Security Notes

  • Stored model names are sanitized to a basename before writing to app storage.
  • Models are stored in the app-private files directory.
  • This plugin does not download models by itself. If your app downloads models, validate source, integrity, size, and expected tensor metadata before loading.

Roadmap

  • Add tests for PHP facade behavior.
  • Add Kotlin tests for tensor flattening and output decoding.
  • Support multiple input tensors.
  • Add optional output reshaping instead of returning a flat array only.
  • Implement iOS with TensorFlow Lite Swift or document Android-only support permanently.

Not Goals

  • Wakeword detection.
  • Background audio capture.
  • Always-on microphone behavior.
  • Model training or conversion.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: Kotlin

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固