meema / laravel-media-recognition
轻松快速地将您的应用程序与AWS Rekognition集成。未来可能会添加其他驱动程序。
Requires
- php: ^7.3|^8.0
- ext-json: *
- aws/aws-php-sns-message-validator: ^1.6
- aws/aws-sdk-php: ^3.163
Requires (Dev)
- orchestra/testbench: ^3.5.0|^3.6.0|^4.0|^5.0|^6.0
- pestphp/pest: ^1.0
- phpunit/phpunit: ^5.0|^6.0|^8.0|^9.3
- vlucas/phpdotenv: ^4.2|^5.3
README
目前,这是一个AWS Rekognition的包装包,附带一些额外实用的方法。
💡 使用方法
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端点
- 在https://console.aws.amazon.com/sns/v3/home打开Amazon SNS控制台
- 在导航窗格中,选择主题,然后选择"创建新主题"。
- 对于主题名称,输入
RekognitionUpdate
,然后选择"创建主题"。
- 复制并记下您刚刚创建的主题ARN。它应该类似于以下内容:
arn:aws:sns:region:123456789012:RekognitionUpdate
。 - 在
RekognitionUpdate
主题详情页面上,在订阅部分中选择"创建订阅"。 - 对于协议,选择"HTTPS"。对于端点,输入您在之前步骤中生成的暴露API URL,包括API URI。
例如,
https://meema-api.sharedwithexpose.com/api/webhooks/media-recognition
- 选择"创建订阅"。
确认您的订阅
最后,我们需要确保订阅已确认。通过导航到RekognitionUpdate
主题页面,您应该看到以下部分
默认情况下,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。
🏝 社区
寻求帮助、讨论最佳实践或任何其他可搜索的对话
与其他使用此包的人闲聊
🚨 安全
请查看我们的安全策略,了解如何报告安全漏洞。
🙏🏼 致谢
📄 许可证
MIT许可证(MIT)。有关更多信息,请参阅LICENSE。
由Meema, Inc.用❤️制作。