tp5上傳阿里雲oss並對接阿里雲檢測人臉(DetectFace)


需求:上傳照片之后,需要檢測上傳的照片里是否存在人臉,如果不存在則不能通過。

流程需要分兩步:第一步,先上傳至阿里雲的oss,獲得照片的url,第二步,再使用第一步獲得的url去檢測(PS:阿里雲的人臉檢測只能支持oss地址,而且必須是上海地區的Bucket)。

 

請先獲取阿里雲的OSS和人臉的appid和key等配置信息。

獲取到之后,再將配置信息新增到【config.php】文件里:

 

 

新增內容:

    //阿里雲配置
    'aliyun'        =>[
        'AccessKeyID'      => 'LTA******ZzQ',
        'AccessKeySecret'  => 'uzY9*******QrFc',
        'EndPoint'   => 'oss-cn-shanghai.aliyuncs.com',
        'Bucket'     => 'ai****ace',
        'face'      => [
            'AccessKeyID'      => 'LT******ZzQ',
            'AccessKeySecret'  => 'uz*******QrFc',
            'endpoint'   => 'facebody.cn-shanghai.aliyuncs.com',
        ],
    ],

 

 

先講一下上傳OSS(PS:已經懂得怎么上傳的,請略過第一步)點此跳轉至第二步

點此下載 oss sdk(地址:https://github.com/aliyun/aliyun-oss-php-sdk)

下載后,解壓到項目的【extend】文件夾

 

解壓完成后,在公共的地方新建一個文件【AliOssUpload】。在哪新建都行,只要后面能調用到就行。本人新建在 common/controller下

 

 

【AliOssUpload】文件內容:

<?php
namespace app\common\controller;

use think\Controller;

require_once APP_PATH . '/../extend/aliyun-oss/autoload.php'; //引入阿里雲OSS SDK,注意替換路徑
use OSS\Core\OssException;
use OSS\OssClient;

class AliOssUpload
{

    /**
     * 調用阿里雲OSS SDK
     * @param string $fileName 上傳的文件
     * @param string $path 上傳的文件
     * @return array 阿里雲OSS上傳處理結果
     *
     */
    public static function AliuploadFile($fileName, $path)
    {
        try {
            //獲取OSS參數值
            $KeyId = config('aliyun.AccessKeyID');
            $KeySecret = config('aliyun.AccessKeySecret');
            $EndPoint = config('aliyun.EndPoint');
            $Bucket = config('aliyun.Bucket');
            //實例化
            $ossClient = new OssClient($KeyId, $KeySecret, $EndPoint);
            //執行阿里雲上傳
            $result = $ossClient->uploadFile($Bucket, $fileName, $path);
            //圖片地址:$result['info']['url']
            $arr = ['code' => 200, 'msg' => '上傳成功', 'data' => $result['info']['url']];
        } catch (OssException $e) {
            $arr = ['code' => 0, 'msg' => $e->getMessage(), 'data' => ''];
        }
        return $arr;
    }
}

 

然后就是調用了,在api/controller里新建【Face.php】文件:

<?php
namespace app\api\controller;  

use app\common\controller\Api;
use app\common\controller\AliOssUpload; //這里就是引用上面的文件

/**
 * 人臉接口
 */
class Face extends Api
{
    protected $noNeedLogin = ['*']; //是否需要登錄 *表示全部都不
    protected $noNeedRight = '*';  //是否需要鑒權 *表示全部都不/**
     * 文件上傳接口
     * @param string $file
     * @return bool
     */
    public static function uploadFile($file)
    {
//        $file = "/uploads/20210624/aa159505d9bad351634bd4b89b674e2b.png";
        $path = ROOT_PATH . "public" . $file;   //絕對路徑
        $file = str_replace('/', '-', substr($file, 1)); //名字里不能出現 / 否則上傳失敗

        //調用阿里雲OSS上傳
        $res = AliOssUpload::AliuploadFile($file, $path);
        if ($res['code'] != 200) {
            return false;
        }
        return $res['data'];
    }
}

 

至此,上傳阿里雲的OSS結束。

傳完后,就要拿返回的url去做人臉檢測了。

 

可以直接使用composer裝人臉的SDK:

composer require alibabacloud/facebody-20191230 1.0.11

 

裝完之后,在api/controller的【Face.php】文件新增調用人臉方法,完整的文件內容如下:

<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\AliOssUpload;

use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\Tea\Tea;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectFaceRequest;

/**
 * 人臉接口
 */
class Face extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    protected $model = null;
    protected $relationSearch = true;


    /**
     * 文件上傳接口
     * @param string $file
     * @return bool
     */
    public static function uploadFile($file)
    {
//        $file = "/uploads/20210624/aa159505d9bad351634bd4b89b674e2b.png";
        $path = ROOT_PATH . "public" . $file;   //絕對路徑
        $file = str_replace('/', '-', substr($file, 1)); //名字里不能出現 / 否則上傳失敗

        //調用阿里雲OSS上傳
        $res = AliOssUpload::AliuploadFile($file, $path);
        if ($res['code'] != 200) {
            return false;
        }
//     if (isset($res['data']) && self::getFace($res['data'])) {
//       return true;
//     }
return $res['data']; } /** * 是否存在人臉 * @param string $img 阿里雲OSS的圖片地址 * @return bool */ public static function getFace($img) { $args = config('aliyun.face'); $args['imageURL'] = $img; return self::main($args); } /** * 使用AK&SK初始化賬號Client * @param string $accessKeyId * @param string $accessKeySecret * @param string $endpoint * @return Facebody Client */ public static function createClient($accessKeyId, $accessKeySecret, $endpoint) { $config = new Config([ // 您的AccessKey ID "accessKeyId" => $accessKeyId, // 您的AccessKey Secret "accessKeySecret" => $accessKeySecret ]); // 訪問的域名 $config->endpoint = $endpoint; return new Facebody($config); } /**
   * 調用阿里雲人臉檢測 * @param array $args * @return bool
*/ public static function main($args) { try { $client = self::createClient($args['AccessKeyID'], $args['AccessKeySecret'], $args['endpoint']); $detectFaceRequest = new DetectFaceRequest([ "imageURL" => $args['imageURL'] ]); $resp = $client->detectFace($detectFaceRequest); $faceInfo = Tea::merge($resp); if (isset($faceInfo['body']) && $faceInfo['body']['Data']['FaceCount'] >= 1) { return true; } return false; // die; // Console::log(Utils::toJSONString(Tea::merge($resp))); } catch (\Exception $exception) { return false; } } }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM