meema/laravel-media-recognition

轻松快速地将您的应用程序与AWS Rekognition集成。未来可能会添加其他驱动程序。

1.0.1 2021-03-26 05:37 UTC

This package is auto-updated.

Last update: 2024-09-19 23:17:54 UTC


README

Latest Version on Packagist GitHub Workflow Status StyleCI Scrutinizer Code Quality Total Downloads Discord License

目前,这是一个AWS Rekognition的包装包,附带一些额外实用的方法。

laravel-media-recognition package image

💡 使用方法

use Meema\MediaRecognition\Facades\Recognize;

// run any of the following methods:
// note: any of the detect*() method parameters are optional and will default to config values

// "image operations"
$recognize = Recognize::path('images/persons.jpg', 'image/jpeg'); // while the $mimeType parameter is optional, it is recommended for performance reasons
$recognize->detectLabels($minConfidence = null, $maxLabels = null)
$recognize->detectFaces($attributes = ['DEFAULT'])
$recognize->detectModeration($minConfidence = null)
$recognize->detectText()

// "video operations"
$recognize = Recognize::path('videos/amazing-video.mp4', 'video/mp4');
$recognize->startLabelDetection($minConfidence = null, $maxResults = 1000)
$recognize->startFaceDetection(string $faceAttribute = 'DEFAULT')
$recognize->startContentModeration(int $minConfidence = null)
$recognize->startTextDetection(array $filters = null)

// get the analysis/status of your jobs
$recognize->getLabelsByJobId(string $jobId)
$recognize->getFacesByJobId(string $jobId)
$recognize->getContentModerationByJobId(string $jobId)
$recognize->getTextDetectionByJobId(string $jobId)

// if you want to track your media recognitions, use the Recognizable trait on your media model && run the included migration
$media = Media::first();
$media->recognize($path)->detectFaces(); // you may chain any of the detection methods

🐙 安装

您可以通过composer安装此包

composer require meema/laravel-media-recognition

包将自动注册自己。

接下来,使用以下命令发布配置文件

php artisan vendor:publish --provider="Meema\MediaRecognition\Providers\MediaRecognitionServiceProvider" --tag="config"

接下来,请将以下键及其值添加到您的.env文件中。

AWS_ACCESS_KEY_ID=xxxxxxx
AWS_SECRET_ACCESS_KEY=xxxxxxx
AWS_DEFAULT_REGION=us-east-1
AWS_SNS_TOPIC_ARN=arn:aws:sns:us-east-1:000000000000:RekognitionUpdate
AWS_S3_BUCKET=bucket-name

以下是发布配置文件的内容

return [
    /**
     * The fully qualified class name of the "media" model.
     */
    'media_model' => \App\Models\Media::class,

    /**
     * IAM Credentials from AWS.
     */
    'credentials' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
    ],

    'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),

    /**
     * Specify the version of the Rekognition API you would like to use.
     * Please only adjust this value if you know what you are doing.
     */
    'version' => 'latest',

    /**
     * The S3 bucket name where the image/video to be analyzed is stored.
     */
    'bucket' => env('AWS_S3_BUCKET'),

    /**
     * Specify the IAM Role ARN.
     *
     * You can find the Role ARN visiting the following URL:
     * https://console.aws.amazon.com/iam/home?region=us-east-1#/roles
     * Please note to adjust the "region" in the URL above.
     * Tip: in case you need to create a new Role, you may name it `Rekognition_Default_Role`
     * by making use of this name, AWS Rekognition will default to using this IAM Role.
     */
    'iam_arn' => env('AWS_IAM_ARN'),

    /**
     * Specify the AWS SNS Topic ARN.
     * This triggers the webhook to be sent.
     *
     * It can be found by selecting your "Topic" when visiting the following URL:
     * https://console.aws.amazon.com/sns/v3/home?region=us-east-1#/topics
     * Please note to adjust the "region" in the URL above.
     */
    'sns_topic_arn' => env('AWS_SNS_TOPIC_ARN'),

];

准备您的媒体模型(可选)

此包包含一个用于您的"媒体模型"的特质,您可以使用它来定义媒体模型与跟踪识别之间的关系。

只需按以下方式使用它

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Meema\MediaRecognition\Traits\Recognizable;

class Media extends Model
{
    use Recognizable;

    // ...
}

设置Webhooks(可选)

此包使用webhooks来通信AWS Rekognition作业的更新。请按照以下步骤为您自己启用webhooks。

请注意,这仅是可选的,您只有在想要跟踪长时间运行的识别作业结果(例如分析视频)时才应启用此功能。

设置暴露

首先,让我们使用Expose来"暴露"或为我们的本地API生成URL。请遵循Expose文档了解如何开始,并在开发环境中生成"实时"和可共享的URL。

这应该像cd my-laravel-api && expose一样简单。

设置AWS SNS主题和订阅

其次,让我们创建一个AWS SNS主题,该主题将通知我们的"暴露"API端点

  1. https://console.aws.amazon.com/sns/v3/home打开Amazon SNS控制台
  2. 在导航窗格中,选择主题,然后选择"创建新主题"。
  3. 对于主题名称,输入RekognitionUpdate,然后选择"创建主题"。

AWS SNS Topic Creation Screenshot

  1. 复制并记下您刚刚创建的主题ARN。它应该类似于以下内容:arn:aws:sns:region:123456789012:RekognitionUpdate
  2. RekognitionUpdate主题详情页面上,在订阅部分中选择"创建订阅"。
  3. 对于协议,选择"HTTPS"。对于端点,输入您在之前步骤中生成的暴露API URL,包括API URI。

例如,

https://meema-api.sharedwithexpose.com/api/webhooks/media-recognition
  1. 选择"创建订阅"。

确认您的订阅

最后,我们需要确保订阅已确认。通过导航到RekognitionUpdate主题页面,您应该看到以下部分

AWS SNS Subscription Confirmation Screenshot

默认情况下,AWS会向您在"订阅"设置中定义的URL发送POST请求。此包自动处理"确认"部分。如果出现问题并且尚未确认,请单击上面的截图中显示的"请求确认"按钮。

您还可以通过访问"仪表板URL"在Expose界面中查看请求,该URL应该类似于:http://127.0.0.1:4040

一旦状态反映为"已确认",您的API将接收AWS提供的webhooks更新。

部署到Laravel Vapor

请注意,截至目前,您无法重用存储在"环境文件"中的AWS凭证。此"解决方案"是调整media-recognition.php-配置文件,通过重命名

从:AWS_ACCESS_KEY_ID - 到:例如 VAPOR_ACCESS_KEY_ID

从:AWS_SECRET_ACCESS_KEY - 到:例如 VAPOR_SECRET_ACCESS_KEY

最后,请确保您的Vapor环境已定义以下值。

🧪 测试

composer test

📈 更新日志

有关最近更改的更多信息,请参阅我们的发布页面

💪🏼 贡献

有关详细信息,请参阅CONTRIBUTING

🏝 社区

寻求帮助、讨论最佳实践或任何其他可搜索的对话

GitHub上的媒体识别

与其他使用此包的人闲聊

加入Meema Discord服务器

🚨 安全

请查看我们的安全策略,了解如何报告安全漏洞。

🙏🏼 致谢

📄 许可证

MIT许可证(MIT)。有关更多信息,请参阅LICENSE

由Meema, Inc.用❤️制作。