基于opencv的人脸检测的web应用

时间:2023-03-09 15:30:39
基于opencv的人脸检测的web应用

参考资料

https://github.com/bsdnoobz/web-based-face-detect

http://opencv-code.com/projects/web-based-interface-for-face-detection-with-opencv/

http://www.cnblogs.com/findingsea/archive/2012/03/31/2427833.html

流程如下图

基于opencv的人脸检测的web应用

背景知识

php调用exe的返回

<html>
  <body>
      <?php
        system("F:\\xampp\\htdocs\\webcam2\\phptest.exe", $info);
        echo $info;
      ?>
  </body>
</html>

基于opencv的人脸检测的web应用

基于opencv的人脸检测的web应用

在线人脸检测

目录结构

│  face-detect.exe
│ haarcascade_frontalface_alt.xml
│ shutter.mp3
│ test.html
│ test.php
│ testphp.php
│ webcam.js
│ webcam.swf

└─out

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JPEGCam Test Page</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Joseph Huckaby">
<!-- Date: 2008-03-15 -->
</head>
<body>
<table><tr><td valign=top>
<h1>JPEGCam Test Page</h1>
<h3>Demonstrates a very simple, one-click capture &amp; upload implementation</h3> <!-- First, include the JPEGCam JavaScript Library -->
<script type="text/javascript" src="webcam.js"></script> <!-- Configure a few settings -->
<script language="JavaScript">
webcam.set_api_url( 'test.php' );
webcam.set_quality( 90 ); // JPEG quality (1 - 100)
webcam.set_shutter_sound( true ); // play shutter click sound
</script> <!-- Next, write the movie to the page at 320x240 -->
<script language="JavaScript">
document.write( webcam.get_html(320, 240) );
</script> <!-- Some buttons for controlling things -->
<br/><form>
<input type=button value="Configure..." onClick="webcam.configure()">
&nbsp;&nbsp;
<input type=button value="Take Snapshot" onClick="webcam.snap()">
</form>
<img id="resultimg" src="" /> <script language="JavaScript">
webcam.set_hook( 'onComplete', 'my_callback_function' );
function my_callback_function(response) {
// alert("sucess " + response);
document.getElementById("resultimg").src=""+response;
webcam.reset();
}
</script> </body>
</html>

test.php

<?php

/* JPEGCam Test Script */
/* Receives JPEG webcam submission and saves to local file. */
/* Make sure your directory has permission to write files as your web server user! */ $filename = date('YmdHis') . '.jpg';
$result = file_put_contents( $filename, file_get_contents('php://input') );
$cmd = 'F://xampp//htdocs//face//face-detect//face-detect.exe --input="'.$filename.'" --outdir=out"';
exec($cmd, $info);
foreach ($info as $i => $line)
{
if (preg_match('/\d+,\s*([^\s]+)\s+\((\d+)x(\d+)\)/i', $line, $m)) {
// print 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/".$m[1];
// echo "<br>";
// echo $m[2];
// echo $m[3];
}
}
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/' . $filename;
print "$url\n";

实验结果

基于opencv的人脸检测的web应用

out目录里就有检测的脸了

source code download

http://pan.baidu.com/share/link?shareid=4035488383&uk=792406754

修改人脸检测自带的php文件

<?php
$file = 'uploads/'.date('YmdHis').'.jpg';;
file_put_contents("$file", file_get_contents("php://input"));
$cmd = 'F://xampp//htdocs//face//face-detect//face-detect.exe --input="'.$file.'" --outdir=uploads"';
// $cmd = 'F://xampp//htdocs//face//face-detect//face-detect.exe --input=uploads//1.jpg --outdir=uploads';
// $last = exec($cmd, &$out);
$last = exec($cmd, $out);
if (strpos($last, 'Error:') === false)
{
$res = array('success' => true);
foreach ($out as $i => $line)
{
if (preg_match('/\d+,\s*([^\s]+)\s+\((\d+)x(\d+)\)/i', $line, $m))
$res['images'][] = array('src' => $m[1], 'width' => $m[2], 'height' => $m[3]);
}
}
else
$res = array('success' => false, 'msg' => $last);
header('Content-Type: application/json');
echo json_encode($res);

效果

基于opencv的人脸检测的web应用