Table of Contents
流程
起始界面
界面
实现细节
中文的使用
文字居中与背景设置
按钮的页面跳转
训练部分
train.php" >train.php
查找目录" >查找目录
测试部分
test.php部分
recognition.php部分

测试部分

Jun 13, 2016 pm 12:22 PM
file gt name quot

php使用face++实现一个简单的人脸识别系统

流程

流程可以分为两部分,一部分是训练,一部分是测试。

关于如何使用face++提供的API可以看http://blog.csdn.net/jianjian1992/article/details/46640483

代码可在http://download.csdn.net/detail/jianjian1992/8866839免费下载。


起始界面

界面

如下所示,将使用说明进行了介绍,因为是初次使用,很多不太会,所以界面弄得比较简单,以能使用为第一目标。


实现细节

index.html是欢迎界面,记录一下几个小问题

中文的使用

在head部分加上字符集使用utf-8就好啦!

 <meta http-equiv="Content-Type" content="text/html; charset = utf-8">
Copy after login

文字居中与背景设置

在html最前面加了个style,设置body的显示效果。

文字显示设置为居中,使用height,width加上margin-top,margin-left便可以设置body部分在页面中显示的地方啦。

背景则设置了图片url,以及居中显示。

<style>body{	text-align:left; 	height:500px;	width:600px;	top:50%;	margin-top:130px;	margin-left:550px;	background-image:url(./imgs/back.jpg);	background-position:center;	background-repeat:repeat-y;}</style>
Copy after login

按钮的页面跳转

onClick里面用location记录将要跳转的界面。

<input type="button" value="点击进入测试" onclick="location='test.php' ">
Copy after login

训练部分

包括两部分:

  • startTrain.php用于输入训练目录,
  • train.php用于将训练数据传到服务器,并将训练成功标记输出

startTrain.php

这一部分做得蛮挫的,查了下php选择文件夹,没有找到该怎么做,所以就用了个来输入目录。真是弱爆了啊大笑

用了一个表单form来记录输入目录,提交表单之后则跳到train.php中进行真正的训练啦。

Copy after login





train.php

这一部分实现了两个功能:

  • 根据输入目录查找所有训练图片,并记录所有图片路径
  • 将所有训练图片一一检测人脸,并将人脸加入到训练模型中


查找目录

php中查找目录中所有文件倒是挺方便的啊,listDir函数在dir.php中。

这个函数根据$dir目录名,将得到的所有文件名存入$names,所有文件路径名则存入$img_urls中。

注意的一点是,如果是中文文件名,则要加上

$file = iconv("gb2312","UTF-8",$file);
Copy after login
这一句才会得到结果的,要不就是空哦

php中字符串的查找可以使用strstr函数,

比如下面的$file_name = strstr($file,'.',true)便是查找$file中‘.’的前面部分,如果是strstr($file,'.',false)则是'.'的后面部分。

动态数组的添加使用array_push就ok啦!

哦,不要忘记了关闭资源啊!有opendir($dir)那么就要有对应的closedir($dir)。

function listDir($dir, &$names, &$img_urls){	if(is_dir($dir))   	{     	if ($dh = opendir($dir)) 		{        	while (($file = readdir($dh)) !== false)			{				     			if((is_dir($dir."/".$file)) && $file!="." && $file!="..")				{     				//echo "文件名:",$file,"

"; listDir($dir."/".$file."/"); } else { $file = iconv("gb2312","UTF-8",$file); if($file!="." && $file!="..") { //var_dump($file); $file_name = strstr($file, '.', true); //echo $file_name."
"; array_push($names, $file_name); array_push($img_urls, $dir."/".$file); } } } closedir($dh); } }}
Copy after login


在train.php中使用如下代码便得到了训练用的所有图片的url啦。

$img_url = array();$person_name = array();$trainDir = $_GET["trainDir"];listDir($trainDir, $person_name, $img_url);echo "从目录中我们得到了 ".sizeof($img_url)." 张图片".<br>;
Copy after login
接着我们创建一个训练组oldpeople_qiaoxi。
$response = $facepp->execute('/group/delete', array('group_name' => 'oldpeople_qiaoxi'));$response = $facepp->execute('/group/create', array('group_name' => 'oldpeople_qiaoxi'));
Copy after login
接着做循环,对每张图片检测人脸

$params['img']          = $img;$params['attribute']    = 'gender,age,race,smiling,glass,pose';$response               = $facepp->execute('/detection/detect',$params);
Copy after login
从返回值$response得到face_id之后,创建一个person并将检测到的人脸加入这个类别person中。

$response = $facepp->execute('/person/delete', array('person_name' => $person_name[$i],'group_name' => 'oldpeople_qiaoxi'));$response = $facepp->execute('/person/create', array('person_name' => $person_name[$i],'group_name' => 'oldpeople_qiaoxi'));$response = $facepp->execute('/person/add_face', array('person_name' => $person_name[$i], 'face_id' => $face_id, 'group_name' => 'oldpeople_qiaoxi'));
Copy after login
将所有图片都处理好之后,便可以训练模型了。

$response = $facepp->execute('/train/identify', array('group_name' => 'oldpeople_qiaoxi'));
Copy after login
训练成功的话,结果如下:



测试部分

这个部分包括2个部分:

  • test.php负责选择测试图片
  • recognition.php负责将测试图片传到服务器并给出结果

test.php部分

显示如下:

选择文件部分用的是来做的,之后表单提交到recognition.php就好了。




recognition.php部分

首先得到图片的url。

$img_url = $_GET["testImgPath"];
Copy after login
之后执行identify得到身份结果。

$response = $facepp->execute('/recognition/identify', array('group_name' => 'oldpeople_qiaoxi', 'img' => $img_url));
Copy after login
这里采用了一个数组来进行中文名与英文名的对应,因为face++不支持中文名的图片,所以传给它的图片都是英文命名的,但是显示需要中文名,所以在这里进行映射。

$person_name = array("ami" => "艾米", "dongjian" => "张东健", "xiaowang" => "小王");
Copy after login







$img_url = array();$person_name = array();$trainDir = $_GET["trainDir"];listDir($trainDir, $person_name, $img_url);echo "从目录中我们得到了 ".sizeof($img_url)." 张图片".<br>;


版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Use java's File.getParent() function to get the parent path of the file Use java's File.getParent() function to get the parent path of the file Jul 24, 2023 pm 01:40 PM

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of the file Use java's File.getParentFile() function to get the parent directory of the file Jul 27, 2023 am 11:45 AM

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

See all articles