laravel 檢測sql_在Laravel PHP應用程序中輕松進行面部檢測

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393747.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393747.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393747.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql學習筆記-insert擴展

1、創建表 -利用已有表&#xff0c;創建表 這樣創建的缺點-- 主鍵會丟失 -- 創建表&#xff0c;表結構與數據與t_emptest 一致 CREATE TABLE t_emptest1 AS SELECT * FROM t_emptest ;-- 創建空表&#xff0c;表結構與t_emptest 一致 CREATE TABLE t_emptest1 AS SELECT * FRO…

linux 調用外部變量,sed當中使用變量替換以及執行外部命令

轉自&#xff1a;http://blog.csdn.net/linwhwylb/article/details/7184748在使用sed對日志或者其它文本進行parse的過程當中&#xff0c;有時候我們需要引用外部變量的值&#xff0c;或者獲取一個shell命令執行的結果&#xff0c;以便達到更加可觀的輸出結果。這里介紹如何做到…

golang 結構體斷言_Golang中的reflect原理

反射&#xff08;reflect&#xff09;是在計算機程序運行時&#xff0c;訪問&#xff0c;檢查&#xff0c;修改它自身的一種能力&#xff0c;是元編程的一種形式。在Java等語言中都很好地支持了反射。Golang也實現了反射&#xff0c;主要核心位于reflect包&#xff0c;官方文檔…

「hadoop」cdh5.12離線安裝(未完成)

cdh 5.12.1 安裝 目前還未成功&#xff0c;僅供個人記錄。【下載必備工具】當前系統為win7_x641、vmware虛擬機12.52、ubuntu16.04服務器版本3、windows下的ssh工具&#xff0c;采用git bash4、jdk安裝包jdk-8u144-linux-x64.tar.gz5、mysql java驅動包 mysql-connector-java-5…

轉載 JDK + Android-SDK + Python + MonkeyRunner 的安裝

轉載來自&#xff1a; 小海豚的博客 http://blog.sina.com.cn/u/1295334083 我只是搬運工。。。 JDK Android-SDK Python MonkeyRunner 的安裝 1. Android-SDK介紹2. 安裝 JDK, Android-SDK&#xff08;包含MonkeyRunner) , Python 3. 設置環境變量4. 驗證是否安裝成功1. …

跟隨器反饋回路電阻_如何將短反饋回路設置為單獨編碼器

跟隨器反饋回路電阻I’ve spent the last couple years as a solo freelance developer. Comparing this experience to previously working in companies, I’ve noticed that those of us who work alone can have fewer iterative opportunities for improvement than devel…

leetcode991. 壞了的計算器(貪心)

在顯示著數字的壞計算器上&#xff0c;我們可以執行以下兩種操作&#xff1a; 雙倍&#xff08;Double&#xff09;&#xff1a;將顯示屏上的數字乘 2&#xff1b; 遞減&#xff08;Decrement&#xff09;&#xff1a;將顯示屏上的數字減 1 。 最初&#xff0c;計算器顯示數字…

模塊怎么用_Android 組件化/模塊化 的理解!

作者&#xff1a;前行的烏龜到現在組件化真的不是什么新鮮東西了&#xff0c;大公司都用的滾瓜爛熟&#xff0c;龍飛鳳舞了&#xff0c;也就是現在部分中型項目和小項目在組件化的路上努力。所以同志們&#xff0c;組件化沒玩過的&#xff0c;不熟悉的趕緊搞起來&#xff0c;說…

操作系統基礎

操作系統基礎一個完整的操作系統包括 &#xff08; kernel application&#xff09;內核 應用程序而我們要學習操作系統&#xff1a;Linux操作系統我們平時所用的WINDOWS和MS-DOS都是微軟出的&#xff0c;而Linux不是微軟出的&#xff0c;Linux的最大好處是非商業軟件&#x…

leetcode1247. 交換字符使得字符串相同(貪心)

有兩個長度相同的字符串 s1 和 s2&#xff0c;且它們其中 只含有 字符 “x” 和 “y”&#xff0c;你需要通過「交換字符」的方式使這兩個字符串相同。 每次「交換字符」的時候&#xff0c;你都可以在兩個字符串中各選一個字符進行交換。 交換只能發生在兩個不同的字符串之間…

