-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
235 lines (199 loc) · 8.86 KB
/
index.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
<!doctype html>
<html>
<head>
<title>tracking.js - match patterns</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800&display=swap" rel="stylesheet">
<script src="js/tracking.js"></script>
<script src="js/adapter.js"></script>
<style>
.image-template {
position: absolute;
left: -1000px;
top: -1000px;
}
#video {
position: absolute;
top: -1000px;
}
.important-text {
font-size: 16px;
font-family: 'Open Sans', sans-serif;
font-weight: 800;
}
</style>
</head>
<body>
<div class="canvas-container">
<video id="video" preload autoplay loop muted controls playsinline></video>
<canvas id="canvas" width="800px" height="800px"></canvas>
</div>
<div class="important-text" id="match-result"></div>
<script>
window.onload = function() {
const cards = [
{ imageFile: 'tarot00.jpg', profile: 'scandal'},
{ imageFile: 'tarot01.jpg', profile: 'smash hit'},
{ imageFile: 'tarot02.jpg', profile: 'mother nature'},
{ imageFile: 'tarot03.jpg', profile: 'radio star'},
{ imageFile: 'tarot04.jpg', profile: 'siren'},
{ imageFile: 'tarot05.jpg', profile: 'bffs'},
{ imageFile: 'tarot06.jpg', profile: 'super fan'},
{ imageFile: 'tarot07.jpg', profile: 'big bad wolf'},
{ imageFile: 'tarot08.jpg', profile: 'forgotten'},
{ imageFile: 'tarot09.jpg', profile: 'service dog'},
{ imageFile: 'tarot10.jpg', profile: 'catalyst'},
{ imageFile: 'tarot11.jpg', profile: 'backstabber'},
{ imageFile: 'tarot12.jpg', profile: 'acesso'},
{ imageFile: 'tarot13.jpg', profile: 'escala'},
{ imageFile: 'tarot14.jpg', profile: 'uso'}
];
var populateImages = function() {
const container = document.getElementsByClassName('canvas-container')[0];
for(let i=0; i<cards.length; i++) {
const image = document.createElement('img');
image.classList.add('image-template');
image.setAttribute('id', cards[i].imageFile.replace('.jpg', ''));
image.setAttribute('src', 'imgs/' + cards[i].imageFile);
container.appendChild(image);
}
}
populateImages();
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const images = document.getElementsByClassName('image-template');
const video = document.getElementById('video');
const matchResult = document.getElementById('match-result');
const templateWidth = 240;
const templateHeight = 368;
const OBJECT_CONFIDENCE_EPSILON = 0.04;
const MINIMUM_CORNER_MATCH_COUNT = 6;
const templateCorners = [];
const templateDescriptors = [];
window.descriptorLength = 512;
window.blurRadius = 2.01;
window.trackerThreshold = 80;
let videoWidth = 640;
let videoHeight = 360;
let canvasWidth = videoWidth;
let canvasHeight = videoHeight;
var setupCameraCapture = function() {
const vidParams = { facingMode: 'environment' };
navigator.getUserMedia({video: vidParams}, function(stream) {
let mVideo = document.getElementById('video');
mVideo.srcObject = stream;
mVideo.onloadedmetadata = function(e) {
mVideo.play();
let mCameraSettings = stream.getVideoTracks()[0].getSettings();
videoWidth = mVideo.videoWidth;
videoHeight = mVideo.videoHeight;
canvasWidth = videoWidth;
canvasHeight = videoHeight;
canvas.setAttribute("width", canvasWidth + "px");
canvas.setAttribute("height", canvasHeight + "px");
video.setAttribute("width", videoWidth + "px");
video.setAttribute("height", videoHeight + "px");
};
}, function(err) {
alert(err.name);
console.log("The following error occurred in getUserMedia: " + err.name);
});
}
setupCameraCapture();
var getTemplateDescriptors = function() {
tracking.Brief.N = window.descriptorLength;
for(let i=0; i<images.length; i++) {
context.drawImage(images[i], 0, 0, templateWidth, templateHeight);
const imageData = context.getImageData(0, 0, templateWidth, templateHeight);
const tBlur = tracking.Image.blur(imageData.data, templateWidth, templateHeight, window.blurRadius);
const tGray = tracking.Image.grayscale(tBlur, templateWidth, templateHeight);
const tCorners = tracking.Fast.findCorners(tGray, templateWidth, templateHeight);
const tDescriptors = tracking.Brief.getDescriptors(tGray, templateWidth, tCorners);
templateCorners.push(tCorners);
templateDescriptors.push(tDescriptors);
context.clearRect(0, 0, canvas.width, canvas.height);
}
}
getTemplateDescriptors();
var debugCorners = function() {
for(let j=0; j<images.length; j++) {
context.drawImage(images[j],
j*templateWidth, 0, templateWidth, templateHeight);
for(let i=0; i<templateCorners[j].length; i+=2) {
context.fillStyle = '#f00';
context.fillRect(templateCorners[j][i] + j*templateWidth,
templateCorners[j][i + 1], 3, 3);
}
}
}
debugCorners();
var TarotTracker = function() {
TarotTracker.base(this, 'constructor');
}
tracking.inherits(TarotTracker, tracking.Tracker);
TarotTracker.prototype.fastThreshold = window.trackerThreshold;
TarotTracker.prototype.track = function(pixels, mWidth, mHeight) {
const frameBlur = tracking.Image.blur(pixels, mWidth, mHeight, window.blurRadius / 2.0);
const frameGrayscale = tracking.Image.grayscale(frameBlur, mWidth, mHeight);
const frameKeypoints = tracking.Fast.findCorners(frameGrayscale, mWidth, mHeight, this.fastThreshold);
const frameDescriptors = tracking.Brief.getDescriptors(frameGrayscale, mWidth, frameKeypoints);
const matchResults = [];
for(let i=0; i<templateDescriptors.length; i++) {
const recMatch = tracking.Brief.reciprocalMatch(templateCorners[i], templateDescriptors[i],
frameKeypoints, frameDescriptors);
recMatch.sort(function(a, b) {
return b.confidence - a.confidence;
});
let confidenceSum = 0;
for(let j=0; j<recMatch.length; j++){
confidenceSum += recMatch[j].confidence;
}
matchResults.push({
index: i,
match: recMatch,
length: recMatch.length,
confidence: (recMatch.length > 0) ? confidenceSum / recMatch.length : 0
});
}
matchResults.sort(function(a, b) {
return b.confidence - a.confidence;
});
// good match := highest confidence larger than (next-highest + ε) && more than 10 matches
const goodMatch = ((matchResults[0].confidence - matchResults[1].confidence) > OBJECT_CONFIDENCE_EPSILON) && (matchResults[0].length > MINIMUM_CORNER_MATCH_COUNT);
this.emit('track', {
data: goodMatch ? matchResults[0] : { match: [] }
});
};
const tracker = new TarotTracker();
tracker.on('track', function(event) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(video, 0, 0, videoWidth, videoHeight);
// Plots matching points on canvas
for (let i = 0; i < Math.min(64, event.data.match.length); i++) {
const frame = event.data.match[i].keypoint2;
context.fillStyle = '#f00';
context.fillRect(frame[0], frame[1], 3, 3);
}
if(event.data.match.length > 0) {
console.log(cards[event.data.index]);
matchResult.innerHTML = cards[event.data.index].profile;
setTimeout(function() {
trackerTask.stop();
setTimeout(function() { trackerTask.run(); }, 200);
}, 100)
}
});
let trackerTask = tracking.track('#video', tracker, { camera: true, fps: 10 });
trackerTask.stop();
document.getElementById('canvas').addEventListener('click', function() {
if(trackerTask.inRunning()) {
trackerTask.stop();
} else {
trackerTask.run();
}
});
}
</script>
</body>
</html>