srivatsavdamaraju commited on
Commit
a75c56e
·
verified ·
1 Parent(s): 53e59d2

Create bestscript.txt

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