Skip to content

Instantly share code, notes, and snippets.

@supachaic
supachaic / App.js
Created January 7, 2019 13:49
src/App.js final look
import React, { Component } from 'react';
import { Route, Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import './App.css';
import Home from './views/Home';
import ImageInput from './views/ImageInput';
import VideoInput from './views/VideoInput';
class App extends Component {
@supachaic
supachaic / VideoInput.js
Created January 7, 2019 11:00
src/views/VideoInput.js
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import Webcam from 'react-webcam';
import { loadModels, getFullFaceDescription, createMatcher } from '../api/face';
// Import face profile
const JSON_PROFILE = require('../descriptors/bnk48.json');
const WIDTH = 420;
const HEIGHT = 420;
@supachaic
supachaic / App.js
Created January 7, 2019 09:33
Add Video Input Component
import React, { Component } from 'react';
import { Route, Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import './App.css';
import Home from './views/Home';
import ImageInput from './views/ImageInput';
import VideoInput from './views/VideoInput';
class App extends Component {
@supachaic
supachaic / ImageInput.js
Last active January 7, 2019 10:54
Final code
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import { loadModels, getFullFaceDescription, createMatcher } from '../api/face';
// Import image to test API
const testImg = require('../img/test.jpg');
// Import face profile
const JSON_PROFILE = require('../descriptors/bnk48.json');
@supachaic
supachaic / face.js
Created January 7, 2019 04:56
Create Face Matcher
// face.js code...
const maxDescriptorDistance = 0.5;
export async function createMatcher(faceProfile) {
// Create labeled descriptors of member from profile
let members = Object.keys(faceProfile);
let labeledDescriptors = members.map(
member =>
new faceapi.LabeledFaceDescriptors(
faceProfile[member].name,
{
"Cherprang": {
"name": "เฌอปราง",
"descriptors": [
[-0.08113786578178406,0.05256778374314308,0.009473100304603577,-0.10561250895261765,-0.1783013790845871,-0.04459364339709282,-0.09838695079088211,-0.11281408369541168,0.15290355682373047,-0.12802188098430634,0.22490034997463226,-0.0876401960849762,-0.20344389975070953,-0.06546909362077713,-0.09518688917160034,0.21771210432052612,-0.1978156566619873,-0.20811164379119873,-0.005263347178697586,0.022171149030327797,0.08166252076625824,-0.024958046153187752,-0.008690833114087582,0.07264935970306396,-0.09877212345600128,-0.32639357447624207,-0.07010464370250702,-0.05077863112092018,-0.06450583040714264,-0.08223631232976913,0.03569770231842995,0.0591503381729126,-0.19877424836158752,0.005495572462677956,0.032966941595077515,0.10699400305747986,0.00043915724381804466,-0.10360816866159439,0.17986394464969635,-0.040484149008989334,-0.30697202682495117,0.03211132436990738,0.08842744678258896,0.19950658082962036,0.15933744609355927,-0.03205663338303566,
@supachaic
supachaic / ImageInput.js
Last active January 6, 2019 08:29
Draw face detection box
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import { loadModels, getFullFaceDescription } from '../api/face';
// Import image to test API
const testImg = require('../img/test.jpg');
// Initial State
const INIT_STATE = {
imageURL: testImg,
@supachaic
supachaic / App.js
Created January 4, 2019 08:53
Import ImageInput component and create new Route
import React, { Component } from 'react';
import { Route, Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import './App.css';
import Home from './views/Home';
import ImageInput from './views/ImageInput';
class App extends Component {
render() {
@supachaic
supachaic / ImageInput.js
Last active January 6, 2019 07:04
src/views/ImageInput.js
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import { loadModels, getFullFaceDescription } from '../api/face';
// Import image to test API
const testImg = require('../img/test.jpg');
// Initial State
const INIT_STATE = {
imageURL: testImg,
@supachaic
supachaic / face.js
Last active March 9, 2019 04:16
src/api/face.js
import * as faceapi from 'face-api.js';
// Load models and weights
export async function loadModels() {
const MODEL_URL = process.env.PUBLIC_URL + '/models';
await faceapi.loadTinyFaceDetectorModel(MODEL_URL);
await faceapi.loadFaceLandmarkTinyModel(MODEL_URL);
await faceapi.loadFaceRecognitionModel(MODEL_URL);
}