capjamesg commited on
Commit
8dddd4b
·
0 Parent(s):

Duplicate from capjamesg/roboflow

Browse files
Files changed (6) hide show
  1. .gitattributes +34 -0
  2. README.md +11 -0
  3. app.js +200 -0
  4. index.html +69 -0
  5. style.css +28 -0
  6. styles.css +75 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Roboflow
3
+ emoji: 🌍
4
+ colorFrom: green
5
+ colorTo: indigo
6
+ sdk: static
7
+ pinned: false
8
+ duplicated_from: capjamesg/roboflow
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.js ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // To display predictions, this app has:
2
+ // 1. A video that shows a feed from the user's webcam
3
+ // 2. A canvas that appears over the video and shows predictions
4
+ // When the page loads, a user is asked to give webcam permission.
5
+ // After this happens, the model initializes and starts to make predictions
6
+ // On the first prediction, an initialiation step happens in detectFrame()
7
+ // to prepare the canvas on which predictions are displayed.
8
+
9
+ var bounding_box_colors = {};
10
+
11
+ var user_confidence = 0.6;
12
+
13
+ // Update the colors in this list to set the bounding box colors
14
+ var color_choices = [
15
+ "#C7FC00",
16
+ "#FF00FF",
17
+ "#8622FF",
18
+ "#FE0056",
19
+ "#00FFCE",
20
+ "#FF8000",
21
+ "#00B7EB",
22
+ "#FFFF00",
23
+ "#0E7AFE",
24
+ "#FFABAB",
25
+ "#0000FF",
26
+ "#CCCCCC",
27
+ ];
28
+
29
+ var canvas_painted = false;
30
+ var canvas = document.getElementById("video_canvas");
31
+ var ctx = canvas.getContext("2d");
32
+
33
+ var model = null;
34
+
35
+
36
+ function detectFrame() {
37
+ // On first run, initialize a canvas
38
+ // On all runs, run inference using a video frame
39
+ // For each video frame, draw bounding boxes on the canvas
40
+ if (!model) return requestAnimationFrame(detectFrame);
41
+
42
+ model.detect(video).then(function(predictions) {
43
+
44
+ if (!canvas_painted) {
45
+ var video_start = document.getElementById("video1");
46
+ canvas.style.width = video_start.width + "px";
47
+ canvas.style.height = video_start.height + "px";
48
+ canvas.width = video_start.width;
49
+ canvas.height = video_start.height;
50
+ // adjust top to margin position of video
51
+
52
+ canvas.top = video_start.top;
53
+ canvas.left = video_start.left;
54
+ canvas.style.top = video_start.top + "px";
55
+ canvas.style.left = video_start.left + "px";
56
+ canvas.style.position = "absolute";
57
+ video_start.style.display = "block";
58
+ canvas.style.display = "absolute";
59
+ canvas_painted = true;
60
+
61
+ var loading = document.getElementById("loading");
62
+ loading.style.display = "none";
63
+ }
64
+ requestAnimationFrame(detectFrame);
65
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
66
+ if (video) {
67
+ drawBoundingBoxes(predictions, ctx)
68
+ }
69
+ });
70
+ }
71
+
72
+ function drawBoundingBoxes(predictions, ctx) {
73
+ // For each prediction, choose or assign a bounding box color choice,
74
+ // then apply the requisite scaling so bounding boxes appear exactly
75
+ // around a prediction.
76
+
77
+ // If you want to do anything with predictions, start from this function.
78
+ // For example, you could display them on the web page, check off items on a list,
79
+ // or store predictions somewhere.
80
+
81
+ for (var i = 0; i < predictions.length; i++) {
82
+ var confidence = predictions[i].confidence;
83
+
84
+ console.log(user_confidence)
85
+
86
+ if (confidence < user_confidence) {
87
+ continue
88
+ }
89
+
90
+ if (predictions[i].class in bounding_box_colors) {
91
+ ctx.strokeStyle = bounding_box_colors[predictions[i].class];
92
+ } else {
93
+ var color =
94
+ color_choices[Math.floor(Math.random() * color_choices.length)];
95
+ ctx.strokeStyle = color;
96
+ // remove color from choices
97
+ color_choices.splice(color_choices.indexOf(color), 1);
98
+
99
+ bounding_box_colors[predictions[i].class] = color;
100
+ }
101
+
102
+ var prediction = predictions[i];
103
+ var x = prediction.bbox.x - prediction.bbox.width / 2;
104
+ var y = prediction.bbox.y - prediction.bbox.height / 2;
105
+ var width = prediction.bbox.width;
106
+ var height = prediction.bbox.height;
107
+
108
+ ctx.rect(x, y, width, height);
109
+
110
+ ctx.fillStyle = "rgba(0, 0, 0, 0)";
111
+ ctx.fill();
112
+
113
+ ctx.fillStyle = ctx.strokeStyle;
114
+ ctx.lineWidth = "4";
115
+ ctx.strokeRect(x, y, width, height);
116
+ ctx.font = "25px Arial";
117
+ ctx.fillText(prediction.class + " " + Math.round(confidence * 100) + "%", x, y - 10);
118
+ }
119
+ }
120
+
121
+ function webcamInference() {
122
+ // Ask for webcam permissions, then run main application.
123
+ var loading = document.getElementById("loading");
124
+ loading.style.display = "block";
125
+
126
+ navigator.mediaDevices
127
+ .getUserMedia({ video: { facingMode: "environment" } })
128
+ .then(function(stream) {
129
+ video = document.createElement("video");
130
+ video.srcObject = stream;
131
+ video.id = "video1";
132
+
133
+ // hide video until the web stream is ready
134
+ video.style.display = "none";
135
+ video.setAttribute("playsinline", "");
136
+
137
+ document.getElementById("video_canvas").after(video);
138
+
139
+ video.onloadedmetadata = function() {
140
+ video.play();
141
+ }
142
+
143
+ // on full load, set the video height and width
144
+ video.onplay = function() {
145
+ height = video.videoHeight;
146
+ width = video.videoWidth;
147
+
148
+ // scale down video by 0.75
149
+
150
+ height = height * 0.75;
151
+ width = width * 0.75;
152
+
153
+ width = Math.round(width);
154
+ height = Math.round(height);
155
+
156
+ video.setAttribute("width", width);
157
+ video.setAttribute("height", height);
158
+ video.style.width = width + "px";
159
+ video.style.height = height + "px";
160
+
161
+ canvas.style.width = width + "px";
162
+ canvas.style.height = height + "px";
163
+ canvas.width = width;
164
+ canvas.height = height;
165
+
166
+ document.getElementById("video_canvas").style.display = "block";
167
+ };
168
+
169
+ ctx.scale(1, 1);
170
+
171
+ // Load the Roboflow model using the publishable_key set in index.html
172
+ // and the model name and version set at the top of this file
173
+ roboflow
174
+ .auth({
175
+ publishable_key: publishable_key,
176
+ })
177
+ .load({
178
+ model: MODEL_NAME,
179
+ version: MODEL_VERSION,
180
+ })
181
+ .then(function(m) {
182
+ model = m;
183
+ // Images must have confidence > CONFIDENCE_THRESHOLD to be returned by the model
184
+ m.configure({ threshold: CONFIDENCE_THRESHOLD });
185
+ // Start inference
186
+ detectFrame();
187
+ });
188
+ })
189
+ .catch(function(err) {
190
+ console.log(err);
191
+ });
192
+ }
193
+
194
+ function changeConfidence () {
195
+ user_confidence = document.getElementById("confidence").value / 100;
196
+ }
197
+
198
+ document.getElementById("confidence").addEventListener("input", changeConfidence);
199
+
200
+ webcamInference();
index.html ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+
9
+ <title>Roboflow + Hugging Face Example</title>
10
+
11
+ <!-- Load roboflow.js, the script that will handle loading and running our computer vision model in the browser -->
12
+ <script src="https://cdn.roboflow.com/0.2.25/roboflow.js"></script>
13
+
14
+ <link rel="stylesheet" href="/styles.css" />
15
+
16
+ <script>
17
+ // Replace the key below with your Roboflow publishable key
18
+ var publishable_key = "rf_U7AD2Mxh39N7jQ3B6cP8xAyufLH3";
19
+
20
+ // Change these values to set your model name, version, and the
21
+ // confidence threshold that must be met for the model to return
22
+ // a prediction.
23
+
24
+ // We recommend setting a low CONFIDENCE_THRESHOLD value while testing
25
+ // and filtering out predictions by confidence level using the
26
+ //"Prediction Confidence %" filter
27
+
28
+ const CONFIDENCE_THRESHOLD = 0.1;
29
+ const MODEL_NAME = "microsoft-coco";
30
+ const MODEL_VERSION = 9;
31
+ </script>
32
+ </head>
33
+
34
+ <body>
35
+ <main>
36
+ <h1>Roboflow + Hugging Face Example 🚀</h1>
37
+ <p>Roboflow enables you to build and run custom computer vision models in your browser, on your device, and via API.
38
+ </p>
39
+ <p>The below example runs <a href="https://universe.roboflow.com/jacob-solawetz/microsoft-coco">Microsoft's
40
+ COCO</a> model to identify common objects, which is one of 50,000 open source models ready to use on <a
41
+ href="https://universe.roboflow.com">Roboflow Universe</a></p>
42
+
43
+ <p>Using the demo below, you can identify <a href="https://universe.roboflow.com/jacob-solawetz/microsoft-coco">80
44
+ different objects</a> using your webcam, from people to chairs to cups.</p>
45
+
46
+ <p id="loading">Loading...</p>
47
+
48
+ <!-- The canvas below is required -->
49
+ <section class="infer-widget">
50
+ <canvas width="640" height="480" id="video_canvas"></canvas>
51
+ </section>
52
+
53
+ <section id="settings">
54
+ <label for="confidence">Prediction Confidence %</label><br>
55
+ <input type="range" min="1" max="100" value="60" class="slider" id="confidence">
56
+ </section>
57
+
58
+ <section class="links">
59
+ <a href="https://blog.roboflow.com/getting-started-with-roboflow/" class="styled-button">Build a Custom Model</a>
60
+ <a href="https://universe.roboflow.com" class="styled-button styled-button-turqouise">Explore 50k+
61
+ Models and Datasets</a>
62
+ </section>
63
+
64
+ <!-- Load our application logic -->
65
+ <script src="/app.js"></script>
66
+ </main>
67
+ </body>
68
+
69
+ </html>
style.css ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ padding: 2rem;
3
+ font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
+ }
5
+
6
+ h1 {
7
+ font-size: 16px;
8
+ margin-top: 0;
9
+ }
10
+
11
+ p {
12
+ color: rgb(107, 114, 128);
13
+ font-size: 15px;
14
+ margin-bottom: 10px;
15
+ margin-top: 5px;
16
+ }
17
+
18
+ .card {
19
+ max-width: 620px;
20
+ margin: 0 auto;
21
+ padding: 16px;
22
+ border: 1px solid lightgray;
23
+ border-radius: 16px;
24
+ }
25
+
26
+ .card p:last-child {
27
+ margin-bottom: 0;
28
+ }
styles.css ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ html, body {
2
+ height: 100%;
3
+ width: 100%;
4
+ font-family: Arial, Helvetica, sans-serif;
5
+ }
6
+
7
+ p, a {
8
+ line-height: 1.5em;
9
+ }
10
+
11
+ #loading {
12
+ display: none;
13
+ }
14
+
15
+ #settings {
16
+ margin-top: 30px;
17
+ margin-bottom: 30px;
18
+ }
19
+
20
+ h1, a {
21
+ color: #6706ce;
22
+ }
23
+
24
+ label {
25
+ font-weight: 50px;
26
+ }
27
+
28
+ a {
29
+ text-decoration: none;
30
+ }
31
+
32
+ main {
33
+ width: 40em;
34
+ margin: auto;
35
+ text-align: center;
36
+ }
37
+
38
+ .styled-button {
39
+ color: #fff;
40
+ text-align: center;
41
+ background-color: #6706ce;
42
+ border: 1px solid #6706ce;
43
+ border-radius: 4px;
44
+ padding: 13px 20px;
45
+ line-height: 1.5em;
46
+ text-decoration: none;
47
+ display: block;
48
+ margin: 10px;
49
+ margin-bottom: 25px;
50
+ width: 100%;
51
+ }
52
+
53
+ .styled-button-turqouise {
54
+ border: none;
55
+ color: black;
56
+ background-color: #00ffce;
57
+ }
58
+
59
+ .infer-widget {
60
+ width: 480px;
61
+ margin: auto;
62
+ }
63
+
64
+ .links {
65
+ display: flex;
66
+ text-align: center;
67
+ }
68
+
69
+ .links:first-child {
70
+ flex: 0 50%;
71
+ }
72
+
73
+ .links:last-child {
74
+ flex: 1 50%;
75
+ }