Skip to content

Commit 58fdb79

Browse files
authored
Merge branch 'development' into adding-testjs-for-UNET
2 parents e9d87cd + 1f69115 commit 58fdb79

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ There are several ways you can use the ml5.js library:
2626

2727

2828

29+
2930
<p id="latest-version">
3031

31-
* You can use the latest version (0.4.0) by adding it to the head section of your HTML document:
32+
* You can use the latest version (0.4.1) by adding it to the head section of your HTML document:
3233

33-
**v0.4.0**
34+
**v0.4.1**
3435

35-
<script src="https://unpkg.com/[email protected].0/dist/ml5.min.js" type="text/javascript"></script>
36+
<script src="https://unpkg.com/[email protected].1/dist/ml5.min.js" type="text/javascript"></script>
3637

3738
</p data-id="latest-version">
3839

3940

4041

4142

43+
4244

4345

4446
* If you need to use an earlier version for any reason, you can change the version number. The [previous versions of ml5 can be found here](https://www.npmjs.com/package/ml5). You can use those previous versions by replacing `<version>` with the ml5 version of interest:
@@ -49,7 +51,7 @@ There are several ways you can use the ml5.js library:
4951

5052
For example:
5153
```js
52-
<script src="https://unpkg.com/ml5@0.3.1/dist/ml5.min.js" type="text/javascript"></script>
54+
<script src="https://unpkg.com/ml5@0.4.1/dist/ml5.min.js" type="text/javascript"></script>
5355
```
5456

5557
* You can also reference "latest", but we do not recommend this as your code may break as we update ml5.

docs/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ The fastest way to get started exploring the creative possibilities of ml5.js ar
1919
3. You can also copy and paste the cdn link to the ml5 library here:
2020

2121
```
22-
<script src="https://unpkg.com/[email protected].0/dist/ml5.min.js"></script>
22+
<script src="https://unpkg.com/[email protected].1/dist/ml5.min.js"></script>
2323
```
2424

2525
***
2626
#### Quickstart: Plain JavaScript
2727

28-
Reference the [latest version](https://unpkg.com/[email protected].0/dist/ml5.min.js) of ml5.js using a script tag in an HTML file as below:
28+
Reference the [latest version](https://unpkg.com/[email protected].1/dist/ml5.min.js) of ml5.js using a script tag in an HTML file as below:
2929

3030

3131
In an **index.html** file, copy and paste the following and open up that file in your web browser.
@@ -36,13 +36,13 @@ In an **index.html** file, copy and paste the following and open up that file in
3636
<head>
3737
<title>Getting Started with ml5.js</title>
3838
<meta name="viewport" content="width=device-width, initial-scale=1.0">
39-
<script src="https://unpkg.com/[email protected].0/dist/ml5.min.js"></script>
39+
<script src="https://unpkg.com/[email protected].1/dist/ml5.min.js"></script>
4040
</head>
4141

4242
<body>
4343
<script>
4444
// Your code will go here
45-
// open up your console - if everything loaded properly you should see 0.4.0
45+
// open up your console - if everything loaded properly you should see 0.4.1
4646
console.log('ml5 version:', ml5.version);
4747
4848
</script>
@@ -71,7 +71,7 @@ In an **index.html** file, copy and paste the following and open up that file in
7171
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
7272
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.min.js"></script>
7373
<!-- ml5 -->
74-
<script src="https://unpkg.com/[email protected].0/dist/ml5.min.js"></script>
74+
<script src="https://unpkg.com/[email protected].1/dist/ml5.min.js"></script>
7575
</head>
7676

7777
<body>

docs/tutorials/hello-ml5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Here you can see that we read in the javascript libraries. This includes our ml5
7272

7373
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
7474
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
75-
<script src="https://unpkg.com/[email protected].0/dist/ml5.min.js"></script>
75+
<script src="https://unpkg.com/[email protected].1/dist/ml5.min.js"></script>
7676
</head>
7777

7878
<body>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ml5",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "A friendly machine learning library for the web.",
55
"main": "dist/ml5.min.js",
66
"directories": {

src/utils/imageUtilities.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// https://opensource.org/licenses/MIT
55

66
import * as tf from '@tensorflow/tfjs';
7+
import p5Utils from './p5Utils';
78

89
// Resize video elements
910
const processVideo = (input, size, callback = () => {}) => {
@@ -61,6 +62,64 @@ const cropImage = (img) => {
6162
return img.slice([beginHeight, beginWidth, 0], [size, size, 3]);
6263
};
6364

65+
const flipImage = (img) => {
66+
// image image, bitmap, or canvas
67+
let imgWidth;
68+
let imgHeight;
69+
let inputImg;
70+
71+
if (img instanceof HTMLImageElement ||
72+
img instanceof HTMLCanvasElement ||
73+
img instanceof HTMLVideoElement ||
74+
img instanceof ImageData) {
75+
inputImg = img;
76+
} else if (typeof img === 'object' &&
77+
(img.elt instanceof HTMLImageElement ||
78+
img.elt instanceof HTMLCanvasElement ||
79+
img.elt instanceof HTMLVideoElement ||
80+
img.elt instanceof ImageData)) {
81+
82+
inputImg = img.elt; // Handle p5.js image
83+
} else if (typeof img === 'object' &&
84+
img.canvas instanceof HTMLCanvasElement) {
85+
inputImg = img.canvas; // Handle p5.js image
86+
} else {
87+
inputImg = img;
88+
}
89+
90+
if (inputImg instanceof HTMLVideoElement) {
91+
// should be videoWidth, videoHeight?
92+
imgWidth = inputImg.width;
93+
imgHeight = inputImg.height;
94+
} else {
95+
imgWidth = inputImg.width;
96+
imgHeight = inputImg.height;
97+
}
98+
99+
100+
if (p5Utils.checkP5()) {
101+
const p5Canvas = p5Utils.p5Instance.createGraphics(imgWidth, imgHeight);
102+
p5Canvas.push()
103+
p5Canvas.translate(imgWidth, 0);
104+
p5Canvas.scale(-1, 1);
105+
p5Canvas.image(img, 0, 0, imgWidth, imgHeight);
106+
p5Canvas.pop()
107+
108+
return p5Canvas;
109+
}
110+
const canvas = document.createElement('canvas');
111+
canvas.width = imgWidth;
112+
canvas.height = imgHeight;
113+
114+
const ctx = canvas.getContext('2d');
115+
ctx.drawImage(inputImg, 0, 0, imgWidth, imgHeight);
116+
ctx.translate(imgWidth, 0);
117+
ctx.scale(-1, 1);
118+
ctx.drawImage(canvas, imgWidth * -1, 0, imgWidth, imgHeight);
119+
return canvas;
120+
121+
}
122+
64123
// Static Method: image to tf tensor
65124
function imgToTensor(input, size = null) {
66125
return tf.tidy(() => {
@@ -79,4 +138,5 @@ export {
79138
processVideo,
80139
cropImage,
81140
imgToTensor,
82-
};
141+
flipImage
142+
};

0 commit comments

Comments
 (0)