Last active
February 5, 2019 14:11
-
-
Save TOcvfan/aa9fea095c93fa646bc341a345f4b779 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Clarifai from 'clarifai'; | |
import Navigation from './components/Navigation/Navigation'; | |
import FaceRecognition from './components/FaceRecognition/FaceRecognition'; | |
import Logo from './components/Logo/Logo'; | |
import Register from './components/Register/Register'; | |
import Signin from './components/Signin/Signin'; | |
import ImageLinkForm from './components/ImageLinkForm/ImageLinkForm'; | |
import Rank from './components/Rank/Rank' | |
import ParticleAnimation from 'react-particle-animation' | |
import './App.css'; | |
const app = new Clarifai.App({ | |
apiKey: 'API_KEY' | |
}); | |
const particlesOptions = { | |
particles: { | |
number: { | |
value: 30, | |
density: { | |
enable: true, | |
value_area: 800 | |
} | |
} | |
} | |
} | |
class App extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
input: '', | |
imageUrl: '', | |
box: {}, | |
route: 'signin', | |
isSignedIn: false, | |
user: { | |
id: '', | |
name: '', | |
email: '', | |
entries: 0, | |
joined: '' | |
} | |
} | |
} | |
loadUser = (data) => { | |
this.setState({user: { | |
id: data.id, | |
name: data.name, | |
email: data.email, | |
entries: data.entries, | |
joined: data.joined | |
}}) | |
} | |
calculateFaceLocation = (data) => { | |
const face = data.outputs[0].data.regions[0].region_info.bounding_box; | |
const image = document.getElementById('inputimage'); | |
const width = Number(image.width); | |
const height = Number(image.height); | |
return { | |
leftCol: face.left_col * width, | |
topRow: face.top_row * height, | |
rightCol: width - (face.right_col * width), | |
bottomRow: height - (face.bottom_row * height) | |
} | |
} | |
displayFaceBox = (box) => { | |
this.setState({box: box}); | |
} | |
onInputChange = (event) => { | |
this.setState({input: event.target.value}); | |
} | |
onButtonSubmit = () => { | |
this.setState({imageUrl: this.state.input}) | |
app.models | |
.predict( | |
Clarifai.FACE_DETECT_MODEL, | |
this.state.input) | |
.then(response => { | |
if (response) { | |
fetch('http://localhost:3000/image', { | |
method: 'put', | |
headers: {'Content-Type': 'application/json'}, | |
body: JSON.stringify({ | |
id: this.state.user.id | |
}) | |
}).then((response) => response.json()) | |
.then((count) => { | |
this.setState(Object.assign(this.state.user, {entries: count})); | |
}) | |
} | |
this.displayFaceBox(this.calculateFaceLocation(response)) | |
}) | |
.catch(err => console.log(err)); | |
} | |
onRouteChange = (route) => { | |
if (route === 'signout'){ | |
this.setState({isSignedIn: false}) | |
} else if (route === 'home'){ | |
this.setState({isSignedIn: true}) | |
} | |
this.setState({route: route}) | |
} | |
render() { | |
const {isSignedIn, imageUrl, route, box} = this.state; | |
return ( | |
<div className="App"> | |
<ParticleAnimation | |
className='particles' | |
params={particlesOptions} | |
/> | |
<Navigation isSignedIn={isSignedIn} onRouteChange={this.onRouteChange}/> | |
{ route === 'home' | |
? <div> | |
<Logo /> | |
<Rank | |
name={this.state.user.name} | |
entries={this.state.user.entries} | |
/> | |
<ImageLinkForm | |
onInputChange={this.onInputChange} | |
onButtonSubmit={this.onButtonSubmit} | |
/> | |
<FaceRecognition box={box} imageUrl={imageUrl}/> | |
</div> | |
: ( | |
route === 'signin' | |
? <Signin loadUser={this.loadUser} onRouteChange={this.onRouteChange}/> | |
: <Register loadUser={this.loadUser} onRouteChange={this.onRouteChange}/> | |
) | |
} | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment