forked from buildar/getting_started_with_webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_processing_pipeline.html
265 lines (217 loc) · 8.69 KB
/
image_processing_pipeline.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>
<head>
<script>
/*
image_processing_pipeline.js by Rob Manson (buildAR.com)
This code is designed to help you explore how the Video/Canvas processing
pipeline works.
The coding style is focused on clearly describing the concepts and is not
designed to be used as production code.
The key concepts covered are:
- the Video/Canvas pipeline
- Array Buffers vs Views
- efficient frame buffer processing using multiple Views
HINT:
Search for "example:" to find useful examples below.
The MIT License
Copyright (c) 2013 Rob Manson, http://buildAR.com. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
///////////////////////////////////////////////
/* EDIT BELOW TO EXPERIMENT */
///////////////////////////////////////////////
// setup variables
var pipeline_processor_speed = 30; // processor iterations per second
var pipeline_width = 640;
var pipeline_height = 480;
// setup frame buffer processor
// - pipeline_callback() calls this function "pipeline_processor_speed" times per second
function example_processor(pipeline_image_data) {
// Key concept: Array Buffers vs Array Buffer Views
// Think of Buffers as long strings of bits
// Think of Typed Arrays as Views into the Buffers
// You can create Views in a specific format
// You can create Views for a specific range
// By using Views on top of a shared Buffer you reduce "copy" overhead
// allow us to turn the processor on and off
if (pipeline_filter_on) {
// create a view where each item is a 4 byte RGBA pixel
var pixels = new Uint32Array(pipeline_image_data.data.buffer);
// create a view where each item is an 8 bit channel
var channels = new Uint8ClampedArray(pipeline_image_data.data.buffer);
// Key concept: efficient frame buffer processing using multiple Views
// Both pixels and channels use the same underlying Buffer but fold the bits
// into different ways of seeing that data
// walk through all the pixels
for (var pixel in pixels) {
// get the channel item id for this pixel
var channel_id = 4*pixel;
// get RGBA channels
var r = channel_id+0;
var g = channel_id+1;
var b = channel_id+2;
var a = channel_id+3;
// calculate the average of all three RGB values
var rgb_avg = (channels[ r ] + channels[ g ] + channels[ b ]) / 3;
// subtract the rgb_avg from 255
//rgb_avg = 255-rgb_avg; // example: invert
// update all three channels to the average
channels[ r ] = rgb_avg; //+ 30; // example: sepia tone
channels[ g ] = rgb_avg;
channels[ b ] = rgb_avg;
// add random noise to the alpha channel
//channels[ a ] = Math.round(Math.random()*255); // example: random alpha noise
// chroma key 1 - make greener things transparent
//if (channels[ g ] > channels[ r ] && channels[ g ] > channels[ b ]) {
// channels[ a ] = 0; // example: make this transparent
//}
// chroma key 2 - make darker things transparent
//if (channels[ r ] < 75 && channels[ g ] < 75 && channels[ g ] < 75) {
// channels[ a ] = 0; // example: make this transparent
//}
}
// draw the update image data back into the canvas
pipeline_ctx.putImageData(pipeline_image_data, 0, 0);
}
}
///////////////////////////////////////////////
/* EDITING BELOW IS OPTIONAL */
///////////////////////////////////////////////
var pipeline_canvas = undefined;
var pipeline_ctx = undefined;
var pipeline_video = undefined;
var pipeline_interval = undefined;
var pipeline_filter_on = false;
// start the whole process by getting a stream
function start() { // called by body.onload
get_stream(document.getElementById("pipeline_canvas"));
}
// get a video stream
function get_stream(canvas) {
pipeline_canvas = canvas;
pipeline_canvas.width = pipeline_width; // set this otherwise it defaults to 300
pipeline_canvas.height = pipeline_height; // set this otherwise it defaults to 150
pipeline_ctx = pipeline_canvas.getContext("2d");
/*
var options = {
video:{
mandatory: {
width: { min: pipeline_width },
height: { min: pipeline_height }
},
// optional: [
// { frameRate: 60 },
// { facingMode: "user" }
// ]
},
audio:false
};
*/
var options = { video:true, audio:false };
get_user_media(options, create_vc_pipeline, got_error);
function got_error(error) {
console.log(error);
}
}
// create a Video/Canvas MediaStream pipeline
function create_vc_pipeline(stream) {
// Key concept: Video/Canvas MediaStream processing pipeline
// The Video/Canvas pipeline allows you to connect a stream in one end
// and have it regularly output image_data objects at the other end
pipeline_video = document.createElement("video");
connect_stream_to_src(stream, pipeline_video);
start_pipeline();
}
// create other types of Stream pipelines here
// ...Image Capture...
// function create_ic_pipeline(stream) { ... }
// ...Recording...
// function create_r_pipeline(stream) { ... }
// start the pipeline processor
function start_pipeline() {
// NOTE: You may prefer to use requestAnimationFrame() or setTimeout() here as needed
// setInterval() has been used in this demo for it's simplicity
pipeline_interval = setInterval(function() {
pipeline_callback();
},
1000/pipeline_processor_speed
);
}
// pipeline processor callback
function pipeline_callback() {
if (pipeline_ctx && pipeline_video) {
pipeline_ctx.drawImage(pipeline_video, 0, 0, pipeline_width, pipeline_height);
example_processor(pipeline_ctx.getImageData(0, 0, pipeline_width, pipeline_height)); // process frame
//console.log(pipeline_video.currentTime); // example: how to log video timestamps
// example: integrate other sensor data with timestamps here
}
}
// toggle filter on/off
function toggle_filter() {
pipeline_filter_on = !pipeline_filter_on;
}
// stop the pipeline processor
function stop_pipeline() {
clearInterval(pipeline_interval);
}
///////////////////////////////////////////////
/* GENERIC GUM POLY FILL */
///////////////////////////////////////////////
// setup get_user_media polyfill
var get_user_media = null;
var connect_stream_to_src = null;
var browser_type = null;
if (navigator.getUserMedia) { // WebRTC 1.0 standard compliant browser
get_user_media = navigator.getUserMedia.bind(navigator);
connect_stream_to_src = function(media_stream, media_element) {
media_element.srcObject = media_stream;
media_element.play();
};
browser_type = "webrtc";
} else if (navigator.mozGetUserMedia) { // early firefox webrtc implementation
get_user_media = navigator.mozGetUserMedia.bind(navigator);
connect_stream_to_src = function(media_stream, media_element) {
media_element.mozSrcObject = media_stream;
media_element.play();
};
browser_type = "firefox";
} else if (navigator.webkitGetUserMedia) { // early webkit webrtc implementation
get_user_media = navigator.webkitGetUserMedia.bind(navigator);
connect_stream_to_src = function(media_stream, media_element) {
media_element.src = webkitURL.createObjectURL(media_stream);
media_element.play();
};
browser_type = "webkit";
} else {
alert("This browser does not support WebRTC - visit WebRTC.org for more info");
}
</script>
<style>
#pipeline_canvas {
/* scale whole canvas here */
width: 640px;
height: 480px;
background-color: #FF0000;
}
</style>
</head>
<body onload="start()">
<p><canvas id="pipeline_canvas"></canvas></p>
<p><a href="#" onClick="toggle_filter()">TOGGLE FILTER</a> <-- View the source of this page to see what this does (HINT: search for "Key concept:" and "example:" to get started)</p>
</body>
</html>