-
Notifications
You must be signed in to change notification settings - Fork 0
/
index1.html
226 lines (206 loc) · 9.07 KB
/
index1.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
<!DOCTYPE html>
<html lang="en">
<!-- faceapi javascript -->
<script src="faceapi_commons.js"></script>
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="faceapi_exif.js"></script>
<script type="text/javascript" src="face_detection.js"></script>
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<!-- for proper image sizing and analyzing -->
<script type="text/javascript" src="faceapi_display.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="assets/css/style.css" />
<title>SurFace</title>
</head>
<body>
<header>
<ul class="main-nav">
<li class="active"><a href="">HOME</a></li>
<li><a href="index1.html">COMPARE</a></li>
<li><a href="index.html">EMOTION</a></li>
</ul>
<div><h1>S u r F a c e</h1></div>
</header>
<div class="container" id="container">
<form action="#">
<div id="box">
<button type="button" id="search" class="btn btn-secondary">Photo Search</button>
<!-- <input id="people-search" type="text"> -->
</div>
</form>
<!-- <div id="random-photo"></div> -->
<div id="imageHolder">
<!-- src links to random pexels img url -->
<!-- <img id="preview" src="https://images.pexels.com/photos/415829/pexels-photo-415829.jpeg"> -->
<!-- <div class="card-header">
Random Photograph
</div> -->
</div>
</div>
<hr>
<h3 id="photos">Similar Faces</h3>
<div id="similar-photos"></div>
<script>
// Face++ (API Key, API Secret Key, 1)
var facepp = new FACEPP('tofb2KC3K9WNkY2-A_VISLJVjWMQh5Df', 'xMmnpBZx-r-3xNFAZJUGKXLnLAEFG4Ls', 1);
$("button").on("click", function () {
var queryURL = "https://api.pexels.com/v1/search?query=face&per_page=25&page=1";
$.ajax({
url: queryURL,
method: "GET",
beforeSend: function(xhr){xhr.setRequestHeader("authorization", "563492ad6f91700001000001ef9932d9916848898cc2c5b1b0ec5725");}
}).then(function (response) {
// photo from pexels request
var photo = response.photos;
var image = $("<img>");
var random_int = Math.floor(Math.random() * (25 + 1));
image.attr({
src: photo[random_int].src.medium
});
// puts response image in #imageHolder
$("#imageHolder").empty();
$("#imageHolder").append(image);
console.log(photo);
console.log(photo[random_int].src.medium);
// URL ACCESS
let dic = {'image_url' : photo[random_int].src.medium};
facepp.detectFace(dic,success, failed);
function selectImage(){
let imageView = document.getElementById('imageHolder');
const reader = new FileReader();
reader.readAsDataURL(photo[random_int].src.medium);
reader.onload = function (e) {
const readImage = e.target.result;
let imageData = facepp.dataURItoBlob(readImage);
let attributes = 'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus';
let dataDic = {'image_url': imageData, 'return_landmark': 2, 'return_attributes': attributes};
facepp.detectFace(dataDic, success, failed);
}
}
// If Dic is successful
function success(e){
console.log("Success! A face was detected");
console.log(e);
let textView = document.getElementById('text');
// Shows stringified detection object in #text
textView.innerText = JSON.stringify(e,null,"\t");
let imageView = document.getElementById('imageHolder');
const faces = e.faces;
for (const index in faces){
const face = faces[index];
const face_rectangle = face.face_rectangle;
console.log(face_rectangle);
// var canvas = document.getElementById('imageHolder');
// var ctx = canvas.getContext("2d");
// ctx.fillStyle = "#FF0000";
// ctx.fillRect(0, 0, 190,75);
// var gender = face.attributes.gender.value;
// console.log("Gender: " + gender);
// emotion object
// returns a number value (0-100)
// var emotion = face.attributes.emotion;
// var anger = emotion.anger;
// var disgust = emotion.disgust;
// var sadness = emotion.sadness;
// var surprise = emotion.surprise;
// var fear = emotion.fear;
// var happiness = emotion.happiness;
// console.log("emotion response: " + emotion);
// changes border color of face container based on gender
// const borderColor = (gender == 'Male') ? 'blue' : 'red';
// console.log('face frame color:' + borderColor);
// gets coordinates of face area
var faceX = face_rectangle.left;
console.log(faceX);
var faceY = face_rectangle.top;
var faceW = face_rectangle.width;
var faceH = face_rectangle.height;
//square face container
var width = 320;
var height = 320;
var imageW = imageView.width;
var imageH = imageView.height;
var naturalWidth = imageView.naturalWidth;
var naturalHeight = imageView.naturalHeight;
const scale = imageW / naturalWidth;
console.log('scale > ' + scale);
// scales image
const offsetX = (width - imageW)*0.5;
const offsetY = (height - imageH)*0.5;
$('<div>').css({
position: 'absolute',
top: faceY * scale + offsetY,
left: faceX * scale + offsetX,
height: faceH * scale,
width: faceW * scale,
border: '2px solid black',
// transform: 'rotate(' + roll + 'deg)'
}).appendTo($("#imageHolder"));
}
}
//if this fails --
function failed(e){
console.log("Failed! No face detected")
console.log(e);
let textView = document.getElementById('text');
textView.innerText = JSON.stringify(e);
}
//orientation for face area container
function fixOrientention(readImage,imageView) {
const image = new Image();
image.onload = () => {
const canvas = document.createElement('canvas');
const initSize = image.src.length;
let width = image.naturalWidth;
console.log(width);
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:
canvas.height = height;
canvas.width = width;
ctx.drawImage(image, 0, 0, width, height);
break;
case 6:
canvas.height = width;
canvas.width = height;
ctx.rotate(Math.PI / 2);
ctx.translate(0, -height);
ctx.drawImage(image, 0, 0, width, height);
break;
case 3:
canvas.height = height;
canvas.width = width;
ctx.rotate(Math.PI);
ctx.translate(-width, -height);
ctx.drawImage(image, 0, 0, width, height);
break;
case 8:
canvas.height = width;
canvas.width = height;
ctx.rotate(-Math.PI / 2);
ctx.translate(-width, 0);
ctx.drawImage(image, 0, 0, width, height);
break;
default:
canvas.height = height;
canvas.width = width;
ctx.drawImage(image, 0, 0, width, height);
break;
}
});
// canvas method
var newBase64 = canvas.toDataURL('image/jpeg', 1.0);
imageView.src = newBase64;
};
image.src = readImage;
}
});
});
</script>
</body>
</html>