-
Notifications
You must be signed in to change notification settings - Fork 27
/
detectBody.html
265 lines (209 loc) · 7.76 KB
/
detectBody.html
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>人体检测</title>
<script type="text/javascript" src="facepp_sdk/jquery.min.js"></script>
<script type="text/javascript" src="facepp_sdk/exif.js"></script>
<script type="text/javascript" src="facepp_sdk/facepp_sdk.js"></script>
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script type="text/javascript" src="common.js"></script>
<link rel="stylesheet" type="text/css" href="common.css">
</head>
<style>
#input{
opacity: 0;
}
#bodyContainer{
position: absolute;
top: 84px;
width: 320px;
height: 320px;
line-height: 320px;
left: 0;
right: 0;
background-color: darkgray;
text-align: center;
margin: 0 auto;
}
#preview{
vertical-align: middle;
max-width: 100%;
max-height: 100%;
}
#text{
left: 5%;
position: absolute;
top: 414px;
width: 90%;
height: 200px;
}
</style>
<body>
<div class="container">
<div class="button" onclick=clickInput()>选择人体照片</div>
<input id="input" type="file" accept="image/*" name="xaunz" onchange="selectImage(this)"/>
<div id="bodyContainer">
<img id="preview" />
</div>
<textarea id="text" readonly="readonly"></textarea>
</div>
</body>
</html>
<script>
resetContainer();
//点击选择图片
function clickInput(){
document.getElementById('input').click();
}
</script>
<script>
var facepp = new FACEPP(APIKEY,APISERET,1);
/*
// 以图片URL的方式上传图片
let dic = {'image_url' : 'https://www.faceplusplus.com.cn/scripts/demoScript/images/demo-pic6.jpg'};
facepp.detectFace(dic,success,failed);
*/
// 选择照片
function selectImage(input){
let imageView = document.getElementById('preview');
const reader = new FileReader();
reader.readAsDataURL(input.files[0]);
reader.onload = function (e) {
//移除之前的人体框
$("#bodyContainer div").remove();
//图片的base64数据
const base64Image = e.target.result;
//显示图片
//修复显示方向不对问题
fixOrientention(base64Image,imageView);
/*
//base64方式上传图片
let dic = {'image_base64' : base64Image};
facepp.detectFace(dic,success,failed);
*/
// 以二进制的方式上传图片
// 将base64转为二进制
let imageData = facepp.dataURItoBlob(base64Image);
//根据业务需求填写的参数
let attributes = 'gender,upper_body_cloth,lower_body_cloth';
//上传图片,获取结果
let dataDic = {'image_file':imageData,'return_attributes':attributes};
//调用接口,检测人体
facepp.bodyDetect(dataDic,success,failed);
}
}
//成功的回调
function success(e){
//显示结果
console.log(e);
let textView = document.getElementById('text');
textView.innerText = JSON.stringify(e,null,"\t");
// 画上人体框,男女颜色不一样
let imageView = document.getElementById('preview');
const bodies = e.humanbodies;
for (const index in bodies){
const body = bodies[index];
const body_rectangle = body.humanbody_rectangle;
//人体坐标
var bodyX = body_rectangle.left;
var bodyY = body_rectangle.top;
var bodyW = body_rectangle.width;
var bodyH = body_rectangle.height;
//bodyContainer尺寸
var width = 320;
var height = 320;
//img尺寸
var imageW = imageView.width;
var imageH = imageView.height;
//图片实际尺寸
var naturalWidth = imageView.naturalWidth;
var naturalHeight = imageView.naturalHeight;
console.log('container尺寸' + width + '----' + height);
console.log('img尺寸' + imageW + '----' + imageH);
console.log('图片实际尺寸' + naturalWidth + '----' + imageH);
const scale = imageW / naturalWidth;
console.log('scale > ' + scale);
const offsetX = (width - imageW)*0.5;
const offsetY = (height - imageH)*0.5;
console.log('offsetX:' + offsetX + 'offsetY' + offsetY);
//添加人体框
$('<div/>').css({
position: 'absolute',
top: bodyY * scale + offsetY,
left: bodyX * scale + offsetX,
height: bodyH * scale,
width: bodyW * scale,
border: '2px solid blue',
}).appendTo($("#bodyContainer"));
}
}
//失败的回调
function failed(e){
console.log(e);
let textView = document.getElementById('text');
textView.innerText = JSON.stringify(e);
}
//图片方向矫正
function fixOrientention(base64Image,imageView) {
const image = new Image();
image.onload = () => {
const canvas = document.createElement('canvas');
const initSize = image.src.length;
let width = image.naturalWidth;
let height = image.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 旋转图片操作
EXIF.getData(image, function () {
const orientation = EXIF.getTag(this, 'Orientation');
console.log(`orientation:${orientation}`);
switch (orientation) {
// 正常状态
case 1:
console.log('旋转0°');
canvas.height = height;
canvas.width = width;
ctx.drawImage(image, 0, 0, width, height);
break;
// 旋转90度
case 6:
console.log('旋转90°');
canvas.height = width;
canvas.width = height;
ctx.rotate(Math.PI / 2);
ctx.translate(0, -height);
ctx.drawImage(image, 0, 0, width, height);
break;
// 旋转180°
case 3:
console.log('旋转180°');
canvas.height = height;
canvas.width = width;
ctx.rotate(Math.PI);
ctx.translate(-width, -height);
ctx.drawImage(image, 0, 0, width, height);
break;
// 旋转270°
case 8:
console.log('旋转270°');
canvas.height = width;
canvas.width = height;
ctx.rotate(-Math.PI / 2);
ctx.translate(-width, 0);
ctx.drawImage(image, 0, 0, width, height);
break;
default:
console.log('default 旋转0°');
canvas.height = height;
canvas.width = width;
ctx.drawImage(image, 0, 0, width, height);
break;
}
});
var newBase64 = canvas.toDataURL('image/jpeg', 1.0);
imageView.src = newBase64;
};
image.src = base64Image;
}
</script>