-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
79 lines (68 loc) · 2.17 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { Component } from 'react'
import logo from './logo.svg';
import cat from './test.jpg';
import './App.css';
import * as tf from '@tensorflow/tfjs';
class App extends Component {
constructor(props) {
super(props)
// Set initial state
this.state = { msg: '' }
// Binding this keyword
// this.handleClick = this.handleClick.bind(this)
}
handleClick() {
// Changing state
this.setState({ msg: '' })
}
async testML(imgRef) {
// const mobilenet = require('@tensorflow-models/mobilenet');
// Get the image reference
const img = tf.browser.fromPixels(imgRef.current);
// Convert the image to a 4d tensor
const smallImg = tf.image.resizeBilinear(img, [224, 224]);
const resized = tf.cast(smallImg, 'float32');
const t4d = tf.tensor4d(Array.from(resized.dataSync()), [1, 224, 224, 3])
// Load the model.
const model = await tf.loadLayersModel('https://gabrieldeml.github.io/Cancer-Client/models/model.json');
// Classify the image.
const predictions = model.predict(t4d);
// Get the output
const value = predictions.dataSync()
console.log('Predictions: ');
console.log(value);
// Display the output of the classification
if (value[0] == 0) {
this.setState({ msg: 'malignant' })
} else {
this.setState({ msg: 'Benign' })
}
}
render() {
const imgRef = React.createRef();
const handleImageUpload = e => {
const [file] = e.target.files;
if (file) {
const reader = new FileReader();
const { current } = imgRef;
current.file = file;
reader.onload = e => {
current.src = e.target.result;
};
reader.readAsDataURL(file);
}
};
return (
<div className="App">
<header className="App-header">
<h1>This is not medical advice it just is a test</h1>
<input type="file" accept="image/*" onChange={handleImageUpload} ref={imgRef} multiple="false" />
<img src={cat} ref={imgRef} />
<button onClick={() => this.testML(imgRef)}>Run model</button>
<p>{this.state.msg}</p>
</header>
</div>
);
}
}
export default App;