Skip to content

Commit

Permalink
camera
Browse files Browse the repository at this point in the history
  • Loading branch information
nik committed Aug 9, 2019
1 parent 604a189 commit 0ea09a6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 38 deletions.
1 change: 0 additions & 1 deletion client/public/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="./renderer.js"></script>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
Expand Down
59 changes: 31 additions & 28 deletions client/src/App.css
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
.App {
text-align: center;
.camera-video {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
display: block;
z-index: 1;
background-color: #000;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 40vmin;
pointer-events: none;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
.camera-pic {
position: fixed;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
z-index: 2;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.camera-pic img {
height: 100%;
}
.camera-button {
width: 100px;
height: 100px;
border-radius: 200px;
background-color: #bbb;
color: #000;
z-index: 3;
position: fixed;
left: 50%;
margin-left: -50px;
bottom: 20px;
display: flex;
align-items: center;
justify-content: center;
}
90 changes: 81 additions & 9 deletions client/src/containers/camera.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
import React, { useRef } from 'react';

const Camera = ({ next }) => {
return (
<div>
<h1>Camera</h1>
<button onClick={next}>TAKE</button>
</div>
);
import React from 'react';
import $ from 'jquery';

var params = {
returnFaceId: 'false',
returnFaceLandmarks: 'false',
returnFaceAttributes: 'age,gender,emotion',
};

class Camera extends React.Component {
constructor(props) {
super(props);
this.state = { img: null, loading: false };
this.video = React.createRef();
this.take = this.take.bind(this);
}

componentDidMount() {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
this.video.current.srcObject = stream;
this.video.current.play();
});
} else {
console.error('No media devices found');
}
}

take() {
const next = this.props.next;
const canvas = document.createElement('canvas');
canvas.width = this.video.current.offsetWidth;
canvas.height = this.video.current.offsetHeight;
const context = canvas.getContext('2d');
context.drawImage(this.video.current, 0, 0, canvas.width, canvas.height);
const data = canvas.toDataURL('image/jpeg');
this.setState({ img: data });
const faceAPIKey = new URLSearchParams(window.location.search).get(
'faceAPIKey'
);
this.video.current.pause();
this.setState({ loading: true });
fetch(data)
.then(res => res.blob())
.then(blobData => {
$.post({
url:
'https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect?' +
$.param(params),
contentType: 'application/octet-stream',
headers: {
'Ocp-Apim-Subscription-Key': '1d33abc84bdb49d5b34870cda25e724b',
},
processData: false,
data: blobData,
})
.done(function(data) {
var faceResult = {
peopleCount: data.length,
};
next();
console.log('Face API result: ', faceResult, data);
})
.fail(function(err) {
this.setState({ loading: false });
console.error('Face API error');
});
});
}

render() {
const { img, loading } = this.state;
return (
<div>
<video className="camera-video" ref={this.video} id="video" autoPlay />
{!loading ? <div className="camera-button" onClick={this.take}>
TAKE
</div> : <div>loading</div>}
</div>
);
}
}

export default Camera;
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0ea09a6

Please sign in to comment.