laravel 檢測sql
by Darren Chowles
達倫·喬爾斯(Darren Chowles)
在Laravel PHP應用程序中輕松進行面部檢測 (Easy facial detection in your Laravel PHP application)
使用Google Cloud Vision API檢測圖像中的人臉 (Detect faces in images using the Google Cloud Vision API)
You’ve probably seen facial detection before. As soon as you upload that family photo to Facebook, you’ll notice the boxes around all detected faces. And with facial recognition, it sometimes even auto-tags the correct friend too. It’s not always 100% accurate, but it’s still some great engineering!
您可能以前曾經看過面部檢測。 將家庭照片上傳到Facebook后,您會注意到所有檢測到的臉Kong周圍的方框。 有了面部識別功能 ,它有時甚至還會自動標記正確的朋友。 它并不總是100%準確,但是仍然是一些很棒的工程!
面部檢測應用 (Applications for facial detection)
In this article, we’ll get up and running using the Google Cloud Vision API to detect faces. We’ll be using an existing image and we’ll draw a box around each detected face.
在本文中,我們將使用Google Cloud Vision API進行檢測并開始運行。 我們將使用現有圖像,并在每個檢測到的臉部周圍繪制一個方框。
There are several real-world use cases for facial detection. Some of these include:
有幾種現實世界的面部檢測用例。 其中一些包括:
- detecting whether an uploaded image has any faces. This might be a screening step as part of a “know your customer” identification workflow. 檢測上傳的圖像是否有臉Kong。 這可能是“了解您的客戶”識別工作流程一部分的篩選步驟。
- image moderation for applications that allow user-generated content. 允許用戶生成內容的應用程序的圖像審核。
- the ability to provide tagging, in the same way social networks do. 具有與社交網絡相同的方式提供標記的功能。
Cloud Vision API中可用的其他功能 (Other functionality available in the Cloud Vision API)
Facial detection is only one of the many functions available in this API. It supports the following additional functionality:
面部檢測只是此API中可用的眾多功能之一。 它支持以下附加功能:
- detection of popular logos. 檢測流行徽標。
- the ability to detect all categories applicable to an image. For example, a photo of a cat might produce the categories: cat, mammal, vertebrate, and Persian. 檢測適用于圖像的所有類別的能力。 例如,貓的照片可能會產生以下類別:貓,哺乳動物,脊椎動物和波斯。
- detecting popular natural and man-made landmarks. 檢測流行的自然和人造地標。
- extracting text from images. 從圖像中提取文本。
- running Safe Search Detection to flag images that contain adult content or violence. 運行安全搜索檢測以標記包含成人內容或暴力內容的圖像。
Google Cloud Platform設定 (Google Cloud Platform setup)
The first step involves creating a new project in the Google Cloud Platform console.
第一步涉及在Google Cloud Platform控制臺中創建一個新項目。
Head over to the dashboard and create a new project.
轉到儀表板并創建一個新項目 。
Once your project is created, keep the Project ID handy.
創建項目后,請隨時保留項目ID。
Follow these steps:
跟著這些步驟:
once you have your project, go to the Create service account key page.
完成項目后,轉到“ 創建服務帳戶密鑰”頁面。
- ensure your Facial Detection project is selected at the top. 確保在頂部選擇了面部檢測項目。
under “Service account”, select “New service account”.
在“服務帳戶”下 ,選擇“新服務帳戶”。
- enter a name in the “Service account name”. 在“服務帳戶名稱”中輸入名稱。
- under “Role”, select “Project” > “Owner”. 在“角色”下,選擇“項目”>“所有者”。
- Finally, click “Create” to have the JSON credentials file downloaded automatically. 最后,單擊“創建”以自動下載JSON憑證文件。
You may also need to Enable the Cloud Vision API via the API Library section.
您可能還需要通過“ API庫”部分啟用Cloud Vision API。
Laravel項目設置 (Laravel project setup)
The next step involves setting up a new Laravel project. If you have an existing Laravel project, you can skip this step.
下一步涉及建立一個新的Laravel項目。 如果您已經有一個Laravel項目,則可以跳過此步驟。
I’m using Laravel 5.5 LTS for this article. In the command line, run the following Composer command to create a new project (you can also use the Laravel installer):
我在本文中使用Laravel 5.5 LTS。 在命令行中,運行以下Composer命令以創建一個新項目(您也可以使用Laravel安裝程序 ):
composer create-project --prefer-dist laravel/laravel sample "5.5.*"
If you used Composer, rename the .env.example file to .env and run the following command afterwards to set the application key:
如果您使用的作曲家,重命名.env.example文件.ENV事后運行以下命令來設置應用程序鍵:
php artisan key:generate
添加Google Cloud-vision軟件包 (Add the Google cloud-vision package)
Run the following command to add the google/cloud-vision
package to your project:
運行以下命令以將google/cloud-vision
包添加到您的項目中:
composer require google/cloud-vision
You can place the downloaded JSON credentials file in your application root. Don’t place it in your public directory. Feel free to rename it. Don’t commit this file to your code repo. One option is to add it to the server manually.
您可以將下載的JSON憑證文件放置在應用程序根目錄中。 不要將其放在您的公共目錄中。 隨時重命名。 不要將此文件提交到您的代碼存儲庫中。 一種選擇是將其手動添加到服務器。
最后,讓我們開始編碼! (Finally, let’s start coding!)
Firstly, ensure you have the GD library installed and active. Most platforms have this enabled by default.
首先,請確保已安裝并激活了GD庫 。 大多數平臺默認情況下都啟用了此功能。
I’ll be adding the following route to my “routes/web.php” file:
我將以下路由添加到“ routes / web.php”文件中:
Route::get('/', 'SampleController@detectFaces');
I’ve created a simple controller to house the code. I’ll be adding all the code within the controller. In a production application, I strongly suggest using separate service classes for any business logic. This way, controllers are lean and stick to their original intention: controlling the input/output.
我創建了一個簡單的控制器來存放代碼。 我將在控制器中添加所有代碼。 在生產應用程序中,我強烈建議對任何業務邏輯使用單獨的服務類。 這樣,控制器就會精簡并堅持其原始意圖:控制輸入/輸出。
We’ll start with a simple controller, adding a use
statement to include the Google Cloud ServiceBuilder
class:
我們將從一個簡單的控制器開始,添加一個use
語句以包括Google Cloud ServiceBuilder
類:
<?php
namespace App\Http\Controllers;
use Google\Cloud\Core\ServiceBuilder;
class SampleController extends Controller{ public function detectFaces() { // Code will be added here }}
The first thing we’ll do is create an instance of the ServiceBuilder
class so we can specify our Project ID and JSON credentials.
我們要做的第一件事是創建ServiceBuilder
類的實例,以便我們可以指定項目ID和JSON憑據。
$cloud = new ServiceBuilder([ 'keyFilePath' => base_path('fda.json'), 'projectId' => 'facial-detection-app' ]);
You specify the location of the JSON file using the keyFilePath
key. I’ve used the Laravel base_path() helper to refer to the fully qualified app root path.
您可以使用keyFilePath
鍵指定JSON文件的位置。 我使用了Laravel base_path()幫助器來引用完全限定的應用程序根路徑。
The next option is the projectId
. This is the value you grabbed when you created the project in the GCP console.
下一個選項是projectId
。 這是在GCP控制臺中創建項目時獲取的值。
Next, we’ll create an instance of the VisionClient
class. The ServiceBuilder
class makes it easy by exposing various factory methods which grant access to services in the API.
接下來,我們將創建VisionClient
類的實例。 通過公開各種工廠方法(可授予對API中服務的訪問權限), ServiceBuilder
類可輕松實現。
$vision = $cloud->vision();
Now that we have an instance of the class, we can start making use of the Vision API. We’ll be using the following image as the example. Feel free to download this image, name it “friends.jpg” and place it in your “public” folder.
現在我們有了該類的實例,我們可以開始使用Vision API。 我們將使用以下圖像作為示例。 可以免費下載此圖像,將其命名為“ friends.jpg”,并將其放置在“ public”文件夾中。
We’ll first create a new image using the GD imagecreatefromjpeg()
function. We’ll use the public_path() Laravel helper to refer to our image placed in the “public” folder.
我們將首先使用GD imagecreatefromjpeg()
函數創建一個新圖像。 我們將使用public_path() Laravel幫助器來引用放置在“ public”文件夾中的圖像。
$output = imagecreatefromjpeg(public_path('friends.jpg'));
Next, we’ll create a Cloud Vision Image
object with this same image and specify that we want to run facial detection:
接下來,我們將使用相同的圖像創建一個Cloud Vision Image
對象,并指定我們要運行面部檢測:
$image = $vision->image(file_get_contents(public_path('friends.jpg')), ['FACE_DETECTION']);
You’ll notice a slight change here. Instead of providing the path to the image, we’re supplying the actual image as a string using file_get_contents()
.
您會在這里看到一些細微的變化。 除了提供圖像的路徑,我們使用file_get_contents()
將實際圖像作為字符串提供。
Then we run the annote()
method on the image:
然后,在圖像上運行annote()
方法:
$results = $vision->annotate($image);
Now that we have the results, we simply need to loop through the found faces and draw boxes around the them using the vertices supplied in the result:
現在我們有了結果,我們只需要遍歷找到的面并使用結果中提供的頂點在它們周圍畫框:
foreach ($results->faces() as $face) { $vertices = $face->boundingPoly()['vertices'];
$x1 = $vertices[0]['x']; $y1 = $vertices[0]['y']; $x2 = $vertices[2]['x']; $y2 = $vertices[2]['y'];
imagerectangle($output, $x1, $y1, $x2, $y2, 0x00ff00);}
Once this is done, we can output the image and destroy it to free up the memory:
完成此操作后,我們可以輸出圖像并銷毀它以釋放內存:
header('Content-Type: image/jpeg'); imagejpeg($output); imagedestroy($output);
And this is the result:
結果如下:
Here is the final controller class code:
這是最終的控制器類代碼:
<?php
namespace App\Http\Controllers;
use Google\Cloud\Core\ServiceBuilder;
class SampleController extends Controller{ public function detectFaces() { $cloud = new ServiceBuilder([ 'keyFilePath' => base_path('fda.json'), 'projectId' => 'facial-detection-app' ]);
$vision = $cloud->vision();
$output = imagecreatefromjpeg(public_path('friends.jpg')); $image = $vision->image(file_get_contents(public_path('friends.jpg')), ['FACE_DETECTION']); $results = $vision->annotate($image);
foreach ($results->faces() as $face) { $vertices = $face->boundingPoly()['vertices'];
$x1 = $vertices[0]['x']; $y1 = $vertices[0]['y']; $x2 = $vertices[2]['x']; $y2 = $vertices[2]['y'];
imagerectangle($output, $x1, $y1, $x2, $y2, 0x00ff00); }
header('Content-Type: image/jpeg');
imagejpeg($output); imagedestroy($output); }}
附加功能 (Additional functionality)
In addition to grabbing the vertices, the response also includes a trove of useful information. This includes the locations of mouths, eyes, eyebrows, noses, etc. Simply print_r()
the $face
variable for a quick peek into the available data.
除了獲取頂點之外,響應還包括大量有用的信息。 這包括嘴巴,眼睛,眉毛,鼻子等的位置。只需print_r()
$face
變量,即可快速瀏覽可用數據。
Another great feature is checking whether the detected face is happy, sad, angry, or surprised. You can even detect whether the face is blurry or underexposed, and whether they’re wearing headwear.
另一個重要功能是檢查檢測到的臉部是高興,悲傷,生氣還是驚訝。 您甚至可以檢測出臉部是否模糊或曝光不足,以及他們是否戴著頭飾。
If you use this and end up doing something cool as a result, please let me know!
如果您使用它并最終做一些很酷的事情,請告訴我!
升級您的Web開發技能! (Upgrade your web dev skills!)
Sign up to my newsletter where I’ll share insightful web development articles to supercharge your skills.
注冊我的時事通訊 ,我將在其中分享有見地的Web開發文章,以增強您的技能。
Originally published at www.chowles.com on July 6, 2018.
最初于2018年7月6日發布在www.chowles.com上。
翻譯自: https://www.freecodecamp.org/news/easy-facial-detection-in-your-laravel-php-application-11664ac9c9b9/
laravel 檢測sql