srivatsavdamaraju commited on
Commit
cbb4878
·
verified ·
1 Parent(s): 909bea7

Upload 2 files

Browse files
Files changed (2) hide show
  1. midas.tflite +3 -0
  2. script.js +109 -0
midas.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:93d871071edff1218973ce25ee27ce95ccd20450c70a55e1b89efa3f5a772cdd
3
+ size 66338288
script.js ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ let currentStream = null;
2
+ let currentFacingMode = 'user';
3
+
4
+ async function loadTFLiteModel() {
5
+ try {
6
+ const modelUrl = 'midas.tflite';
7
+ const wasmPath = await tflite.setWasmPath('https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/wasm/');
8
+ const model = await tflite.loadTFLiteModel(modelUrl);
9
+ console.log('Model loaded successfully');
10
+ return model;
11
+ } catch (error) {
12
+ console.error('Error loading TFLite model:', error);
13
+ }
14
+ }
15
+
16
+ function preprocessImage(imageElement) {
17
+ try {
18
+ const tensor = tf.browser.fromPixels(imageElement).toFloat();
19
+ const resized = tf.image.resizeBilinear(tensor, [256, 256]); // Resize to 256x256 as required by the model
20
+ const normalized = resized.div(255.0).expandDims(0); // Normalize to [0,1] and add batch dimension
21
+ return normalized;
22
+ } catch (error) {
23
+ console.error('Error during image preprocessing:', error);
24
+ }
25
+ }
26
+
27
+ async function predictDepth(model, preprocessedImage) {
28
+ try {
29
+ const depthMap = model.predict(preprocessedImage);
30
+ const squeezed = depthMap.squeeze(); // Remove batch dimension
31
+ const normalizedDepthMap = squeezed.div(squeezed.max()).mul(255).toInt(); // Normalize the depth map to [0,255]
32
+ return normalizedDepthMap;
33
+ } catch (error) {
34
+ console.error('Error during depth prediction:', error);
35
+ }
36
+ }
37
+
38
+ function renderDepthMap(depthMap, canvasElement) {
39
+ try {
40
+ const [width, height] = [depthMap.shape[1], depthMap.shape[0]];
41
+ const imageData = new ImageData(width, height);
42
+
43
+ const data = depthMap.dataSync();
44
+
45
+ for (let i = 0; i < data.length; i++) {
46
+ const value = data[i];
47
+ imageData.data[4 * i] = value; // R
48
+ imageData.data[4 * i + 1] = value; // G
49
+ imageData.data[4 * i + 2] = value; // B
50
+ imageData.data[4 * i + 3] = 255; // A
51
+ }
52
+
53
+ const ctx = canvasElement.getContext('2d');
54
+ canvasElement.width = width;
55
+ canvasElement.height = height;
56
+ ctx.putImageData(imageData, 0, 0);
57
+ } catch (error) {
58
+ console.error('Error rendering depth map:', error);
59
+ }
60
+ }
61
+
62
+ function analyzeDepth(depthMap) {
63
+ try {
64
+ const depthArray = depthMap.dataSync();
65
+ const averageDepth = depthArray.reduce((a, b) => a + b, 0) / depthArray.length;
66
+ console.log('Average Depth:', averageDepth);
67
+ document.getElementById('averageDepth').innerText = `Average Depth: ${averageDepth.toFixed(2)}`;
68
+ } catch (error) {
69
+ console.error('Error analyzing depth:', error);
70
+ }
71
+ }
72
+
73
+ async function startVideo(facingMode = 'user') {
74
+ const video = document.getElementById('video');
75
+
76
+ if (currentStream) {
77
+ currentStream.getTracks().forEach(track => track.stop());
78
+ }
79
+
80
+ try {
81
+ const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode } });
82
+ currentStream = stream;
83
+ video.srcObject = stream;
84
+
85
+ video.addEventListener('loadeddata', async () => {
86
+ const model = await loadTFLiteModel();
87
+
88
+ if (model) {
89
+ setInterval(async () => {
90
+ const preprocessedImage = preprocessImage(video);
91
+ const depthMap = await predictDepth(model, preprocessedImage);
92
+
93
+ const canvas = document.getElementById('depthCanvas');
94
+ renderDepthMap(depthMap, canvas);
95
+ analyzeDepth(depthMap);
96
+ }, 500); // Run depth prediction every 500ms
97
+ }
98
+ });
99
+ } catch (error) {
100
+ console.error('Error accessing the camera:', error);
101
+ }
102
+ }
103
+
104
+ document.getElementById('swapButton').addEventListener('click', () => {
105
+ currentFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
106
+ startVideo(currentFacingMode);
107
+ });
108
+
109
+ startVideo();