interop_如何在Blazor中實現JavaScript Interop

interop介紹 (Introduction) In this article, we will learn about JavaScript Interop in Blazor. We will understand what JavaScript Interop is and how we can implement it in Blazor with the help of a sample application.在本文中&#xff0c;我們將學習Blazor中Ja…

Centos 7和 Centos 6開放查看端口 防火墻關閉打開

Centos 7 firewall 命令&#xff1a; 查看已經開放的端口&#xff1a; firewall-cmd --list-ports 開啟端口 firewall-cmd --zonepublic --add-port80/tcp --permanent 命令含義&#xff1a; –zone #作用域 –add-port80/tcp #添加端口&#xff0c;格式為&#xff1a;端口/通訊…

和get redis_SpringBoot整合Redis,你get了嗎?

Our-task介紹本篇博客是我github上our-task&#xff1a;一個完整的清單管理系統的配套教程文檔&#xff0c;這是SpringBootVue開發的前后端分離清單管理工具&#xff0c;仿滴答清單。目前已部署在阿里云ECS上&#xff0c;可進行在線預覽&#xff0c;隨意使用&#xff08;附詳細…

linux課程設計qq,仿QQ聊天系統課程設計.doc

目錄緒論1一&#xff0e;需求分析11.1軟件功能需求分析21.2 安全需求分析2二&#xff0e;總體設計32.1 軟件結構圖32.2 功能描述32.2.1注冊功能概要42.2.2登錄功能概要42.2.3聊天功能概要52.3 安全設計6三&#xff0e;數據庫設計63.1概念結構設計63.2邏輯結構設計73.3物理結構設…

ocp linux 基礎要點

基本命令&#xff1a; 創建/修改/刪除用戶 useradd/usermod/userdel 創建/修改/刪除用戶組 groupadd/groupmod/groupdel 修改所屬用戶/所屬用戶組 chown/chgrp 修改權限 chmod 創建文件夾 mkdir 創建文件 touch 切換目錄 …

leetcode1386. 安排電影院座位(貪心)

如上圖所示&#xff0c;電影院的觀影廳中有 n 行座位&#xff0c;行編號從 1 到 n &#xff0c;且每一行內總共有 10 個座位&#xff0c;列編號從 1 到 10 。 給你數組 reservedSeats &#xff0c;包含所有已經被預約了的座位。比如說&#xff0c;researvedSeats[i][3,8] &…

首席技術執行官_如何在幾分鐘內找到任何首席執行官的電子郵件地址

首席技術執行官by Theo Strauss由西奧斯特勞斯(Theo Strauss) 如何在幾分鐘內找到任何首席執行官的電子郵件地址 (How to find any CEO’s email address in minutes) 銀河電子郵件指南&#xff1a;第一部分 (The Emailer’s Guide To The Galaxy: Part I) I’m 17, so my net…

Linux 查看磁盤或文件夾及文件大小

當磁盤大小超過標準時會有報警提示&#xff0c;這時如果掌握df和du命令是非常明智的選擇。 df可以查看一級文件夾大小、使用比例、檔案系統及其掛入點&#xff0c;但對文件卻無能為力。 du可以查看文件及文件夾的大小。 兩者配合使用&#xff0c;非常有效。比如用df查看哪個…

Python列表基礎

列表&#xff1a;創建列表:list[] 注意&#xff1a;列表里面類型可以是不同的類型 取值&#xff1a;list[2]   替換&#xff1a;注意不要越界(下表超出了可表示范圍) 操作&#xff1a; 合并列表&#xff1a;   list3list2list1 列表的重復:   (list8*3)   判斷元素是否…

樹莓派 觸摸屏_如何用樹莓派搭建一個顆粒物(PM2.5)傳感器

用樹莓派、一個廉價的傳感器和一個便宜的屏幕監測空氣質量。-- Stephan Tetzel(作者)大約一年前&#xff0c;我寫了一篇關于如何使用樹莓派和廉價傳感器測量 空氣質量 的文章。我們這幾年已在學校里和私下使用了這個項目。然而它有一個缺點&#xff1a;由于它基于無線/有線網&a…