Skip to content

Commit de31d38

Browse files
committed
Convert an image to a tensor
1 parent f70eab9 commit de31d38

File tree

6 files changed

+5305
-0
lines changed

6 files changed

+5305
-0
lines changed

image-to-tensor/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# image-to-tensor
2+
3+
> Convert pixels to tensors
4+
5+
## Getting Started
6+
7+
```
8+
yarn
9+
yarn start
10+
```

image-to-tensor/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>TensorFlow Template</title>
5+
<meta charset="UTF-8" />
6+
<link
7+
rel="stylesheet"
8+
href="./node_modules/modern-normalize/modern-normalize.css"
9+
/>
10+
</head>
11+
12+
<body>
13+
<div id="app"></div>
14+
15+
<script src="./src/index.js"></script>
16+
</body>
17+
</html>

image-to-tensor/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "image-to-tensor",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.html",
6+
"scripts": {
7+
"start": "parcel index.html --open",
8+
"build": "parcel build index.html"
9+
},
10+
"dependencies": {
11+
"@tensorflow/tfjs": "^2.0.1",
12+
"modern-normalize": "^0.7.0"
13+
},
14+
"devDependencies": {
15+
"@babel/core": "7.2.0",
16+
"parcel-bundler": "^1.6.1"
17+
},
18+
"keywords": []
19+
}

image-to-tensor/src/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as tf from '@tensorflow/tfjs';
2+
import './styles.css';
3+
4+
const img = new Image();
5+
img.crossOrigin = 'anonymous';
6+
img.src =
7+
'https://images.unsplash.com/flagged/photo-1572349854775-e5768e1afb74?ixlib=rb-1.2.1&auto=format&fit=crop&w=1267&q=80';
8+
9+
img.onload = (event) => {
10+
const imgTensor = tf.browser.fromPixels(event.target);
11+
imgTensor.print();
12+
13+
document.getElementById('app').innerHTML = `
14+
<code>TensorFlow version: ${tf.version.tfjs}</code>
15+
<h1>${imgTensor.shape[1]}x${imgTensor.shape[0]}</h1>
16+
<canvas id="printCanvas"></canvas>
17+
`;
18+
19+
const printCanvas = document.getElementById('printCanvas');
20+
tf.browser.toPixels(imgTensor, printCanvas);
21+
};

image-to-tensor/src/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
padding: 20px 40px;
3+
}

0 commit comments

Comments
 (0)