face_search.ftl
8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<div class="layui-row">
<div class="layui-col-xs7 layui-col-md-offset3" align="center">
<div style="margin: 0,auto; width: 800px;height: 80px;background-color: #383939">
<div style="margin: 0,auto;height: 80px;text-align:center;line-height:80px;font-size: 40px;color: #E51C23">
人脸识别系统
</div>
</div>
<div id="regcoDiv">
</div>
<div>
<table frame="void">
<tr>
<td><button title="人脸识别" value="人脸识别" onclick="getMedia2()"
style="color:#FFFFFF;height: 30px;display:block;margin:0 auto;margin-top:10px;width:120px;background-color: #3F51B5;border-radius:5px;text-align: center;line-height: 30px;font-size: 20px" >摄像头识别</button></td>
<td><button style="color:#FFFFFF;height: 30px;display:block;margin:0 auto;margin-top:10px;width:120px;background-color: #3F51B5;border-radius:5px;text-align: center;line-height: 30px;font-size: 20px" onclick="imageTo()">照片识别</button></td>
</tr>
<tr><td colspan="2"><button id="snap" onclick="chooseFileChangeComp()" style="color:#FFFFFF;height: 30px;display:block;margin:0 auto;margin-top:10px;width:100px;background-color: #3F51B5;border-radius:5px;text-align: center;line-height: 30px;font-size: 20px">提交</button></td></tr>
</table>
</div>
<div>
<img id="imageDivComp" />
</div>
</div>
</div>
<script>
function chooseFile() {
$("#file1").trigger('click');
}
function imageTo()
{
$("#regcoDiv").empty();
let imageInput = "<h2>点击图片区域上传文件</h2><input style='display: none' type='file' name='file1' id='file1' multiple='multiple' /><br><img src='images/shibie.jpg' onclick='chooseFile()' id='img0' style='width: 30rem;height: 25rem;'>";
$("#regcoDiv").append(imageInput);
}
$(document).on("change","#file1",function(){
var objUrl = getObjectURL(this.files[0]) ;//获取文件信息
console.log("objUrl = "+objUrl);
if (objUrl) {
$("#img0").attr("src", objUrl);
}
});
function getObjectURL(file) {
var url = null;
if (window.createObjectURL!=undefined) {
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
$("#imageDivComp").click(function () {
$("#chooseFileComp").click();
});
function getMedia2() {
$("#regcoDiv").empty();
let vedioComp = "<video id='video2' width='500px' height='500px' autoplay='autoplay' muted=true style='margin-top: 20px'></video><canvas id='canvas2' width='500px' height='500px' style='display: none'></canvas>";
$("#regcoDiv").append(vedioComp);
let constraints = {
video: {width: 500, height: 500},
// audio: true
};
//获得video摄像头区域
let video = document.getElementById("video2");
//这里介绍新的方法,返回一个 Promise对象
// 这个Promise对象返回成功后的回调函数带一个 MediaStream 对象作为其参数
// then()是Promise对象里的方法
// then()方法是异步执行,当then()前的方法执行完后再执行then()内部的程序
// 避免数据没有获取到
let promise = navigator.mediaDevices.getUserMedia(constraints);
promise.then(function (MediaStream) {
video.srcObject = MediaStream;
video.play();
});
// var t1 = window.setTimeout(function() {
// chooseFileChangeComp()
// },2000)
}
function chooseFileChangeComp() {
let regcoDivComp = $("#regcoDiv");
if(regcoDivComp.has('video').length)
{
let video = document.getElementById("video2");
let canvas = document.getElementById("canvas2");
let ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, 500, 500);
var base64File = canvas.toDataURL();
var formData = new FormData();
formData.append("groupId", "101")
formData.append("file", base64File);
$.ajax({
type: "post",
url: "/faceSearch",
data: formData,
contentType: false,
processData: false,
async: false,
success: function (text) {
var res = JSON.stringify(text)
if (text.code == 0) {
var name = text.data.name;
$("#nameDiv").html("姓名:" + name);
var similar = text.data.similarValue;
$("#similarDiv").html("相似度:" + similar + "%");
var age = text.data.age;
$("#ageDiv").html("年龄:" + age);
var gender = text.data.gender;
$("#genderDiv").html("性别:" + gender);
// img.css("background-image", 'url(' + text.data.image + ')');
alert("姓名:" + name +"\n相似度:" + similar + "%" + "\n年龄:" + age +"\n性别:" + gender);
} else {
$("#nameDiv").html("");
$("#similarDiv").html("");
$("#ageDiv").html("");
$("#genderDiv").html("");
alert("人脸不匹配")
}
},
error: function (error) {
$("#nameDiv").html("");
$("#similarDiv").html("");
$("#ageDiv").html("");
$("#genderDiv").html("");
alert(JSON.stringify(error))
}
});
}
else
{
var file = $("#file1")[0].files[0];
if (file == undefined) {
alert("请选择有人脸的图片进行识别");
return;
}
var formData = new FormData();
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
var base64 = reader.result;
formData.append("file", base64);
formData.append("groupId", 101);
$.ajax({
type: "post",
url: "/faceSearch",
data: formData,
contentType: false,
processData: false,
async: false,
success: function (text) {
var res = JSON.stringify(text)
if (text.code == 0) {
var name = text.data.name;
// $("#nameDiv").html("姓名:" + name);
var similar = text.data.similarValue;
// $("#similarDiv").html("相似度:" + similar + "%");
var age = text.data.age;
// $("#ageDiv").html("年龄:" + age);
var gender = text.data.gender;
// $("#genderDiv").html("性别:" + gender);
// img.css("background-image", 'url(' + text.data.image + ')');
alert("姓名:" + name +"\n相似度:" + similar + "%" + "\n年龄:" + age +"\n性别:" + gender);
} else {
$("#nameDiv").html("");
$("#similarDiv").html("");
$("#ageDiv").html("");
$("#genderDiv").html("");
alert("人脸不匹配")
}
},
error: function (error) {
$("#nameDiv").html("");
$("#similarDiv").html("");
$("#ageDiv").html("");
$("#genderDiv").html("");
alert(JSON.stringify(error))
}
});
}
}
}
</script>