meghdeepmondal commited on
Commit
3bd7fe8
·
verified ·
1 Parent(s): ef3816d

Upload 3 files

Browse files
Files changed (3) hide show
  1. index.html +30 -0
  2. main.css +66 -0
  3. main.js +197 -0
index.html ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <title>Roboflow Demo</title>
6
+
7
+ <meta name="viewport" content="width=640, user-scalable=no" />
8
+
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
10
+ integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
11
+ crossorigin="anonymous"></script>
12
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
13
+ integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww=="
14
+ crossorigin="anonymous"></script>
15
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/async/3.2.0/async.min.js"
16
+ integrity="sha512-6K6+H87tLdCWvY5ml9ZQXLRlPlDEt8uXmtELhuJRgFyEDv6JvndWHg3jadJuBVGPEhhA2AAt+ROMC2V7EvTIWw=="
17
+ crossorigin="anonymous"></script>
18
+
19
+ <script src="https://cdn.roboflow.com/0.2.26/roboflow.js"></script>
20
+
21
+ <link rel="stylesheet" href="./main.css">
22
+ <script src="./main.js"></script>
23
+ </head>
24
+
25
+ <body class="loading">
26
+ <video id="video" autoplay muted playsinline></video>
27
+ <div id="fps"></div>
28
+ </body>
29
+
30
+ </html>
main.css ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ html, body {
2
+ background-color: black;
3
+ color: white;
4
+ font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
5
+ }
6
+
7
+ body video {
8
+ transition: filter 250ms linear;
9
+ }
10
+
11
+ body.loading video {
12
+ filter: grayscale(1) brightness(0.25);
13
+ }
14
+
15
+ body.loading:before {
16
+ content: "Loading Model...";
17
+ color: white;
18
+ text-align: center;
19
+ width: 100%;
20
+ position: absolute;
21
+ top: 20px;
22
+ font-size: 3em;
23
+ font-weight: bold;
24
+ z-index: 100;
25
+ }
26
+
27
+ html, body, video, canvas {
28
+ width: 100%;
29
+ height: 100%;
30
+ margin: 0;
31
+ padding: 0;
32
+ }
33
+
34
+ video, canvas {
35
+ position: fixed;
36
+ top: 0;
37
+ left: 0;
38
+ }
39
+
40
+ body:after {
41
+ content: "";
42
+ position: fixed;
43
+ bottom: 20px;
44
+ right: 20px;
45
+ width: 350px;
46
+ height: 150px;
47
+ z-index: 1;
48
+ background-image: url("https://uploads-ssl.webflow.com/5eca8e43c4dfff837f0f6392/5ecad58e650fb5ec53e8b811_roboflow_assets_logo_white.png");
49
+ background-size: contain;
50
+ background-repeat: no-repeat;
51
+ background-position: bottom right;
52
+ }
53
+
54
+ #fps {
55
+ position: fixed;
56
+ bottom: 10px;
57
+ left: 10px;
58
+ }
59
+
60
+ #fps:empty {
61
+ display: none;
62
+ }
63
+
64
+ #fps:after {
65
+ content: " fps";
66
+ }
main.js ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*jshint esversion:6*/
2
+
3
+ $(function () {
4
+ const video = $("video")[0];
5
+
6
+ var model;
7
+ var cameraMode = "environment"; // or "user"
8
+
9
+ const startVideoStreamPromise = navigator.mediaDevices
10
+ .getUserMedia({
11
+ audio: false,
12
+ video: {
13
+ facingMode: cameraMode
14
+ }
15
+ })
16
+ .then(function (stream) {
17
+ return new Promise(function (resolve) {
18
+ video.srcObject = stream;
19
+ video.onloadeddata = function () {
20
+ video.play();
21
+ resolve();
22
+ };
23
+ });
24
+ });
25
+
26
+ var publishable_key = "rf_wXetABwdpDSNel2bzZ9iExUOjbx1";
27
+ var toLoad = {
28
+ model: "sic-project-0001",
29
+ version: 2
30
+ };
31
+
32
+ const loadModelPromise = new Promise(function (resolve, reject) {
33
+ roboflow
34
+ .auth({
35
+ publishable_key: publishable_key
36
+ })
37
+ .load(toLoad)
38
+ .then(function (m) {
39
+ model = m;
40
+ resolve();
41
+ });
42
+ });
43
+
44
+ Promise.all([startVideoStreamPromise, loadModelPromise]).then(function () {
45
+ $("body").removeClass("loading");
46
+ resizeCanvas();
47
+ detectFrame();
48
+ });
49
+
50
+ var canvas, ctx;
51
+ const font = "16px sans-serif";
52
+
53
+ function videoDimensions(video) {
54
+ // Ratio of the video's intrisic dimensions
55
+ var videoRatio = video.videoWidth / video.videoHeight;
56
+
57
+ // The width and height of the video element
58
+ var width = video.offsetWidth,
59
+ height = video.offsetHeight;
60
+
61
+ // The ratio of the element's width to its height
62
+ var elementRatio = width / height;
63
+
64
+ // If the video element is short and wide
65
+ if (elementRatio > videoRatio) {
66
+ width = height * videoRatio;
67
+ } else {
68
+ // It must be tall and thin, or exactly equal to the original ratio
69
+ height = width / videoRatio;
70
+ }
71
+
72
+ return {
73
+ width: width,
74
+ height: height
75
+ };
76
+ }
77
+
78
+ $(window).resize(function () {
79
+ resizeCanvas();
80
+ });
81
+
82
+ const resizeCanvas = function () {
83
+ $("canvas").remove();
84
+
85
+ canvas = $("<canvas/>");
86
+
87
+ ctx = canvas[0].getContext("2d");
88
+
89
+ var dimensions = videoDimensions(video);
90
+
91
+ console.log(
92
+ video.videoWidth,
93
+ video.videoHeight,
94
+ video.offsetWidth,
95
+ video.offsetHeight,
96
+ dimensions
97
+ );
98
+
99
+ canvas[0].width = video.videoWidth;
100
+ canvas[0].height = video.videoHeight;
101
+
102
+ canvas.css({
103
+ width: dimensions.width,
104
+ height: dimensions.height,
105
+ left: ($(window).width() - dimensions.width) / 2,
106
+ top: ($(window).height() - dimensions.height) / 2
107
+ });
108
+
109
+ $("body").append(canvas);
110
+ };
111
+
112
+ const renderPredictions = function (predictions) {
113
+ var dimensions = videoDimensions(video);
114
+
115
+ var scale = 1;
116
+
117
+ ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
118
+
119
+ predictions.forEach(function (prediction) {
120
+ const x = prediction.bbox.x;
121
+ const y = prediction.bbox.y;
122
+
123
+ const width = prediction.bbox.width;
124
+ const height = prediction.bbox.height;
125
+
126
+ // Draw the bounding box.
127
+ ctx.strokeStyle = prediction.color;
128
+ ctx.lineWidth = 4;
129
+ ctx.strokeRect(
130
+ (x - width / 2) / scale,
131
+ (y - height / 2) / scale,
132
+ width / scale,
133
+ height / scale
134
+ );
135
+
136
+ // Draw the label background.
137
+ ctx.fillStyle = prediction.color;
138
+ const textWidth = ctx.measureText(prediction.class).width;
139
+ const textHeight = parseInt(font, 10); // base 10
140
+ ctx.fillRect(
141
+ (x - width / 2) / scale,
142
+ (y - height / 2) / scale,
143
+ textWidth + 8,
144
+ textHeight + 4
145
+ );
146
+ });
147
+
148
+ predictions.forEach(function (prediction) {
149
+ const x = prediction.bbox.x;
150
+ const y = prediction.bbox.y;
151
+
152
+ const width = prediction.bbox.width;
153
+ const height = prediction.bbox.height;
154
+
155
+ // Draw the text last to ensure it's on top.
156
+ ctx.font = font;
157
+ ctx.textBaseline = "top";
158
+ ctx.fillStyle = "#000000";
159
+ ctx.fillText(
160
+ prediction.class,
161
+ (x - width / 2) / scale + 4,
162
+ (y - height / 2) / scale + 1
163
+ );
164
+ });
165
+ };
166
+
167
+ var prevTime;
168
+ var pastFrameTimes = [];
169
+ const detectFrame = function () {
170
+ if (!model) return requestAnimationFrame(detectFrame);
171
+
172
+ model
173
+ .detect(video)
174
+ .then(function (predictions) {
175
+ requestAnimationFrame(detectFrame);
176
+ renderPredictions(predictions);
177
+
178
+ if (prevTime) {
179
+ pastFrameTimes.push(Date.now() - prevTime);
180
+ if (pastFrameTimes.length > 30) pastFrameTimes.shift();
181
+
182
+ var total = 0;
183
+ _.each(pastFrameTimes, function (t) {
184
+ total += t / 1000;
185
+ });
186
+
187
+ var fps = pastFrameTimes.length / total;
188
+ $("#fps").text(Math.round(fps));
189
+ }
190
+ prevTime = Date.now();
191
+ })
192
+ .catch(function (e) {
193
+ console.log("CAUGHT", e);
194
+ requestAnimationFrame(detectFrame);
195
+ });
196
+ };
197
+ });